Last active
November 13, 2025 16:39
-
-
Save akiraaisha/c3540fd8f8cfce14071a23a74e4f3d43 to your computer and use it in GitHub Desktop.
Bash Script for User Creation with Group Assignment, Batch Mode, and Audit‑Friendly Options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # add_user.sh - Create users with default or custom password and assign to groups | |
| # Usage: | |
| # Single user: sudo ./add_user.sh -u <username> -g <group> [-p <password>] | |
| # Batch file: sudo ./add_user.sh -b <file> | |
| # Options: | |
| # -h Show help message | |
| # -v Enable verbose output | |
| # -p Set custom password (otherwise uses default) | |
| DEFAULT_PASS="ChangeMe123!" # fallback default password | |
| VERBOSE=false | |
| PASSWORD="" | |
| usage() { | |
| echo "Usage:" | |
| echo " $0 -u <username> -g <group> [-p <password>] # Create single user" | |
| echo " $0 -b <file> # Batch create users from file (CSV: username,group,password)" | |
| echo "Options:" | |
| echo " -h Show this help message" | |
| echo " -v Enable verbose output" | |
| echo " -p Set custom password" | |
| exit 0 | |
| } | |
| log() { | |
| if [ "$VERBOSE" = true ]; then | |
| echo "[INFO] $1" | |
| fi | |
| } | |
| create_user() { | |
| local USERNAME=$1 | |
| local GROUP=$2 | |
| local PASS=$3 | |
| # Use provided password or fallback default | |
| [ -z "$PASS" ] && PASS=$DEFAULT_PASS | |
| log "Checking group $GROUP..." | |
| if ! getent group "$GROUP" > /dev/null; then | |
| echo "Group $GROUP does not exist. Creating it..." | |
| groupadd "$GROUP" | |
| fi | |
| log "Creating user $USERNAME in group $GROUP..." | |
| useradd -m -g "$GROUP" -s /bin/bash "$USERNAME" | |
| log "Setting password for $USERNAME..." | |
| echo "${USERNAME}:${PASS}" | chpasswd | |
| log "Forcing password change on first login..." | |
| chage -d 0 "$USERNAME" | |
| echo "✅ User $USERNAME created in group $GROUP with password set." | |
| } | |
| # --- Root check --- | |
| if [ "$EUID" -ne 0 ]; then | |
| echo "❌ This script must be run as root (use sudo)." | |
| exit 1 | |
| fi | |
| # Parse parameters | |
| while getopts "u:g:b:p:hv" opt; do | |
| case $opt in | |
| u) USERNAME=$OPTARG ;; | |
| g) GROUP=$OPTARG ;; | |
| b) BATCHFILE=$OPTARG ;; | |
| p) PASSWORD=$OPTARG ;; | |
| h) usage ;; | |
| v) VERBOSE=true ;; | |
| *) usage ;; | |
| esac | |
| done | |
| # Batch mode | |
| if [ -n "$BATCHFILE" ]; then | |
| if [ ! -f "$BATCHFILE" ]; then | |
| echo "Batch file $BATCHFILE not found!" | |
| exit 1 | |
| fi | |
| while IFS=',' read -r USERNAME GROUP PASS; do | |
| [ -z "$USERNAME" ] && continue | |
| create_user "$USERNAME" "$GROUP" "$PASS" | |
| done < "$BATCHFILE" | |
| exit 0 | |
| fi | |
| # Single user mode | |
| if [ -n "$USERNAME" ] && [ -n "$GROUP" ]; then | |
| create_user "$USERNAME" "$GROUP" "$PASSWORD" | |
| else | |
| usage | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment