DEV Community

Cover image for Automating Linux User Management with Bash Script
Nnamdi Kenneth Nwosu
Nnamdi Kenneth Nwosu

Posted on

Automating Linux User Management with Bash Script

Introduction
Managing user accounts in a Linux environment can be a daunting task, especially when onboarding a large number of new developers. To streamline this process, I have created a Bash script, create_users.sh, which automates the creation of user accounts, assigns them to appropriate groups, generates random passwords, and logs all actions performed.

This article explains the script in detail and demonstrates its usage. The script and article are part of the HNG Internship task, and you can learn more about the program (https://hng.tech/internship) and (https://hng.tech/premium)

Image description

Script Breakdown
Prerequisites
Ensure that your system has the necessary permissions to create users, groups, and modify system files. You need sudo access to run the script successfully.

Script Explanation
Logging and Password Files Initialization:
The script initializes the log file (/var/log/user_management.log) and the password file (/var/secure/user_passwords.csv). It sets appropriate permissions to ensure only the file owner can read the password file.

LOGFILE="/var/log/user_management.log"
PASSFILE="/var/secure/user_passwords.csv"
touch $LOGFILE
touch $PASSFILE
chmod 600 $PASSFILE

Enter fullscreen mode Exit fullscreen mode

Logging Function:
A function log_action is defined to log actions with a timestamp.

log_action() {
    echo "$(date "+%Y-%m-%d %H:%M:%S") - $1" >> $LOGFILE
}

Enter fullscreen mode Exit fullscreen mode

Input File Check:
The script checks if an input file is provided as an argument. If not, it exits with a usage message.

if [ -z "$1" ]; then
    echo "Usage: bash create_users.sh <name-of-text-file>"
    exit 1
fi

Enter fullscreen mode Exit fullscreen mode

Reading Input File:
The script reads the input file line by line, processing each username and associated groups.

while IFS=';' read -r username groups; do
    # Processing logic
done < "$1"

Enter fullscreen mode Exit fullscreen mode

User and Group Creation:
For each line, the script:

Removes leading/trailing whitespace.
Checks if the user already exists.
Creates the user with a personal group.
Creates additional groups if specified and adds the user to these groups.

if id -u "$username" >/dev/null 2>&1; then
    log_action "User $username already exists."
    continue
fi
useradd -m -s /bin/bash -G "$username" "$username"

Enter fullscreen mode Exit fullscreen mode

Password Generation:
The script generates a random password using openssl, sets it for the user, and stores it securely.

password=$(openssl rand -base64 12)
echo "$username:$password" | chpasswd
echo "$username,$password" >> $PASSFILE

Enter fullscreen mode Exit fullscreen mode

Completion Log:
The script logs the completion of the user creation process.

log_action "User creation process completed."
echo "User creation process completed. Check $LOGFILE for details."

Enter fullscreen mode Exit fullscreen mode

Conclusion
The create_users.sh script simplifies the task of managing user accounts in a Linux environment by automating user and group creation, password generation, and logging. It ensures security and efficiency, making it an essential tool for SysOps engineers.

To learn more about the HNG Internship and the opportunities it offers, visit here and here.

Top comments (0)