DEV Community

Cover image for Creating Linux Users with a Bash Script
Boaz Sottie
Boaz Sottie

Posted on

Creating Linux Users with a Bash Script

As a SysOps engineer, efficiently managing user accounts and their permissions is a critical task. In this article, I will walk you through a bash script that automates the creation of users and their associated groups on a Linux system. This script reads from a text file, generates random passwords, and logs all actions for easy tracking.

Script Overview

The script, create_users.sh, is designed to read a text file containing usernames and group names, create the users and their respective groups, set up home directories, and generate passwords securely.

Detailed Explanation

Input File and Initial Setup

The script starts by checking if an input file is provided and sets up the necessary directories and files for logging and storing passwords securely.

if [ $# -ne 1 ]; then
  echo "Usage: $0 <name-of-text-file>"
  exit 1
fi

input_file=$1
log_file="/var/log/user_management.log"
password_file="/var/secure/user_passwords.csv"

mkdir -p /var/secure
chmod 700 /var/secure

> $log_file
> $password_file
chmod 600 $password_file

Enter fullscreen mode Exit fullscreen mode

Generating Random Passwords

A function to generate a random password is defined. This function creates a 12-character password using a mix of alphanumeric characters.

generate_password() {
  < /dev/urandom tr -dc 'A-Za-z0-9' | head -c 12
}

Enter fullscreen mode Exit fullscreen mode

Processing the Input File

The script reads each line of the input file, extracting the username and groups. It removes any existing whitespace and checks if the user already exists.

while IFS=';' read -r username groups; do
  username=$(echo "$username" | xargs)
  groups=$(echo "$groups" | xargs)

  if id "$username" &>/dev/null; then
    echo "User $username already exists. Skipping..." | tee -a $log_file
    continue
  fi

Enter fullscreen mode Exit fullscreen mode

Creating Users and Groups
If the user does not exist, the script creates the user along with a personal group, generates a password, and sets the password for the user. It also logs these actions.

  useradd -m -s /bin/bash -G "$username" "$username"
  echo "Created user $username with group $username" | tee -a $log_file

  password=$(generate_password)
  echo "$username:$password" | chpasswd
  echo "$username,$password" >> $password_file
  echo "Set password for user $username" | tee -a $log_file

Enter fullscreen mode Exit fullscreen mode

Adding Users to Additional Groups
If additional groups are specified, the script creates those groups if they do not already exist and adds the user to these groups.

  if [ -n "$groups" ]; then
    IFS=',' read -ra ADDR <<< "$groups"
    for group in "${ADDR[@]}"; do
      if ! getent group "$group" &>/dev/null; then
        groupadd "$group"
        echo "Created group $group" | tee -a $log_file
      fi
      usermod -aG "$group" "$username"
      echo "Added user $username to group $group" | tee -a $log_file
    done
  fi
done < "$input_file"

Enter fullscreen mode Exit fullscreen mode

Conclusion

This script simplifies the task of managing user accounts in a Linux environment, ensuring that all necessary actions are logged and passwords are stored securely. By automating user creation and group assignments, SysOps engineers can save time and reduce the potential for errors.

Learn more about the HNG Internship program and how it empowers interns with practical tasks Here and Here.

Top comments (0)