useradd Command Guide for Creating New User Accounts
useradd creates a new user account on the system — defining its home directory, shell, groups, and the entry that appears in /etc/passwd.
What is useradd, and Why Use It?
useradd is the low-level tool for adding accounts: it writes the new account’s line into /etc/passwd, creates its home directory, and sets up group membership. Almost every “add a user” workflow on a server — developers, service accounts, deploy users — starts here.
On its own useradd is minimal: it creates the account but leaves it locked (no password set) until you run passwd. Friendly distributions wrap it in adduser, which does the same job interactively.
sudo useradd -m -s /bin/bash alice
Why Learn useradd?
- Servers need accounts — every team member, service, and automation identity is a
useraddaway. - It’s the primitive under
adduser— when the friendly wrapper is missing (minimal installs, containers, scripts),useraddis what’s there. - Service accounts depend on it — databases, web servers, and CI runners all run as accounts created this way.
- Understanding it demystifies
/etc/passwd— you’ll know exactly what each field of the new line means.
Creating a user is the first step of setup; securing it is the rest. Set a strong password (or SSH key), pick the narrowest groups, and give service accounts a no-login shell.
1. Create a Basic User
The common invocation, with home directory and a real shell:
sudo useradd -m -s /bin/bash username
Example:
# Create bob with a home directory and bash
$ sudo useradd -m -s /bin/bash bob
# Set the password (the account is locked until this)
$ sudo passwd bob
New password:
Retype new password:
passwd: password updated successfully
# Verify the account exists
$ id bob
uid=1001(bob) gid=1001(bob) groups=1001(bob)
$ ls -ld /home/bob
drwxr-xr-x 3 bob bob 4096 Dec 4 10:00 /home/bob
-mcreates/home/username— without it, the account exists but has no home directory.-s /bin/bashsets the login shell; the default on many systems is a minimal or nologin shell.- Always follow with
passwd— a new account can’t log in until it has a password or key.
2. Create a User With Groups
Add the user to supplementary groups at creation time:
sudo useradd -m -s /bin/bash -G sudo,docker username
Example:
# A developer who can sudo and use docker
$ sudo useradd -m -s /bin/bash -G sudo,docker dev
$ id dev
uid=1002(dev) gid=1002(dev) groups=1002(dev),27(sudo),999(docker)
-Gtakes a comma-separated list of supplementary groups.- Group membership only applies at login — an existing shell won’t see it until re-login.
- The first group in the system’s
GROUPSdefault becomes the user’s primary group automatically.
3. Create a Service Account
Service accounts are for programs, not people — no login, no password:
sudo useradd -r -s /usr/sbin/nologin -M appuser
Example:
# A system account for a web service
$ sudo useradd -r -s /usr/sbin/nologin -M wwwapp
$ id wwwapp
uid=999(wwwapp) gid=999(wwwapp) groups=999(wwwapp)
$ grep wwwapp /etc/passwd
wwwapp:x:999:999::/home/wwwapp:/usr/sbin/nologin
-rmarks it a system account (UID range reserved for services).-s /usr/sbin/nologinmakes interactive login fail by design.-Mskips home directory creation — services rarely need one.- This is the pattern behind
postgres,www-data, and every service account you’ll meet.
4. The Options That Matter
| Option | Meaning |
|---|---|
-m | Create the home directory |
-d /path | Set a custom home directory |
-s /bin/shell | Set the login shell |
-g primarygroup | Set the primary group |
-G g1,g2 | Add supplementary groups |
-u UID | Force a specific user ID |
-r | Create a system account (low UIDs) |
-c "comment" | Add a description (GECOS field) |
-e YYYY-MM-DD | Set an account expiry date |
Example:
# A contractor account with an expiry date and custom home
$ sudo useradd -m -s /bin/bash -G devs -c "Contractor, acme.io" -e 2027-03-01 contractor
$ grep contractor /etc/passwd
contractor:x:1003:1003:Contractor, acme.io:/srv/contractor:/bin/bash
-eis how temporary access gets enforced — the account simply stops working on the date.-cis free-text metadata; it’s the “comment” column of/etc/passwd.
5. useradd vs adduser vs Deleting
Different tools, different jobs:
| Task | Command |
|---|---|
| Create user (friendly) | sudo adduser sam (Debian/Ubuntu) |
| Create user (manual) | sudo useradd -m -s /bin/bash sam |
| Create user (RHEL) | sudo adduser sam or useradd |
| Set password | sudo passwd sam |
| Modify groups later | sudo usermod -aG group sam |
| Delete user + home | sudo userdel -r sam |
Example:
# adduser: interactive, asks questions, sets password inline
$ sudo adduser sam
Adding user `sam' ...
New password: ...
# userdel: remove the account and everything it owns
$ sudo userdel -r sam
$ grep sam /etc/passwd # nothing — account gone
adduseris a friendly script built onuseradd— same result, guided process.userdel -rremoves the account and its home directory and mail spool; without-r, orphaned files stay behind.- After deletion, verify nothing the user owned is left:
find / -user sam 2>/dev/null.
Common Pitfalls
- Creating a user and forgetting the password.
useraddalone leaves the account locked — nobody can log in untilpasswd(or an SSH key) is set. - Omitting
-m. The account exists but has no home directory; the first login may fail or behave oddly. - Forgetting that groups apply at login. Adding
-G sudodoesn’t affect already-open shells; log out and back in, then confirm withid. - Using blanket sudo for convenience accounts. Give
-G sudoonly when full admin is intended; prefer narrow group grants otherwise. - Deleting with
userdelbut not-r. Orphaned home directories and files remain, owned by a now-nonexistent UID — clean them up or they become stray data. - Creating service accounts as login accounts. A
-s /bin/bashservice account with a password is an attack surface; services want-r -s /usr/sbin/nologin.
Related Pages
| Page | Why it’s related |
|---|---|
| passwd | Sets the password every new account needs |
| /etc/passwd | The file useradd writes the new account into |
| id | Verifying the new account’s UID, GID, and groups |
| su | Switching into the new account once it exists |
| sudo | The elevation user creation requires |
| chown | Fixing ownership when files outlive their account |
useradd Cheatsheet
| Command | Meaning |
|---|---|
sudo useradd -m -s /bin/bash sam | Create a normal user with home + bash |
sudo useradd -m -G sudo,docker sam | Create a user with supplementary groups |
sudo useradd -r -s /usr/sbin/nologin svc | Create a locked system/service account |
sudo useradd -d /srv/sam -m sam | Create with a custom home directory |
sudo useradd -c "description" sam | Add a comment/GECOS field |
sudo useradd -e 2027-01-01 sam | Set an account expiry date |
sudo passwd sam | Set the password (unlocks the account) |
sudo usermod -aG group sam | Add a group to an existing user |
sudo userdel -r sam | Delete the user and their files |