Simplifying User Management on Linux with a Bash Script

Managing user accounts and groups on a Linux system is a fundamental task for system administrators. To streamline this process, I've developed a user management script in Bash, providing a simple yet powerful tool for efficiently handling user-related operations. In this article, we'll explore the features, usage, and benefits of this script.
The Need for an Efficient User Management Solution
As systems grow and more users are added, handling user accounts becomes increasingly complex. A robust user management system is essential for maintaining security, ensuring proper access controls, and facilitating the overall administration of a Linux environment. The usermanagement script addresses these needs by offering a set of intuitive and versatile options.
#!/bin/bash
# User Account Management Script
# Function to display usage information
display_usage() {
echo "Usage: $0 [options]"echo "Options:"echo " -c, --create Create a new user account."echo " -cg, --create_group Create a new group."echo " -lg, --list_group List all users on the group."echo " -mg, --modify_group Add multiple users in a group."echo " -d, --delete Delete an existing user account."echo " -r, --reset Reset the password of an existing user account."echo " -m, --modify Modify a user group."echo " -l, --list List all user accounts on the system."echo " -h, --help Display this help message."
}
# Function to create a new user account
create_user() {
read -p "Enter the new username: " username
# Check if the username already existsif id "$username" &>/dev/null; thenecho "Error: Username '$username' already exists. Please choose a different username."exit 1
fi
# Prompt for passwordread -s -p "Enter the password: " password
echo -e "\n"
# Create the user
sudo useradd -m -p "$(openssl passwd -1 "$password")" "$username"
echo "Success: User '$username' created."
}
# Function to modify a user account
modify_user() {
read -p "Enter the group name: " group
read -p "Enter the new username: " username
# Check if the username already existsif id "$username" &>/dev/null; then# Check if the user is already in the specified groupif groups "$username" | grep -q "\<$group\>"; thenecho "Error: User '$username' is already in the '$group' group."exit 1
fielseecho "Error: User '$username' does not exist."exit 1
fi
# Update the user group
sudo usermod -aG "$group" "$username"
echo "Success: User '$username' added to the '$group' group."
}
# Function to delete an existing user account
delete_user() {
read -p "Enter the username to delete: " username
# Check if the username existsif id "$username" &>/dev/null; then
sudo userdel -r "$username"echo "Success: User '$username' deleted."elseecho "Error: Username '$username' does not exist."exit 1
fi
}
# Function to reset the password of an existing user account
reset_password() {
read -p "Enter the username to reset password: " username
# Check if the username existsif id "$username" &>/dev/null; thenread -s -p "Enter the new password: " new_password
echo -e "\n"
# Reset the password
sudo usermod -p "$(openssl passwd -1 "$new_password")" "$username"
echo "Success: Password for user '$username' reset."elseecho "Error: Username '$username' does not exist."exit 1
fi
}
# Function to list all user accounts on the system
list_users() {
echo "List of User Accounts:"cut -d: -f1,3 /etc/passwd | column -t
}
# Function to create a new group
create_group() {
read -p "Enter the group name: " group_name
# Check if the group already existsif grep -q "^$group_name:" /etc/group; thenecho "Error: Group '$group_name' already exists."else
sudo groupadd "$group_name"echo "Success: Group '$group_name' created."fi
}
# Function to add users to a group
add_users_to_group() {
read -p "Enter the group name: " group_name
read -p "Enter the comma-separated list of usernames to add to the group: " user_list
# Check if the group existsif grep -q "^$group_name:" /etc/group; then
IFS=',' read -ra users <<<"$user_list"for user in "${users[@]}"; do# Check if the user existsif id "$user" &>/dev/null; then
sudo usermod -aG "$group_name" "$user"echo "Success: User '$user' added to the group '$group_name'."elseecho "Error: User '$user' does not exist."fidoneelseecho "Error: Group '$group_name' does not exist."fi
}
# Function to list users in a group
list_users_in_group() {
read -p "Enter the group name: " group_name
# Check if the group existsif grep -q "^$group_name:" /etc/group; thenusers=$(getent group "$group_name" | cut -d: -f4)
if [ -z "$users" ]; thenecho "No users in the group '$group_name'."elseecho "Users in the group '$group_name': $users"fielseecho "Error: Group '$group_name' does not exist."fi
}
# Check if there are no arguments provided
if [ $# -eq 0 ]; then
display_usage
exit 1
fi
# Parse command-line options
while [ "$#" -gt 0 ]; docase "$1" in
-c | --create)
create_user
;;
-d | --delete)
delete_user
;;
-r | --reset)
reset_password
;;
-m | --modify)
modify_user
;;
-cg | --create_group)
create_group
;;
-mg | --modify_group)
add_users_to_group
;;
-lg | --list_group)
list_users_in_group
;;
-l | --list)
list_users
;;
-h | --help)
display_usage
exit 0
;;
*)
echo "Error: Unknown option '$1'."
display_usage
exit 1
;;
esacshift
done
exit 0
Features of the User Management Script
1. Creating User Accounts
Creating a new user account is a common administrative task. The script prompts the administrator to enter a new username and password, ensuring the account is securely set up.
./usermanagement.sh -c
2. Creating and Managing Groups
The script allows the creation of new groups, essential for organizing users efficiently. Additionally, it facilitates the addition of multiple users to a specific group, streamlining group management.
./usermanagement.sh -cg # Create a new group ./usermanagement.sh -mg # Add users to a group ./usermanagement.sh -lg # List users in a group
3. Deleting User Accounts
When a user account is no longer needed, the script simplifies the process of deleting it. The administrator is prompted to enter the username to be deleted.
./usermanagement.sh -d
4. Resetting User Passwords
Password resets are straightforward with the script. It prompts for the username and the new password, ensuring a secure and efficient password change.
./usermanagement.sh -r
5. Modifying User Groups
Modifying a user's group membership is made easy. The script prompts for the group name and the username, ensuring users are efficiently managed across different groups.
./usermanagement.sh -m
6. Listing User Accounts
The script provides a simple way to list all user accounts on the system, aiding administrators in gaining an overview of the existing users.
./usermanagement.sh -l
Simplifying Administrative Tasks
One of the primary advantages of the usermanagement script is its ability to simplify routine administrative tasks. With clear prompts and informative messages, even users with minimal Linux experience can confidently manage user accounts and groups.
How to Use the User Management Script
The script is designed to be user-friendly, with a simple syntax and clear options. The help option provides a quick reference guide:
./usermanagement.sh -h
Conclusion
Efficient user management is crucial for maintaining a secure and organized Linux environment. The usermanagement script simplifies this process, offering a reliable and user-friendly solution for administrators. Whether creating user accounts, managing groups, or performing other routine tasks, this script provides a versatile tool to streamline Linux system administration.
Feel free to customize and extend the script based on your specific requirements. Download the usermanagement script and start simplifying your user management tasks today.

