Lesson 8: User & Group Management
Goal: Learn to create, modify, and delete users and groups on a Linux system.
Table of Contents
Current User Info
# Who am I?
whoami
# Full user ID info
id
# Example output:
# uid=1000(benjamin) gid=1000(benjamin) groups=1000(benjamin),27(sudo),999(docker)
# Which groups do I belong to?
groups
# Info about another user
id alice
groups alice
# Currently logged-in users
who
w
User Management
Create a user
# Create user with home directory
sudo useradd -m alice
# Create user with home dir, default shell, and comment
sudo useradd -m -s /bin/bash -c "Alice Smith" alice
# Create user and set password immediately
sudo useradd -m -s /bin/bash alice && sudo passwd alice
Flags:
| Flag | Meaning |
|---|---|
-m |
Create home directory |
-s /bin/bash |
Set login shell |
-c "Full Name" |
Add comment (usually full name) |
-G sudo,docker |
Add to supplementary groups |
-d /custom/home |
Custom home directory path |
-e 2026-12-31 |
Account expiry date |
Modify a user
# Change the user's shell
sudo usermod -s /bin/zsh alice
# Add user to a group (append — IMPORTANT: use -a)
sudo usermod -aG docker alice
sudo usermod -aG sudo alice
# Change username
sudo usermod -l alice-new alice
# Lock a user account
sudo usermod -L alice
# Unlock a user account
sudo usermod -U alice
# Change home directory
sudo usermod -d /new/home -m alice
usermod -G (without -a) replaces all groups. Always use -aG to append.
Delete a user
# Delete user (keep home directory)
sudo userdel alice
# Delete user AND their home directory
sudo userdel -r alice
Switch users
# Switch to another user
su - alice
# Run a single command as another user
su - alice -c "whoami"
# Switch to root
sudo -i
# or
su -
Password Management
# Set or change password for a user
sudo passwd alice
# Change your own password
passwd
# Force password change on next login
sudo passwd -e alice
# Lock/unlock password
sudo passwd -l alice # lock
sudo passwd -u alice # unlock
# Check password status
sudo passwd -S alice
# Output: alice P 03/22/2026 0 99999 7 -1
# P = has password, L = locked, NP = no password
# Set password expiry policy
sudo chage -M 90 alice # max 90 days
sudo chage -m 7 alice # min 7 days between changes
sudo chage -W 14 alice # warn 14 days before expiry
sudo chage -l alice # list policy
Group Management
Create a group
sudo groupadd developers
sudo groupadd -g 1500 webteam # with specific GID
Add users to groups
# Add existing user to a group
sudo usermod -aG developers alice
sudo usermod -aG developers bob
# Verify
groups alice
# Output: alice : alice developers
Remove user from a group
sudo gpasswd -d alice developers
Delete a group
sudo groupdel developers
List all groups
# All groups on the system
cat /etc/group
# Just group names
cut -d: -f1 /etc/group
# Groups with their members
getent group developers
# Output: developers:x:1001:alice,bob
Practical: Shared project directory
# 1. Create a group
sudo groupadd project-x
# 2. Add users to the group
sudo usermod -aG project-x alice
sudo usermod -aG project-x bob
# 3. Create shared directory
sudo mkdir /opt/project-x
# 4. Set ownership and permissions
sudo chown root:project-x /opt/project-x
sudo chmod 2775 /opt/project-x
# 2 = SetGID (new files inherit group)
# 775 = owner+group: full, others: read+execute
# 5. Now alice and bob can both create files here,
# and all files will belong to the project-x group
Important Files
/etc/passwd — User accounts
cat /etc/passwd | head -3
Format:
benjamin:x:1000:1000:Benjamin Tietz:/home/benjamin:/bin/bash
│ │ │ │ │ │ └── Login shell
│ │ │ │ │ └── Home directory
│ │ │ │ └── Comment (full name)
│ │ │ └── Primary GID
│ │ └── UID
│ └── Password placeholder (actual in /etc/shadow)
└── Username
/etc/shadow — Encrypted passwords
sudo cat /etc/shadow | head -3
This file is only readable by root. It stores encrypted passwords.
/etc/group — Group definitions
cat /etc/group | head -5
Format:
developers:x:1001:alice,bob
│ │ │ └── Members
│ │ └── GID
│ └── Password placeholder
└── Group name
Exercises
Exercise 1: User information
# 1. Display your username
whoami
# 2. Show your UID, GID, and groups
id
# 3. List all groups you belong to
groups
# 4. Check how many users exist on the system
wc -l /etc/passwd
# 5. List only real users (UID >= 1000)
awk -F: '$3 >= 1000 {print $1}' /etc/passwd
Exercise 2: Create a user (requires sudo)
# 1. Create a test user
sudo useradd -m -s /bin/bash -c "Test User" testuser
# 2. Set a password
sudo passwd testuser
# 3. Verify
id testuser
ls -la /home/testuser/
# 4. Switch to the user
su - testuser
whoami
exit
# 5. Clean up
sudo userdel -r testuser
Exercise 3: Group management (requires sudo)
# 1. Create a group
sudo groupadd testgroup
# 2. Verify
getent group testgroup
# 3. Add your user to the group
sudo usermod -aG testgroup $USER
# 4. Check (may need to log out and back in)
groups
# 5. Clean up
sudo gpasswd -d $USER testgroup
sudo groupdel testgroup
Key Takeaways
whoami,id,groups— check your identityuseradd -m -s /bin/bash— create users properlyusermod -aG group user— add to group (always use -a)userdel -r— delete user and home directorygroupadd/groupdel— manage groupspasswd— manage passwords/etc/passwd,/etc/shadow,/etc/group— the key files
Next Lesson: Lesson 9: Search & Find →