Automating Linux User Management with Bash Script
Tags: devops, linux, bash, aws
Published: 2024-07-03 11:25:14

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v51mwrb99sp0u1gceh4x.png)


**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](https://hng.tech/internship) and [here](https://hng.tech/premium).