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 u…


This content originally appeared on DEV Community and was authored by Nnamdi Nwosu Kenneth

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

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
}

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

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"

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"

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

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."

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.


This content originally appeared on DEV Community and was authored by Nnamdi Nwosu Kenneth


Print Share Comment Cite Upload Translate Updates
APA

Nnamdi Nwosu Kenneth | Sciencx (2024-07-03T11:25:14+00:00) Automating Linux User Management with Bash Script. Retrieved from https://www.scien.cx/2024/07/03/automating-linux-user-management-with-bash-script/

MLA
" » Automating Linux User Management with Bash Script." Nnamdi Nwosu Kenneth | Sciencx - Wednesday July 3, 2024, https://www.scien.cx/2024/07/03/automating-linux-user-management-with-bash-script/
HARVARD
Nnamdi Nwosu Kenneth | Sciencx Wednesday July 3, 2024 » Automating Linux User Management with Bash Script., viewed ,<https://www.scien.cx/2024/07/03/automating-linux-user-management-with-bash-script/>
VANCOUVER
Nnamdi Nwosu Kenneth | Sciencx - » Automating Linux User Management with Bash Script. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/03/automating-linux-user-management-with-bash-script/
CHICAGO
" » Automating Linux User Management with Bash Script." Nnamdi Nwosu Kenneth | Sciencx - Accessed . https://www.scien.cx/2024/07/03/automating-linux-user-management-with-bash-script/
IEEE
" » Automating Linux User Management with Bash Script." Nnamdi Nwosu Kenneth | Sciencx [Online]. Available: https://www.scien.cx/2024/07/03/automating-linux-user-management-with-bash-script/. [Accessed: ]
rf:citation
» Automating Linux User Management with Bash Script | Nnamdi Nwosu Kenneth | Sciencx | https://www.scien.cx/2024/07/03/automating-linux-user-management-with-bash-script/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.