Simplify Linux User Management with Bash Scripting: A Step-by-Step Guide

Simplify Linux User Management with Bash Scripting: A Step-by-Step Guide

Introduction: Managing user accounts on a Linux system is a crucial task for system administrators. It involves creating, deleting, resetting passwords, and listing user accounts efficiently. In this blog post, we'll explore how to streamline these tasks using Bash scripting. We'll break down each block of code in our user_management.sh script to help beginners understand the process easily.

  1. Script Overview: Let's start by understanding the purpose of our user_management.sh script. This script provides a command-line interface for performing various user management tasks:
  • Creating a new user account

  • Deleting an existing user account

  • Resetting passwords for existing user accounts

  • Listing all user accounts on the system

#!/bin/bash

# Function to display usage information and available options
function display_usage {
    echo "Usage: $0 [OPTIONS]"
    echo "options:"
    echo "  -c, --create  Create a new user account."
    echo "  -d, --delete  Delete an existing user account."
    echo "  -r, --reset   Reset password for an existing user account."
    echo "  -l, --list    List all user accounts on the system."
    echo "  -h, --help    Display this help and exit."
}

  1. Creating a New User Account (create_user function): The create_user function guides the administrator through the process of creating a new user account. It prompts the administrator to enter a new username and password. After verifying that the username doesn't already exist, it uses the sudo useradd command to create the user account with the specified username and password.
# Function to create a new user account
function create_user {
    read -p "Enter the new username: " username

    # Check if the user name already exists
    if id "$username" &>/dev/null; then
        echo "Error: The username '$username' already exists. Please choose a different username."
        return 1
    fi

    # Prompt for password (Note: you might want to use 'read -s' to hide the password input)
    read -p "Enter the password for $username: " password

    # Create the user account using sudo
    if sudo useradd -m -p "$password" "$username"; then
        echo "User account '$username' created successfully."
    else
        echo "Error: Failed to create user account '$username'."
        return 1
    fi
}

  1. Deleting an Existing User Account (delete_user function): The delete_user function facilitates the deletion of an existing user account. It asks the administrator to enter the username of the account to be deleted. If the username exists, it utilizes sudo userdel -r to delete the user account along with their home directory and mailbox. Otherwise, it displays an error message indicating that the username doesn't exist.
# Function to delete an existing user account
function delete_user {
    read -p "Enter the username to delete: " username

    # Check if the username exists
    if id "$username" &>/dev/null; then
        if sudo userdel -r "$username" 2>/dev/null; then # Redirect stderr to /dev/null
            echo "User account '$username' deleted Successfully."
        else
            echo "Error: Failed to delete user account '$username'."
            return 1
        fi
    else
        echo "Error: The username '$username' does not exist. Please enter a valid username."
        return 1
    fi
}

  1. Resetting Password for an Existing User Account (reset_password function): The reset_password function enables the administrator to reset the password for an existing user account. It prompts the administrator to enter the username for which the password needs to be reset. After verifying the username's existence, it sets the new password using echo username:password | chpasswd.
# Function to reset the password for an existing user account
function reset_password {
    read -p "Enter the username to reset password: " username

    # Check if the username exists
    if id "$username" &>/dev/null; then
        # Prompt for password (Note: you might want to use 'read -s' to hide the password input)
        read -p "Enter the new password for $username: " password

        # Set the new password
        echo "$username:$password" | sudo chpasswd

        echo "Password for user '$username' reset successfully."
    else
        echo "Error: The username '$username' does not exist. Please enter a valid username."
    fi
}
  1. Listing All User Accounts (list_users function): The list_users function provides a simple way to view all user accounts present on the system. It reads the /etc/passwd file and formats the output to display a list of usernames along with their corresponding user IDs (UIDs).
# Function to list all user accounts on the system
function list_users {
    echo "User accounts on the system:"
    cat /etc/passwd | awk -F: '{print "- " $1 " (UID: " $3 ")"}'
}

Conclusion: In this comprehensive guide, we've broken down each block of code in our user_management.sh script to explain its purpose and functionality. By leveraging Bash scripting, system administrators can automate and simplify user management tasks on Linux systems. Feel free to customize the script according to your specific requirements and enhance your Linux user management experience. For the full code, including the user_management.sh script and any updates, please check my GitHub repository.

GitHub Repository Link:

https://github.com/swathipunreddy/Project-on-User-Mangement/blob/main/user_mangement.sh