Skip to content

Latest commit

 

History

History
212 lines (155 loc) · 4.3 KB

File metadata and controls

212 lines (155 loc) · 4.3 KB

Got it 👍. Based on the requirements and the steps shown in your image, here’s the complete working Bash script for creating a new user with an auto-generated password, setting the password, and forcing the user to change it at first login:

#!/bin/bash

# Script should be executed with root privileges
if [[ "${UID}" -ne 0 ]]; then
  echo "Please run with sudo or root."
  exit 1
fi

# Check if at least one argument is provided
if [[ "${#}" -lt 1 ]]; then
  echo "Usage: ${0} USER_NAME [COMMENT]..."
  echo "Create a user with name USER_NAME and optional COMMENT."
  exit 1
fi

# Store the first argument as the username
USER_NAME="${1}"

# The rest of the arguments are treated as comment
shift
COMMENT="${@}"

# Generate a random password
PASSWORD=$(date +%s%N)

# Create the user with the comment and home directory
useradd -c "${COMMENT}" -m ${USER_NAME}

# Check if user was successfully created
if [[ $? -ne 0 ]]; then
  echo "The account could not be created."
  exit 1
fi

# Set the password for the user
echo ${PASSWORD} | passwd --stdin ${USER_NAME} &>/dev/null

# Check if password was set successfully
if [[ $? -ne 0 ]]; then
  echo "Password could not be set."
  exit 1
fi

# Force password change on first login
passwd -e ${USER_NAME} &>/dev/null

# Display the username, password, and hostname
echo
echo "Username: ${USER_NAME}"
echo "Password: ${PASSWORD}"
echo "Host: $(hostname)"

✅ This script will:

  • Exit if not run as root.
  • Require at least one argument (username).
  • Treat any extra arguments as a comment.
  • Auto-generate a random password.
  • Create the user with home directory.
  • Assign the password and enforce a change at first login.
  • Display username, password, and host info.

Perfect 👍 Let me explain this script step by step in simple words:

#!/bin/bash
  • This line tells the system to use bash shell to run the script.

if [[ "${UID}" -ne 0 ]]; then
  echo "Please run with sudo or root."
  exit 1
fi
  • ${UID} is a special variable that stores the current user’s ID.
  • Root’s UID is always 0.
  • If the script is not run as root (UID not 0), it prints an error and exits with status 1.

if [[ "${#}" -lt 1 ]]; then
  echo "Usage: ${0} USER_NAME [COMMENT]..."
  echo "Create a user with name USER_NAME and optional COMMENT."
  exit 1
fi
  • ${#} = total number of arguments passed to the script.
  • If fewer than 1 argument is given, it shows help/usage message and exits.
  • ${0} = script name.

USER_NAME="${1}"
  • The first argument passed is stored as the username.

shift
COMMENT="${@}"
  • shift removes the first argument (username), so now only the extra arguments remain.
  • ${@} = all remaining arguments.
  • These are saved in COMMENT, which will be stored as the account description.

PASSWORD=$(date +%s%N)
  • This generates a password based on the current date + nanoseconds.
  • Example: 1680344598842862051
  • Ensures uniqueness each time the script runs.

useradd -c "${COMMENT}" -m ${USER_NAME}
  • Creates a new user:

    • -c "${COMMENT}" = adds the comment (like full name / description).
    • -m = ensures a home directory is created.

if [[ $? -ne 0 ]]; then
  echo "The account could not be created."
  exit 1
fi
  • $? = exit status of the last command.
  • If user creation (useradd) failed, it prints an error and exits.

echo ${PASSWORD} | passwd --stdin ${USER_NAME} &>/dev/null
  • Sets the generated password for the user.
  • --stdin lets passwd read the password from standard input.
  • &>/dev/null hides any output (makes it silent).

if [[ $? -ne 0 ]]; then
  echo "Password could not be set."
  exit 1
fi
  • If setting the password fails, exit with error.

passwd -e ${USER_NAME} &>/dev/null
  • Forces the user to change the password on first login.

echo
echo "Username: ${USER_NAME}"
echo "Password: ${PASSWORD}"
echo "Host: $(hostname)"
  • Prints the new username, password, and the system hostname.
  • $(hostname) = machine name where the account was created.

🔑 Example Run:

sudo ./create_user.sh Alex "Test User"

Output:

Username: Alex
Password: 1680344598842862051
Host: server1