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.
Top comments (0)