User Account Management Using shell Scripting


Task 1: Account Creation
Steps:
Check if the username already exists before creating the account.
Prompt for username and password securely.
Create the new account and set the password.
Confirm successful account creation.
Execution:
#!/bin/bash
# Function to create a user account
create_user() {
read -p "Enter new username: " username
if id "$username" &>/dev/null; then
echo "Error: User '$username' already exists."
exit 1
fi
read -s -p "Enter password for $username: " password
echo
sudo useradd "$username"
echo "$username:$password" | sudo chpasswd
echo "User '$username' created successfully."
}
create_user
Explanation:
id "$username"
checks if the user already exists.read -s -p
ensures password entry is hidden.useradd
creates the account.chpasswd
sets the password.Prints success confirmation.
Task 2: Account Deletion
Steps:
Check if the username exists before deletion.
Prompt for username to be deleted.
Delete the account and confirm.
Execution:
delete_user() {
read -p "Enter username to delete: " username
if ! id "$username" &>/dev/null; then
echo "Error: User '$username' does not exist."
exit 1
fi
sudo userdel -r "$username"
echo "User '$username' deleted successfully."
}
delete_user
Explanation:
id "$username"
ensures the user exists before deletion.userdel -r
removes the user along with their home directory.Prints a success message.
Task 3: Password Reset
Steps:
Check if the username exists before resetting password.
Prompt for username and new password.
Reset the password securely.
Confirm password change.
Execution:
reset_password() {
read -p "Enter username to reset password: " username
if ! id "$username" &>/dev/null; then
echo "Error: User '$username' does not exist."
exit 1
fi
read -s -p "Enter new password: " password
echo
echo "$username:$password" | sudo chpasswd
echo "Password for '$username' reset successfully."
}
reset_password
Explanation:
Ensures the account exists before modifying credentials.
Uses
chpasswd
to securely reset passwords.Prints confirmation.
Task 4: List User Accounts
Steps:
Fetch all system users and their corresponding UIDs.
Format the output for readability.
Execution:
list_users() {
echo "User Accounts on the System:"
cut -d: -f1,3 /etc/passwd | column -t -s:
}
list_users
Explanation:
Extracts usernames and their UIDs from
/etc/passwd
.Formats output neatly using
column -t -s:
.
Task 5: Help and Usage
Steps:
- Display available command options and their usage.
Execution:
usage() {
echo "Usage: user_management.sh [OPTION]"
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"
echo " -h, --help Display usage information"
}
usage
Explanation:
Prints all available command-line options.
Ensures users understand script functionality.
Final Implementation: Running the Full Script
Now, let's package everything into one script (user_
management.sh
) with proper argument handling:
#!/bin/bash
# Function to display usage
usage() {
echo "Usage: $0 [-c|--create] [-d|--delete] [-r|--reset] [-l|--list] [-h|--help]"
}
create_user() {
read -p "Enter new username: " username
if id "$username" &>/dev/null; then
echo "Error: User '$username' already exists."
exit 1
fi
read -s -p "Enter password for $username: " password
echo
sudo useradd "$username"
echo "$username:$password" | sudo chpasswd
echo "User '$username' created successfully."
}
delete_user() {
read -p "Enter username to delete: " username
if ! id "$username" &>/dev/null; then
echo "Error: User '$username' does not exist."
exit 1
fi
sudo userdel -r "$username"
echo "User '$username' deleted successfully."
}
reset_password() {
read -p "Enter username to reset password: " username
if ! id "$username" &>/dev/null; then
echo "Error: User '$username' does not exist."
exit 1
fi
read -s -p "Enter new password: " password
echo
echo "$username:$password" | sudo chpasswd
echo "Password for '$username' reset successfully."
}
list_users() {
echo "User Accounts on the System:"
cut -d: -f1,3 /etc/passwd | column -t -s:
}
case "$1" in
-c|--create) create_user ;;
-d|--delete) delete_user ;;
-r|--reset) reset_password ;;
-l|--list) list_users ;;
-h|--help) usage ;;
*) echo "Invalid option! Use -h or --help for usage."; exit 1 ;;
esac
How to Use the Script
Create a User →
./user_
management.sh
-c
Delete a User →
./user_
management.sh
-d
Reset a Password →
./user_
management.sh
-r
List Users →
./user_
management.sh
-l
Show Help →
./user_
management.sh
-h
This script follows DevOps best practices, ensuring security, error handling, and user-friendly execution.
Subscribe to my newsletter
Read articles from Mridul Pandey directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
