Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 useradd away.
  • It’s the primitive under adduser — when the friendly wrapper is missing (minimal installs, containers, scripts), useradd is 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
  • -m creates /home/username — without it, the account exists but has no home directory.
  • -s /bin/bash sets 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)
  • -G takes 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 GROUPS default 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
  • -r marks it a system account (UID range reserved for services).
  • -s /usr/sbin/nologin makes interactive login fail by design.
  • -M skips 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

OptionMeaning
-mCreate the home directory
-d /pathSet a custom home directory
-s /bin/shellSet the login shell
-g primarygroupSet the primary group
-G g1,g2Add supplementary groups
-u UIDForce a specific user ID
-rCreate a system account (low UIDs)
-c "comment"Add a description (GECOS field)
-e YYYY-MM-DDSet 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
  • -e is how temporary access gets enforced — the account simply stops working on the date.
  • -c is free-text metadata; it’s the “comment” column of /etc/passwd.

5. useradd vs adduser vs Deleting

Different tools, different jobs:

TaskCommand
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 passwordsudo passwd sam
Modify groups latersudo usermod -aG group sam
Delete user + homesudo 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
  • adduser is a friendly script built on useradd — same result, guided process.
  • userdel -r removes 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. useradd alone leaves the account locked — nobody can log in until passwd (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 sudo doesn’t affect already-open shells; log out and back in, then confirm with id.
  • Using blanket sudo for convenience accounts. Give -G sudo only when full admin is intended; prefer narrow group grants otherwise.
  • Deleting with userdel but 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/bash service account with a password is an attack surface; services want -r -s /usr/sbin/nologin.

PageWhy it’s related
passwdSets the password every new account needs
/etc/passwdThe file useradd writes the new account into
idVerifying the new account’s UID, GID, and groups
suSwitching into the new account once it exists
sudoThe elevation user creation requires
chownFixing ownership when files outlive their account

useradd Cheatsheet

CommandMeaning
sudo useradd -m -s /bin/bash samCreate a normal user with home + bash
sudo useradd -m -G sudo,docker samCreate a user with supplementary groups
sudo useradd -r -s /usr/sbin/nologin svcCreate a locked system/service account
sudo useradd -d /srv/sam -m samCreate with a custom home directory
sudo useradd -c "description" samAdd a comment/GECOS field
sudo useradd -e 2027-01-01 samSet an account expiry date
sudo passwd samSet the password (unlocks the account)
sudo usermod -aG group samAdd a group to an existing user
sudo userdel -r samDelete the user and their files