Want to really understand how Linux handles users and groups? This hands-on guide not only gives you commands — it breaks them down flag by flag, so you know exactly what you're doing.
Grab your terminal and follow along. Ready? Let’s get started. 🚀
📁 Important Files — Know What Powers User Management
/etc/passwd # User info: username:x:UID:GID:comment:home:shell
/etc/shadow # Has (hashed) passwords
/etc/group # Lists all groups
💡 Try:
cat /etc/passwd | head -n 3
sudo cat /etc/shadow | head -n 3
cat /etc/group | head -n 3
🛠️ Create a User with useradd
useradd [OPTIONS] username
🔑 Common Flags:
-m → Create a home directory (e.g., /home/username)
-d /custom/path → Set a custom home directory
-s /bin/bash → Set user’s login shell
-c "comment" → Add a comment (e.g., full name or role)
-G group1,group2 → Add to secondary groups (must already exist)
-g group → Set primary group (must already exist)
📌 Practical Example:
sudo useradd -m -d /home/john -c "C++ Developer" -s /bin/bash -G sudo,adm john
Explanation:
-m: Create /home/john
-d /home/john: Use this as the home directory
-c "C++ Developer": Store this comment
-s /bin/bash: Set Bash as login shell
-G sudo,adm: Add to secondary groups
➡️ Now set a password:
sudo passwd john
🔄 Modify a User with usermod
usermod [OPTIONS] username
Useful Flags:
-aG group1,group2 → Append user to new secondary groups
-s /new/shell → Change default shell
-d /new/home → Change home dir (-m to move files)
-c → Change comment
🔧 Example:
sudo usermod -aG docker,developers john
Explanation:
-a: Append (don’t remove existing groups!)
-G: Add to docker and developers groups
❌ Delete a User with userdel
sudo userdel [OPTIONS] username
Flags:
- -r → Remove user’s home directory and mail spool Example:
sudo userdel -r john
Result: Deletes john + /home/john
👥 Group Management with groupadd, groupdel
➕ Add a Group:
sudo groupadd devops
❌ Delete a Group:
sudo groupdel devops
🔍 View All Groups:
cat /etc/group
🔍 Check Group Membership
groups # For current user
groups username # For another user
id username # UID, GID, group list
🧑💼 Make a User Admin (Sudo Access)
On Ubuntu:
sudo usermod -aG sudo john
On CentOS:
sudo usermod -aG wheel john
🕵️ Monitor Logged-In Users
who -H # Show who is logged in
w # Who is logged in + what they’re doing
uptime # Show load, users, system uptime
id # Show UID, GID, and groups
whoami # Show your effective user ID
📜 Login History
last # Recent logins
last -u username # Logins by a specific user
✍️ Final Words
Linux user & group management is must-know stuff for sysadmins, DevOps engineers, and anyone using Linux daily.