useradd — Create and Manage User Accounts on Linux

Practical guide to useradd, usermod and userdel — create, modify and delete Linux user accounts, home directories, groups and passwords safely.

useradd is the low-level standard tool for creating Linux user accounts directly – without -m it leaves no home directory, and without a follow-up passwd the account stays locked. On Debian and Ubuntu the friendlier adduser wrapper handles those steps interactively and applies sensible defaults. Together with usermod, userdel, passwd and the group commands you manage accounts, shells and group memberships. Almost every one of these actions needs sudo and reaches deep into the system.

Create Users

useradd <user> — Create a new user (minimal, no home directory on some distros).

sudo useradd john

useradd -m <user> — Create a user with a home directory.

sudo useradd -m john

useradd -m -s <shell> <user> — Create a user with a specific login shell.

sudo useradd -m -s /bin/bash john

useradd -m -G <groups> <user> — Create a user and add to supplementary groups.

sudo useradd -m -G sudo,docker john

useradd -r -s /usr/sbin/nologin <user> — Create a system user (no login, for services).

sudo useradd -r -s /usr/sbin/nologin appuser

useradd -m -c '<comment>' <user> — Create a user with a full name/comment.

sudo useradd -m -c 'John Doe' john

Modify Users

usermod -aG <group> <user> — Add a user to a group (append, keep existing groups).

sudo usermod -aG docker john

usermod -s <shell> <user> — Change a user's login shell.

sudo usermod -s /bin/zsh john

usermod -l <newname> <oldname> — Rename a user account.

sudo usermod -l johndoe john

usermod -d <dir> -m <user> — Change home directory and move files.

sudo usermod -d /home/johndoe -m john

usermod -L <user> — Lock a user account (disable login).

sudo usermod -L john

usermod -U <user> — Unlock a user account.

sudo usermod -U john

Delete Users

userdel <user> — Delete a user (keep home directory).

sudo userdel john

userdel -r <user> — Delete a user and remove home directory.

sudo userdel -r john

Passwords

passwd <user> — Set or change a user's password.

sudo passwd john

passwd -l <user> — Lock a user's password.

sudo passwd -l john

passwd -u <user> — Unlock a user's password.

sudo passwd -u john

passwd -e <user> — Expire password (force change on next login).

sudo passwd -e john

chage -l <user> — Show password aging information.

sudo chage -l john

chage -M <days> <user> — Set maximum password age in days.

sudo chage -M 90 john

Groups

groupadd <group> — Create a new group.

sudo groupadd developers

groupdel <group> — Delete a group.

sudo groupdel developers

groups <user> — Show all groups a user belongs to.

groups john

gpasswd -a <user> <group> — Add a user to a group.

sudo gpasswd -a john docker

gpasswd -d <user> <group> — Remove a user from a group.

sudo gpasswd -d john docker

getent group <group> — Show members of a group.

getent group docker

Info & Listing

id <user> — Show user UID, GID, and group memberships.

id john

whoami — Show the current username.

whoami

getent passwd <user> — Show user account details from passwd database.

getent passwd john

cat /etc/passwd | grep <user> — Look up a user in the passwd file.

cat /etc/passwd | grep john

last <user> — Show last login times for a user.

last john

Conclusion

For everyday work a handful of commands suffice: useradd -m creates a full account with a home directory, passwd sets the first password, and usermod -aG appends group memberships. That last one hides the most common trap – forget the -a and usermod -G replaces all existing supplementary groups instead of adding to them. Keep in mind that membership in sudo or docker effectively grants root-equivalent privileges and should be handed out deliberately. And userdel -r wipes the entire home directory along with all its data – without asking.

Further Reading

  • su – switch to another user or to root
  • sudo – run individual commands with elevated privileges
  • id – show a user's UID, GID and group memberships