The useradd command
The useradd command creates a user account…
The useradd command creates a user account: sudo useradd -m -s /bin/bash sam makes the account sam with a home folder and bash as its shell.
Taught in Advanced, lesson 41
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
sudo useradd [-m] [-s SHELL] [-G GROUPS] [-c COMMENT] NAME
Create a user account. Adds a user to /etc/passwd and gives them their own group. It is the low-level tool and does only what you ask: without -m there is no home folder, and the shell is /bin/sh unless you set -s /bin/bash. The new account has no password until you run sudo passwd NAME. Needs sudo. Everything here is simulated in the page: no real accounts, passwords or software are involved.
In the practice terminal:Accounts are made in the practice machine’s own /etc/passwd, inside the page. No real account is created, and no password is asked for or kept.
How to use useradd
Create a user with a home folder
Without -m there is no home folder, and without -s the shell is plain sh. Set a password with passwd afterwards.
sudo useradd -m -s /bin/bash sam && id sam && ls /home[sudo] password for learner: uid=1001(sam) gid=1001(sam) groups=1001(sam) learner sam
Set the password: passwd
sudo useradd -m sam && sudo passwd sam[sudo] password for learner: New password: Retype new password: passwd: password updated successfully
Add groups and a name as you create it
-G lists extra groups and -c is a comment, usually the person’s full name.
sudo useradd -m -G adm,sudo -c "Sam Lee" sam && grep sam /etc/passwd && groups sam[sudo] password for learner: sam:x:1001:1001:Sam Lee:/home/sam:/bin/sh sam : sam adm sudo
Where accounts live: /etc/passwd
One line per account: the name, x (the password is kept in /etc/shadow), the user ID, the group ID, a comment, the home folder and the shell.
sudo useradd -m sam && tail -n 2 /etc/passwd[sudo] password for learner: learner:x:1000:1000:learner:/home/learner:/bin/bash sam:x:1001:1001::/home/sam:/bin/sh
Without sudo
useradd samuseradd: Permission denied. useradd: cannot lock /etc/passwd; try again later.
Options
| -m | make the home folder /home/NAME |
|---|---|
| -s SHELL | the login shell, such as /bin/bash |
| -G GROUPS | extra groups, separated by commas |
| -c COMMENT | a full name or note |
| -d HOME | a different home folder |
More examples
sudo useradd -m -s /bin/bash sam[sudo] password for learner:
id samuid=1001(sam) gid=1001(sam) groups=1001(sam)
Questions about useradd
What is the difference between useradd and adduser?
useradd is the basic tool on every Linux system and does only what its options say. adduser, on Debian and Ubuntu, is a friendlier script that asks questions, makes the home folder and sets a password.
How do I delete a user?
sudo userdel sam removes the account, and sudo userdel -r sam its home folder too.
How do I give a new user sudo?
Add the account to the sudo group: sudo usermod -aG sudo sam (the group is wheel on Fedora). It works from that user’s next login.
Learn it step by step
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.