Shell Script  Task

Shell Script Task

Β·

5 min read

🐧 Shell Script to Create User Accounts with -c or --create Option πŸ› οΈ

Are you tired of manually creating user accounts on your Linux system? πŸ€” Do you want a quick and easy way to automate user creation with a simple script? Look no further! In this blog, I’ll walk you through a shell script that allows you to create new user accounts with just one command. Let’s dive in! πŸš€

Week 3 Challenge 1: User Account Management

In this challenge, you will create a bash script that provides options for managing user accounts on the system. The script should allow users to perform various user account-related tasks based on command-line arguments.

  • Part 1: Account Creation

  1. Implement an option -c or --create that allows the script to create a new user account. The script should prompt the user to enter the new username and password.

  2. Ensure that the script checks whether the username is available before creating the account. If the username already exists, display an appropriate message and exit gracefully.

  3. After creating the account, display a success message with the newly created username.

πŸ’‘ Key Features

  • User-Friendly Prompts: The script guides the user step-by-step.

  • Username Availability Check: Prevents duplicate usernames.

  • Secure Password Input: Passwords are hidden during input.

  • Graceful Error Handling: Displays clear error messages and exits gracefully.


πŸ›‘ Important Notes

  • Privileges Required: The script uses sudo to run useradd, so you need root or sudo privileges for run command

  • Password Hashing: The script uses openssl passwd -1 for password hashing. For production environments, consider using more secure hashing methods. in realtime you canuse this method

  • Linux-Specific: This script is designed for Linux systems. Adjustments may be needed for other Unix-like systems.

Part 2: Account Deletion

  1. /Implement an option -d or --delete that allows the script to delete an existing user account. The script should prompt the user to enter the username of the account to be deleted.

  2. Ensure that the script checks whether the username exists before attempting to delete the account. If the username does not exist, display an appropriate message and exit gracefully.

  3. After successfully deleting the account, display a confirmation message with the deleted username.

Part 3: Password Reset

  1. Implement an option -r or --reset that allows the script to reset the password of an existing user account. The script should prompt the user to enter the username and the new password.

  2. Ensure that the script checks whether the username exists before attempting to reset the password. If the username does not exist, display an appropriate message and exit gracefully.

  3. After resetting the password, display a success message with the username and the updated password.

πŸ’‘ Key Features

  • User-Friendly Prompts: The script guides the user step-by-step.

  • Username Existence Check: Ensures the account exists before attempting to reset the password.

  • Secure Password Input: Passwords are hidden during input.

  • Graceful Error Handling: Displays clear error messages and exits gracefully.


πŸ›‘ Important Notes

  • Privileges Required: The script uses sudo to run chpasswd, so you required root or sudo privileges.

  • Password Security: Ensure that the new password follows your system's password policy.

  • Linux-Specific: This script is designed for Linux systems. Adjustments may be needed for other Unix-like systems.

Part 4: List User Accounts

  1. Implement an option -l or --list that allows the script to list all user accounts on the system. The script should display the usernames and their corresponding user IDs (UID).

  • Submission Instructions

    Create a bash script named user_management.sh that implements the User Account Management as described in the challenge.

    Add comments in the script to explain the purpose and logic of each part.

user_managemet script

#!/bin/bash

# Function to create a new user
create_user() {
    read -p "Enter new username: " username

    if id "$username" &>/dev/null; then
        echo "Error: The username '$username' already exists. Please choose a different username."
        exit 1
    fi

    read -s -p "Enter new password: " password
    echo

    sudo useradd -m -p $(openssl passwd -1 "$password") "$username"

    if [ $? -eq 0 ]; then
        echo "Success: The user '$username' has been created."
    else
        echo "Error: Failed to create the user '$username'."
        exit 1
    fi
}

# Function to delete a user account
delete_user() {
    read -p "Enter the username to delete: " username

    if ! id "$username" &>/dev/null; then
        echo "Error: The username '$username' does not exist. Please check the username and try again."
        exit 1
    fi

    sudo userdel -r "$username"

    if [ $? -eq 0 ]; then
        echo "Success: The user '$username' has been deleted."
    else
        echo "Error: Failed to delete the user '$username'."
        exit 1
    fi
}

# Function to reset a user's password
reset_password() {
    read -p "Enter the username to reset password: " username

    if ! id "$username" &>/dev/null; then
        echo "Error: The username '$username' does not exist. Please check the username and try again."
        exit 1
    fi

    read -s -p "Enter the new password: " new_password
    echo

    echo "$username:$new_password" | sudo chpasswd

    if [ $? -eq 0 ]; then
        echo "Success: The password for user '$username' has been reset."
    else
        echo "Error: Failed to reset the password for user '$username'."
        exit 1
    fi
}

# Function to list all user accounts
list_users() {
    echo "Listing all user accounts on the system:"
    echo "--------------------------------------"
    getent passwd | awk -F: '{print "Username:", $1, "| UID:", $3}'
}

# Main script logic
if [[ $1 == "-c" || $1 == "--create" ]]; then
    create_user
elif [[ $1 == "-d" || $1 == "--delete" ]]; then
    delete_user
elif [[ $1 == "-r" || $1 == "--reset" ]]; then
    reset_password
elif [[ $1 == "-l" || $1 == "--list" ]]; then
    list_users
else
    echo "Usage: $0 -c|--create | -d|--delete | -r|--reset | -l|--list"
    exit 1
fi

Β