DEV Community

Cover image for BashBlaze Day 3: Tackling User Management with Bash (And a Few Laughs!)
Muhammad Hanzala Ali
Muhammad Hanzala Ali

Posted on

BashBlaze Day 3: Tackling User Management with Bash (And a Few Laughs!)

Day 3 of my Bash Blaze challenge has arrived, and it’s packed with user management commands. It was all fun and games until I realized I was the Bash overlord controlling accounts on my system. Here’s how I conquered this challenge and wrote a script so slick that even my cat looked impressed (or maybe it was just yawning).

Challenge Overview: User Management Bash Script

The task: create a Bash script that handles user accounts like a pro—or at least like a DevOps engineer trying to impress HR.😀

The Game Plan:

  • Account Creation: Add users and make sure they’re not already on the list (because cloning is creepy).
  • Account Deletion: Remove users (with their home directories for bonus cleanup points).
  • Password Reset: Reset passwords while pretending I’m a hacker in a movie montage.
  • User Listing: Display all users like it’s a roll call at school.
  • Help Command: Because every good script should offer unsolicited advice.

The Code That Makes the Magic Happen

#!/bin/bash

<<note
Usage: ./user_management.sh [Options]
note

# Displaying usage information like a helpful Bash superhero
function display_usage {
        echo "Usage: ./user_management.sh [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."
}

# Creating a new user account (because why not play God?)
function create_user {
        read -p "Enter the new username: " new_user

        if id "$new_user" &>/dev/null; then
                echo "Error: The username '$new_user' already exists. Maybe try 'user2.0'?"
                exit 1
        else
                read -sp "Enter password for $new_user: " password
                sudo useradd -m -p "$(openssl passwd -1 "$password")" "$new_user"
                echo -e "\nUser '$new_user' created successfully. They owe you big time."
        fi
}

# Deleting a user account (peace out, user!)
function delete_user {
        read -p "Enter the username to delete: " username

        if id "$username" &>/dev/null; then
                sudo userdel -r "$username"
                echo "User '$username' deleted. Gone, but not forgotten (unless you use rm -rf)."
        else
                echo "Error: The username '$username' does not exist. Did they ghost us?"
        fi
}

# Resetting passwords (because 'password123' is so last season)
function reset_passwd {
        read -p "Enter the username to reset password for: " reset_user

        if id "$reset_user" &>/dev/null; then
                read -sp "Enter new password for '$reset_user': " password
                echo "$reset_user:$password" | sudo chpasswd
                echo -e "\nPassword for '$reset_user' reset. Use wisely, young Padawan."
        else
                echo "Error: '$reset_user' not found. Check your spelling or your luck."
        fi
}

# Listing user accounts (because we're nosy)
function list_users {
        echo "Here are the current VIPs (Very Important Password-holders):"
        awk -F: '{ print "- " $1 " (UID: " $3 ")" }' /etc/passwd
}

# Script begins here
if [ $# -eq 0 ] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
        display_usage
        exit 0
fi

while [ $# -gt 0 ]; do
        case "$1" in
                -c | --create)
                        create_user
                        ;;
                -d | --delete)
                        delete_user
                        ;;
                -r | --reset)
                        reset_passwd
                        ;;
                -l | --list)
                        list_users
                        ;;
                *)
                        echo "Error: Invalid option '$1'. Try '--help' for better luck."
                        exit 1
                        ;;
        esac
        shift
done

Enter fullscreen mode Exit fullscreen mode

Key Bash Commands for User Management

1. Creating Users
sudo useradd -m username
Explanation: The -m flag ensures a home directory is created for the new user. This simple command is a must for adding users quickly.

2. Setting Passwords
sudo passwd username
This command lets you set or change a password for a user, essential for maintaining secure access.

3. Deleting Users
sudo userdel -r username
The -r flag removes the user's home directory along with their profile, ensuring a complete cleanup.

4. Adding Users to Groups
sudo usermod -aG groupname username
The -aG option appends the user to an existing group without removing them from other groups.

Pro Tip:

Always double-check user permissions with:

groups username
This helps you verify that the user is part of the correct groups and has the appropriate access.

Challenges Faced and How I Overcame Them

During Day 3, I encountered scenarios where user permissions needed to be fine-tuned for specific scripts. Here’s how I tackled these challenges:

  1. Error: User already exists
    Solution: Use id username to check user existence before creation.

  2. Permissions Denied
    Solution: Apply chmod and chown commands to fix permissions on directories.

Real-World Application

User management is not just theoretical; it plays a significant role in CI/CD pipelines, ensuring only authorized users can deploy code or make changes. Integrating these Bash skills into your daily tasks can simplify user access management and enhance security protocols.

Conclusion

Day 3 of my Bash Blaze challenge was both insightful and practical. Learning these user management commands has empowered me to be more confident in my DevOps journey. As I move on to Day 4, which promises even more exciting concepts, I invite you to follow along and join me in mastering Bash scripting.

Top comments (0)