Automating User and Group Management with a Bash Script.

RMAG news

Automating User and Group Management with a Bash Script

Managing user accounts and groups on a Linux system can be a repetitive and error-prone task, especially when dealing with a large number of users. Automating this process not only saves time but also ensures consistency and security. In this article, I’ll walk you through a Bash script that automates user and group management, including creating users, assigning them to groups, setting passwords, and logging all actions.

Prerequisites

Before diving into the script, ensure you have the following:

A Linux operating system (tested on Ubuntu).
Bash shell (/bin/bash).
OpenSSL for password generation (openssl).
Root privileges (sudo).

The Script

Below is the Bash script create_users.sh that handles user and group creation based on an input file formatted as username;group.

#!/bin/bash

# Check if the input file exists
if [ ! -f $1 ]; then
echo “Error: Input file not found.”
exit 1
fi

# Ensure log and secure directories are initialized once
LOG_FILE=“/var/log/user_management.log”
PASSWORD_FILE=“/var/secure/user_passwords.csv”

# Initialize log file
if [ ! -f $LOG_FILE ]; then
sudo touch $LOG_FILE
sudo chown root:root $LOG_FILE
fi

# Initialize password file
if [ ! -f $PASSWORD_FILE ]; then
sudo mkdir -p /var/secure
sudo touch $PASSWORD_FILE
sudo chown root:root $PASSWORD_FILE
sudo chmod 600 $PASSWORD_FILE
fi

# Redirect stdout and stderr to the log file
exec > >(sudo tee -a $LOG_FILE) 2>&1

# Function to check if user exists
user_exists() {
id $1 &>/dev/null
}

# Read each line from the input file
while IFS=‘;’ read -r username groups; do
# Trim whitespace
username=$(echo $username | tr -d ‘[:space:]’)

# Check if the user already exists
if user_exists $username; then
echo “User $username already exists.”
continue
fi

# Create user
sudo useradd -m $username

# Create personal group (same as username)
sudo groupadd $username

# Add user to personal group
sudo usermod -aG $username $username

# Create home directory
sudo mkdir -p “/home/$username
sudo chown $username:$username “/home/$username

# Generate random password
password=$(openssl rand -base64 12)

# Set password for user
echo $username:$password | sudo chpasswd

# Log actions
echo “User $username created. Password: $password

# Store passwords securely
echo $username,$password | sudo tee -a $PASSWORD_FILE

done < $1

How to Use the Script

Clone the Repository

First, clone the repository containing the script:

git clone <repository-url>
cd <repository-directory>

Create an Input File

Create a text file (e.g., user_list.txt) with each line formatted as username;group. Example:

alice;admin
bob;users
charlie;

Run the Script

Ensure the script is executable:

chmod +x create_users.sh

Execute with sudo (root privileges required):

sudo ./create_users.sh user_list.txt

View Logs and Passwords

Log file (/var/log/user_management.log):

sudo cat /var/log/user_management.log

Passwords file (/var/secure/user_passwords.csv):

sudo cat /var/secure/user_passwords.csv

Conclusion

Automating user and group management with a Bash script is a powerful way to streamline administrative tasks on Linux systems. By following the steps above, you can efficiently manage user accounts, ensuring they are set up correctly and securely.

If you’re interested in learning more about opportunities to enhance your programming skills and gain real-world experience, check out the HNG Internship. For companies looking to hire top tech talent, the HNG Hire platform offers a great way to connect with skilled developers. Additionally, explore the HNG Premium program for exclusive resources and benefits.

Happy coding!
Written by: Candy-DevOps