0% found this document useful (0 votes)
2 views

Linux

The document provides an overview of managing users and groups in Linux systems using shell scripting, highlighting key commands such as useradd, userdel, and groupadd. It includes a sample script for adding and removing users and groups, along with best practices for input validation and security. The conclusion emphasizes the efficiency and consistency that scripting brings to user and group management tasks.

Uploaded by

Raghuraman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Linux

The document provides an overview of managing users and groups in Linux systems using shell scripting, highlighting key commands such as useradd, userdel, and groupadd. It includes a sample script for adding and removing users and groups, along with best practices for input validation and security. The conclusion emphasizes the efficiency and consistency that scripting brings to user and group management tasks.

Uploaded by

Raghuraman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

🧑‍🤝‍🧑 Adding and Removing Users and Groups in Shell Scripting

🧾 1. Introduction
In Linux systems, users and groups are fundamental to managing access control,
permissions, and security. Shell scripting can automate user and group
management, making it efficient for system administrators to handle multiple tasks
at once.

🛠️ 2. Key Commands
Command Purpose
useradd Add a new user
userdel Delete a user
usermod Modify a user account
groupadd Add a new group
groupdel Delete a group
gpasswd -a Add user to group
gpasswd -d Remove user from group
passwd Set user password (manual or with input)

📂 3. Script Example: Add/Remove Users and Groups


#!/bin/bash

# Check if the script is run as root


if [ "$EUID" -ne 0 ]; then
echo " Please run as root."
exit 1
fi

echo "User and Group Management Script"


echo "---------------------------------"
echo "1. Add User"
echo "2. Delete User"
echo "3. Add Group"
echo "4. Delete Group"
echo "5. Add User to Group"
echo "6. Remove User from Group"
read -p "Choose an option [1-6]: " OPTION

case $OPTION in
1)
read -p "Enter username to add: " USERNAME
useradd "$USERNAME"
echo " User '$USERNAME' added."
;;
2)
read -p "Enter username to delete: " USERNAME
userdel -r "$USERNAME"
echo " User '$USERNAME' deleted."
;;
3)
read -p "Enter group name to add: " GROUPNAME
groupadd "$GROUPNAME"
echo " Group '$GROUPNAME' added."
;;
4)
read -p "Enter group name to delete: " GROUPNAME
groupdel "$GROUPNAME"
echo " Group '$GROUPNAME' deleted."
;;
5)
read -p "Enter username: " USERNAME
read -p "Enter group name: " GROUPNAME
gpasswd -a "$USERNAME" "$GROUPNAME"
echo " User '$USERNAME' added to group '$GROUPNAME'."
;;
6)
read -p "Enter username: " USERNAME
read -p "Enter group name: " GROUPNAME
gpasswd -d "$USERNAME" "$GROUPNAME"
echo " User '$USERNAME' removed from group '$GROUPNAME'."
;;
*)
echo " Invalid option"
;;
esac
🔍 4. Explanation of the Script
• Uses useradd, userdel, groupadd, groupdel, gpasswd for user/group tasks.
• read gathers user input.
• case statement handles menu logic.
• Ensures script is run with root privileges ($EUID check).
• Uses -r flag with userdel to also delete the home directory.

📌 5. Use Case: Automatically Create a User with Group and Set


Password
#!/bin/bash

USERNAME="student1"
GROUPNAME="students"
PASSWORD="Welcome@123"

# Create group if it doesn't exist


if ! grep -q "^$GROUPNAME:" /etc/group; then
groupadd "$GROUPNAME"
fi

# Create user and assign to group


useradd -m -G "$GROUPNAME" "$USERNAME"
echo "$USERNAME:$PASSWORD" | chpasswd

echo " User '$USERNAME' created with group '$GROUPNAME'."

This is useful for setting up lab environments or bulk onboarding.

✅ 6. Best Practices
• Always validate input to avoid accidental deletions.
• Use -m with useradd to create home directories.
• Store passwords securely and consider encrypting scripts.
• Log changes to a file for audit purposes.
🧠 7. Real-World Applications
Scenario Benefit of Scripting
School/college setup Add multiple student users and assign groups
Company onboarding Create users with passwords, assign roles
Cleanup scripts Automatically delete users who leave
Group permission setup Assign dev, test, admin access by group

🏁 8. Conclusion
Managing users and groups through shell scripts provides a powerful, efficient, and
repeatable way of administering Linux systems. Whether it’s creating a single user
or deploying hundreds, scripting reduces human error and ensures consistency.

You might also like