CIS120 Linux Fundamentals by Scott Shaper

useradd, userdel, and usermod Commands

Linux gives you three main commands to manage user accounts: useradd creates a new user, userdel removes one, and usermod changes an existing account. Think of them as the official way to add, remove, or update entries in /etc/passwd, /etc/shadow, and /etc/group—instead of editing those files by hand. Only root (or someone with the right sudo privileges) can run these commands. Together they cover the full lifecycle of a user account: create with useradd, change with usermod, and remove with userdel when access is no longer needed.

Quick Reference

Command What It Does Common Use
useradd Creates a new user account Adding new users to the system
userdel Removes a user account Deleting users who no longer need access
usermod Modifies an existing user account Changing shell, home, groups, or locking an account

Note: All three require root privileges. Use sudo useradd ..., sudo userdel ..., and sudo usermod ....

useradd Command

useradd creates a new user: it adds a line to /etc/passwd (and usually /etc/shadow and /etc/group), can create a home directory with -m, and uses defaults from /etc/default/useradd. It's the first step in giving a new person—or a service account—access to the machine.

When to Use useradd

Common useradd Options

Option What It Does When to Use It
-mCreate the user's home directoryAlmost always for real users
-d pathSet home directory pathWhen you want a custom path (e.g. /home/jdoe)
-s shellSet default shelle.g. -s /bin/bash or -s /usr/sbin/nologin
-c "comment"Set GECOS (full name, etc.)To store the user's full name
-g groupSet primary group (by name or GID)When the user should have a specific primary group
-G group1,group2Add user to supplementary groupsTo give extra group memberships
-u UIDSet user ID (must be unique)When you need a specific UID
-e YYYY-MM-DDSet account expiration dateFor temporary accounts

useradd Examples

# Create user jdoe with a home directory and set password
sudo useradd -m jdoe
sudo passwd jdoe

# Create user with full name and bash as shell
sudo useradd -m -c "Jane Doe" -s /bin/bash jdoe
sudo passwd jdoe

# Create user and add to supplementary groups (e.g. developers, sudo)
sudo useradd -m -G developers,sudo jdoe
sudo passwd jdoe

# System/service account (no login)
sudo useradd -r -s /usr/sbin/nologin myapp

-r creates a system account (low UID). /usr/sbin/nologin prevents interactive login. Always run passwd username after useradd for real users so they can log in.

userdel Command

userdel removes a user account from the system. It deletes the user's entry from /etc/passwd (and usually from /etc/shadow and /etc/group membership). You can choose whether to remove their home directory and mail spool as well.

When to Use userdel

Common userdel Options

Option What It Does When to Use It
-rRemove the user's home directory and mail spoolWhen you want to delete their files too
-fForce removal even if user is logged in (use with caution)Only when necessary; can leave orphaned processes

userdel Examples

# Remove account only; home directory stays (e.g. for backup)
sudo userdel jdoe

# Remove account and delete home directory and mail spool
sudo userdel -r jdoe

# Force removal even if user is logged in (use with care)
sudo userdel -f -r jdoe

Back up important data before using -r. Use -f only when you understand the impact: the user may be kicked out and processes may run as a non-existent user. Check with who or ps -u username that the user isn't logged in.

usermod Command

usermod changes an existing user account. It updates the user's entry in /etc/passwd (and related files): you can change the home directory, shell, full name, group memberships, lock or unlock the account, and more. Use it whenever you need to fix or update a user's settings instead of deleting and recreating the account.

When to Use usermod

Common usermod Options

Option What It Does When to Use It
-d pathSet new home directory pathWhen moving a user's home
-mMove contents of current home to new home (use with -d)When changing home and keeping files
-s shellSet default shelle.g. -s /bin/bash or -s /usr/sbin/nologin
-c "comment"Set GECOS (full name, etc.)To update the user's full name
-aG group1,group2Add user to supplementary groups (append)To add groups without removing existing ones
-G group1,group2Set supplementary groups (replaces list; use -aG to add)When you want to set the full list
-LLock the account (password invalid)To disable login without deleting the account
-UUnlock the accountTo re-enable after -L
-e YYYY-MM-DDSet account expiration dateFor temporary access

usermod Examples

# Change default shell
sudo usermod -s /bin/bash jdoe
sudo usermod -s /usr/sbin/nologin jdoe

# Add user to groups (use -aG so you don't remove existing groups)
sudo usermod -aG developers,sudo jdoe

# Change full name (GECOS)
sudo usermod -c "Jane Doe" jdoe

# Lock and unlock account
sudo usermod -L jdoe
sudo usermod -U jdoe

# Move home directory and move existing files there
sudo usermod -m -d /home/jdoe2 jdoe

Always use -aG when adding groups so you don't wipe out existing group memberships. The user should not be logged in when changing UID, GID, or home directory.

Tips for Success

Common Mistakes to Avoid

Best Practices