DEV Community

Cover image for AUTOMATING USER CREATION AND MANAGEMENT WITH BASH USING REPLIT
B.G.Skillz 🧑‍💻
B.G.Skillz 🧑‍💻

Posted on

1

AUTOMATING USER CREATION AND MANAGEMENT WITH BASH USING REPLIT

What is the Purposes of this Task
Creating of User and adding them to group manually and setting the permission one by one, will take a lot time. Bash Script can do the task automatically. This is useful in companies.

Scenario for the Task
Assuming I am working with TechNova Corp, and the company recruit a new set of employees. The IT department is resposible for creating the Linux Accounts and setting up Access permissions. Instead of Typing commands for each user.

How the Script will work

  1. Creating Random Username: The script will help to create Username name randomly after the First_Name and the Last_Name has been declared and initialized with values
#!/bin/bash
# Define First and Last Names
First_Names=("Bolarinwa" "Ayinla" "Ayoade" "Ajibade" "Lawal" "Oseni" "Adeyemi" "Adeyinka" "Adeyinka")
Last_Names=("Gbolahan" "Moses" "Ayobami" "Mayowa" "Ademola" "Sodiq" "Opeyemi" "Adebayo" "Tunde")
Enter fullscreen mode Exit fullscreen mode

The base directory was also set BASE_DIR="/home/dept"
sudo mkdir -p "$BASE_DIR"

  1. Define User Groups: There are 6 department which include Departments=("Sales" "Marketing" "Finance" "HR" "IT" "Legal")

  2. Create Users and Assign Them to Groups

  • The useradd command was used to create new Linux Users from the name initialized above and also assign them to their department group
  1. Setting permission for Department Folders Each department have different folders
  • /dept/Sales

  • /dept/Marketing

  • /dept/Finance

  • /dept/HR

  • /dept/IT

  • /dept/Legal
    Users are only allow to access their department folder they cant access another Department folder
    chown and chmod was used for the access control

# Create department directories with appropriate permissions
for dept in "${Departments[@]}"; do
    sudo mkdir -p "$BASE_DIR/$dept"
    sudo chmod 770 "$BASE_DIR/$dept"  # 770 allows only the owner and group to access
    sudo chown root:root "$BASE_DIR/$dept"
done
Enter fullscreen mode Exit fullscreen mode
  1. Generating a Summary Report of all created users, their groups and access permissions

Image description

Below is the full script

#!/bin/bash

# Define First and Last Names
First_Names=("Bolarinwa" "Ayinla" "Ayoade" "Ajibade" "Lawal" "Oseni" "Adeyemi" "Adeyinka" "Adeyinka")
Last_Names=("Gbolahan" "Moses" "Ayobami" "Mayowa" "Ademola" "Sodiq" "Opeyemi" "Adebayo" "Tunde")

# Define Departments
Departments=("Sales" "Marketing" "Finance" "HR" "IT" "Legal")

# Set base directory (changed from /dept to /home/dept)
BASE_DIR="/home/dept"
sudo mkdir -p "$BASE_DIR"

# Create department directories with appropriate permissions
for dept in "${Departments[@]}"; do
    sudo mkdir -p "$BASE_DIR/$dept"
    sudo chmod 770 "$BASE_DIR/$dept"  # 770 allows only the owner and group to access
    sudo chown root:root "$BASE_DIR/$dept"
done

# Number of users to create
NUM_USERS=9

# Create a report file
REPORT_FILE="user_report.txt"
echo "Username | Group | Password" > "$REPORT_FILE"
echo "--------------------------" >> "$REPORT_FILE"

# Create users and assign to departments
for ((i=0; i<$NUM_USERS; i++)); do
    FIRST="${First_Names[$RANDOM % ${#First_Names[@]}]}"
    LAST="${Last_Names[$RANDOM % ${#Last_Names[@]}]}"
    USERNAME="$(echo "$FIRST.$LAST" | tr '[:upper:]' '[:lower:]')"

    PASSWORD=$(openssl rand -base64 8)
    DEPT="${Departments[$RANDOM % ${#Departments[@]}]}"

    # Ensure the department group exists
    sudo groupadd -f "$DEPT"

    # Create user with home directory
    sudo useradd -m -s /bin/bash "$USERNAME"

    # Set user password
    echo "$USERNAME:$PASSWORD" | sudo chpasswd

    # Assign user to department group
    sudo usermod -aG "$DEPT" "$USERNAME"

    # Set correct ownership and permissions for department folder
    sudo chown root:"$DEPT" "$BASE_DIR/$DEPT"
    sudo chmod 770 "$BASE_DIR/$DEPT"

    echo "$USERNAME | $DEPT | $PASSWORD" >> "$REPORT_FILE"
    echo "User $USERNAME created and assigned to $DEPT department"
done

echo "User creation complete. Check $REPORT_FILE for details."

Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay