DEV Community

Alan Varghese
Alan Varghese

Posted on

Automating User Management in Linux with Bash Scripts

As a DevOps engineer or system administrator, you often find yourself performing repetitive tasks. One of the most common is managing user accounts—especially when onboarding a new team or cleaning up after a project.

Manually running useradd for 20 people isn't just boring; it's prone to errors. That's why I built a simple User Management Automation tool using Bash.

In this post, I'll walk you through how these scripts work and how you can use them to streamline your workflow.


🚀 The Goal

The objective was to create a system that can:

  1. Read a list of usernames from a text file.
  2. Bulk create users with a default password and force a password change on first login.
  3. Bulk delete users and their home directories.
  4. Log every action for auditing purposes.

🛠️ The Scripts

1. The User List (users.txt)

Instead of hardcoding names, we use a simple text file. Just add one username per line:

dev1
dev2
ronald
Enter fullscreen mode Exit fullscreen mode

2. User Creation (create_users.sh)

This script handles the heavy lifting of onboarding. It checks if a user exists, creates them if they don't, sets a temporary password, and expires it immediately to ensure security.

#!/bin/bash

USER_FILE="users.txt"
PASSWORD="DevOps@1234!"
LOG_FILE="user_creation.log"

echo "User Creation Started: $(date)" >> $LOG_FILE

while read USERNAME
do
    if id "$USERNAME" &>/dev/null
    then
        echo "User $USERNAME already exists" | tee -a $LOG_FILE
    else 
        sudo useradd -m $USERNAME
        echo "$USERNAME:$PASSWORD" | sudo chpasswd
        sudo passwd -e $USERNAME
        echo "User $USERNAME created succesfully" | tee -a $LOG_FILE
    fi
done < $USER_FILE

echo "User Creation Completed: $(date)" >> $LOG_FILE
Enter fullscreen mode Exit fullscreen mode

Key Features:

  • useradd -m: Creates the home directory automatically.
  • chpasswd: Efficiently sets passwords from a string.
  • passwd -e: Forces the user to change their password at the first login—a crucial security step!

3. User Deletion (del_user.sh)

When it's time to offboard, this script makes it a one-command job.

#!/bin/bash

USER_LIST="users.txt"
LOG_FILE="user_deletion.log"

echo "User Deletion Started: $(date)" >> $LOG_FILE

while read USERNAME
do 
    if id "$USERNAME" &>/dev/null
    then
        sudo userdel -r "$USERNAME"
        echo "User $USERNAME Deleted Successfully" | tee -a $LOG_FILE
    else
        echo "User $USERNAME does not exist" | tee -a $LOG_FILE
    fi
done < $USER_LIST

echo "User Deletion Completed: $(date)" >> $LOG_FILE
Enter fullscreen mode Exit fullscreen mode

Key Features:

  • userdel -r: Removes the user and their home directory, keeping the system clean.
  • Error Handling: Checks if the user exists before trying to delete them.

📈 Logging for Auditing

Both scripts generate log files (user_creation.log and user_deletion.log). This is essential for tracking who was created and when, which is a standard requirement in production environments.

💡 How to Use It

  1. Clone the repository.
  2. Populate users.txt with your desired usernames.
  3. Make the scripts executable: chmod +x *.sh
  4. Run ./create_users.sh to onboard or ./del_user.sh to offboard.

🔒 Security Note

For this demonstration, the password is hardcoded. In a real-world production scenario, you should consider:

  • Using an environment variable for the default password.
  • Using a secret management tool.
  • Prompting for a password during script execution.

🏁 Conclusion

Bash scripting is a superpower for any Linux user. With just a few lines of code, we turned a tedious manual process into a reliable, logged, and automated workflow.

How do you handle user management in your environment? Let me know in the comments!


Check out the full project on my GitHub

Top comments (0)