Creating, Changing and Removing Users

useradd: a new account

Creating users is an admin job, so it needs sudo. The options matter:

sudo useradd -m -s /bin/bash -c "Bob Builder" bob
id bob
ls -la /home/bob
grep bob /etc/passwd

In the terminal

student@vulntech:~$ sudo useradd -m -s /bin/bash -c "Bob Builder" bob
[sudo] password for student:
student@vulntech:~$ id bob
uid=1001(bob) gid=1002(bob) groups=1002(bob)
student@vulntech:~$ ls -la /home/bob
total 3
drwxr-xr-x 2 bob  bob    0 Oct 17 06:36 .
drwxr-xr-x 4 root root   0 Oct 17 05:31 ..
-rw-r--r-- 1 bob  bob   32 Oct 16 17:43 .bash_profile
-rw-r--r-- 1 bob  bob  669 Oct 16 17:43 .bashrc
-rw-r--r-- 1 bob  bob    0 Oct 16 17:43 .hushlogin
student@vulntech:~$ grep bob /etc/passwd
bob:x:1001:1002:Bob Builder:/home/bob:/bin/bash
OptionMeans
-mMake the home directory /home/bob (filled with the files from /etc/skel)
-s /bin/bashThe login shell
-c "..."A comment, usually the full name
-G group1,group2Extra groups
-u 1500Choose the UID yourself

The new account has no password yet, so nobody can log in with it. Set one:

sudo useradd -m -s /bin/bash bob
sudo passwd bob

In the terminal

student@vulntech:~$ sudo useradd -m -s /bin/bash bob
[sudo] password for student:
student@vulntech:~$ sudo passwd bob
New password:
Retype new password:
passwd: password updated successfully

Common mistake: Without -m, useradd doesn't create a home directory, and the user lands in / with nowhere to keep files.

Tip: On Ubuntu you'll also meet adduser, a friendlier script that asks questions and sets the password for you. useradd works the same on every Linux system, so it's the one to know.

usermod: change an account

sudo usermod -c "Alice A. Archer" -s /bin/sh alice
getent passwd alice

In the terminal

student@vulntech:~$ sudo usermod -c "Alice A. Archer" -s /bin/sh alice
[sudo] password for student:
student@vulntech:~$ getent passwd alice
alice:x:1001:1002:Alice A. Archer:/home/alice:/bin/sh
usermod optionChanges
-s shellThe login shell
-c "..."The comment
-aG groupAdds an extra group (next lesson)
-L / -ULocks / unlocks the password, like passwd -l/-u
-l newnameRenames the user

userdel: remove an account

userdel user removes the account but leaves its files. -r also deletes the home directory:

sudo userdel -r alice
ls /home
id alice

In the terminal

student@vulntech:~$ sudo userdel -r alice
[sudo] password for student:
userdel: alice mail spool (/var/mail/alice) not found
student@vulntech:~$ ls /home
student
student@vulntech:~$ id alice
id: ‘alice’: no such user: Invalid argument

The mail spool not found message is harmless: Alice never got any mail on this machine.

Remember: sudo useradd -m -s /bin/bash name, then sudo passwd name. usermod changes, userdel -r removes with the home directory.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…