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
- 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")
The base directory was also set BASE_DIR="/home/dept"
sudo mkdir -p "$BASE_DIR"
Define User Groups: There are 6 department which include
Departments=("Sales" "Marketing" "Finance" "HR" "IT" "Legal")
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
- 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
andchmod
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
- Generating a Summary Report of all created users, their groups and access permissions
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."
Top comments (0)