User Management Basics: Getting Started with useradd, usermod, and userdel
What you'll be able to do
- Create a user with useradd and get it to a state where it can log in
- Add groups safely with usermod -aG
- Understand what userdel removes and delete through a recoverable procedure
Prerequisites (read these first)
What You'll Learn
- You will be able to create a user with
useraddand get it to a state where it can log in. - You will be able to add groups safely with
usermod -aG. - You will be able to understand what
userdelremoves and delete through a recoverable procedure.
Who this is for: anyone who now has to create server accounts for new team members.
Terms defined up front
Each term below is defined once, here.
- A user is the identity that logs into Linux and operates it. There are two kinds: accounts people use, and accounts services use (system users).
- A group is a collection of users. Permissions on files and commands can be granted per group, not only per user.
- UID / GID are the ID numbers assigned to users and groups. Internally Linux decides by these numbers, not by names.
- A login shell is the shell that starts right after login.
/bin/bashis typical; setting/usr/sbin/nologinblocks login entirely. - A supplementary group is any group beyond the first that a user belongs to. It is also called a secondary group. The first one is the primary group.
- A home directory is the user's own workspace,
/home/<username>.
What Can You Do with useradd, usermod, and userdel?
Linux user management comes down to three commands: useradd to create users, usermod to modify their attributes, and userdel to remove them. This guide covers both Ubuntu/Debian and RHEL/CentOS with practical command examples.
Every command below needs sudo. User records are system-wide configuration, and an ordinary user cannot rewrite them.
Quick Reference
sudo useradd -m -s /bin/bash alice # Create user sudo passwd alice # Set password sudo usermod -aG sudo alice # Grant sudo privileges sudo userdel -r alice # Delete user and home directory
Prerequisites
- OS: Ubuntu 22.04 / Debian or RHEL-based (examples use Ubuntu)
- Run commands as a user with sudo privileges
1. How to Create a User with useradd
The useradd command adds a new user to the system. Without options, it does not create a home directory — always use the -m flag in practice.
sudo useradd -m -s /bin/bash alice
Common Options:
| Option | Description |
|---|---|
-m |
Create home directory |
-s /bin/bash |
Set login shell |
-d /custom/home |
Specify custom home directory path |
-G group1,group2 |
Set initial supplementary groups |
-c "Alice Smith" |
Comment field (typically full name) |
-e 2026-12-31 |
Account expiry date (YYYY-MM-DD) |
-r |
Create a system account (for services) |
Setting a Password
useradd alone does not set a password — the account cannot log in until you run passwd:
sudo passwd alice
Verifying the New User
id alice
uid=1001(alice) gid=1001(alice) groups=1001(alice)
grep alice /etc/passwd
alice:x:1001:1001::/home/alice:/bin/bash
The /etc/passwd fields are: username:password(x):UID:GID:comment:home:shell.
On Ubuntu/Debian, adduser is also available
adduser is an interactive wrapper around useradd. Use useradd for scripting and automation; use adduser for manual one-off setups since it prompts for a password automatically.
sudo adduser alice # Interactive setup including password
2. How to Modify a User with usermod
usermod changes attributes of an existing user. The most frequent operation is adding a user to a group.
Granting sudo Privileges
sudo usermod -aG sudo alice # Ubuntu/Debian sudo usermod -aG wheel alice # RHEL/CentOS/Fedora
Forget the -a and group memberships disappear
Never omit -a (append) from -aG. What happens when it goes wrong is the following.
- With
-Galone, every membership other than the groups you listed is removed. It replaces rather than appends. - Run it on yourself and drop out of the
sudogroup, andsudostops working. Since Ubuntu disables the root password, recovery needs a rescue boot from a physical console.
How to stay safe takes two habits.
- Record the current memberships with
id alicebefore running the command, so you can write them back if you replace them. - Practise on a test user, never on your own account.
id alice # record memberships before sudo usermod -aG sudo alice id alice # confirm the addition
Other Common Changes
Change home directory:
sudo usermod -d /new/home -m alice # -m moves existing files
Change login shell:
sudo usermod -s /bin/zsh alice
Lock or unlock an account:
sudo usermod -L alice # Lock password auth (key-based login still works) sudo usermod -U alice # Unlock
usermod -L prefixes the password hash in /etc/shadow with !, so the only thing it disables is password authentication. A user with a public key in ~/.ssh/authorized_keys can still log in over SSH after the lock. For the procedure that reliably stops login, see the userdel section below.
Verifying Changes
id alice groups alice
uid=1001(alice) gid=1001(alice) groups=1001(alice),27(sudo)
Group changes take effect at the next login. To apply them in the current session without logging out, run newgrp sudo.
3. How to Delete a User with userdel
userdel removes a user from the system. Use -r to also delete the home directory and mail spool.
sudo userdel -r alice
Behavior comparison:
| Command | User removed | Home directory | Mail spool |
|---|---|---|---|
userdel alice |
Yes | Kept | Kept |
userdel -r alice |
Yes | Deleted | Deleted |
userdel -r cannot be undone
What happens when it goes wrong comes down to two things.
-rdeletes the home directory and everything in it: the scripts, keys, and work files that user left behind.- If the UID is reused after deletion, files restored from a backup can end up owned by a different person.
How to stay safe takes three steps.
- Find out what is about to disappear.
sudo ls -la /home/alice # inspect what will be deleted sudo du -sh /home/alice # check the size
- Move anything you need out of the way.
sudo tar czf /root/alice-home.tar.gz /home/alice
- If there is no hurry, stop the login first and wait.
usermod -L disables password authentication only — SSH public-key login still works. To stop it reliably, run all three of these together.
sudo usermod -L alice # disable password authentication sudo chage -E 0 alice # expire the account (key-based login is refused too) sudo usermod -s /usr/sbin/nologin alice # disable the login shell
To reverse it, use sudo usermod -U alice, sudo chage -E -1 alice, and sudo usermod -s /bin/bash alice. To retire the key itself, move it aside with sudo mv /home/alice/.ssh/authorized_keys /root/alice-authorized_keys.bak.
Delete only once you are sure there is no going back.
Deleting a Logged-in User
A logged-in user cannot be deleted. The command refuses with something like userdel: user alice is currently used by process 1234.
who # Check who is logged in sudo pkill -u alice # Kill all processes owned by alice sudo userdel -r alice
pkill -u terminates that user's processes without asking. Unsaved work and any service that user is running stop too. Check what is running with ps -u alice first.
4. Group Management Basics
Users and groups are tightly coupled in Linux. Here are the essential group operations.
Create a Group
sudo groupadd developers
Add a User to a Group
sudo usermod -aG developers alice
Delete a Group
sudo groupdel developers
Check Group Membership
grep developers /etc/group
developers:x:1002:alice
/etc/group fields: groupname:password:GID:member-list
5. Password Policy and Account Expiry
Use chage to inspect and configure password aging policies.
sudo chage -l alice
Last password change : May 31, 2026 Password expires : never Account expires : never
Setting expiry:
sudo chage -M 90 alice # Expire password after 90 days sudo chage -E 2026-12-31 alice # Set account expiry date
6. System Accounts for Services
Service daemons (nginx, postgres, etc.) run under dedicated system accounts that cannot log in interactively. Create them with -r:
sudo useradd -r -s /usr/sbin/nologin -d /var/lib/myapp myapp
Key flags for service accounts:
| Flag | Purpose |
|---|---|
-r |
Mark as system account (UID < 1000) |
-s /usr/sbin/nologin |
Prevent interactive login |
-d /var/lib/myapp |
Set working directory |
-M |
Skip home directory creation |
Summary: Practical Command Patterns
Standard Flow for a New User
sudo useradd -m -s /bin/bash -c "Alice Smith" alice sudo passwd alice sudo usermod -aG sudo alice id alice # Verify
Quick Reference Commands
id username # UID/GID and group list groups username # Group memberships grep username /etc/passwd # passwd entry grep username /etc/group # Group memberships sudo cat /etc/shadow # Password hash info
Troubleshooting
Symptom: the new user cannot log in
Cause: passwd was never run, so the password is unset and the account is locked.
Check:
sudo passwd -S alice
alice L 05/31/2026 0 99999 7 -1
The second field is L for locked, P when a password is set.
Fix:
sudo passwd alice
Symptom: useradd succeeded but there is no home directory
Cause: it was run without -m. On Ubuntu, useradd does not create a home by default.
Check:
ls -ld /home/alice
Fix: copy the contents of /etc/skel (the skeleton directory for new users) first, then set the owner and permissions. Without the skeleton, there is no .bashrc or .profile, which leads to a different set of problems: a broken prompt, no aliases, and ~/.local/bin missing from PATH.
sudo mkdir /home/alice sudo cp -a /etc/skel/. /home/alice/ sudo chown -R alice:alice /home/alice sudo chmod 700 /home/alice
On Ubuntu and Debian, sudo mkhomedir_helper alice performs the same work in one step.
Symptom: a group was added but the privileges do not apply
Cause: group changes take effect at the user's next login. The current session still holds the old information.
Check:
id alice # the current state in /etc/group groups # what this session is carrying
Fix: log out and back in. To apply it immediately, run newgrp sudo.
Symptom: userdel fails with currently used by process
Cause: processes owned by that user are still running.
Check:
ps -u alice
Fix: review the list, run sudo pkill -u alice, then delete.
Completion Checklist
- [ ] Created the home directory with
useradd -m - [ ] Set a password with
passwdso the account can log in - [ ] Added groups with
usermod -aG(with the-a) - [ ] Verified UID, GID, and memberships with
id - [ ] Inspected the home directory and took a backup before deleting
- [ ] When locking instead of deleting, went as far as
chage -E 0to stop key-based login too