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/passwdIn 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
| Option | Means |
|---|---|
-m | Make the home directory /home/bob (filled with the files from /etc/skel) |
-s /bin/bash | The login shell |
-c "..." | A comment, usually the full name |
-G group1,group2 | Extra groups |
-u 1500 | Choose 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 bobIn 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,useradddoesn'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.useraddworks 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 aliceIn 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 option | Changes |
|---|---|
-s shell | The login shell |
-c "..." | The comment |
-aG group | Adds an extra group (next lesson) |
-L / -U | Locks / unlocks the password, like passwd -l/-u |
-l newname | Renames 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 aliceIn 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, thensudo passwd name.usermodchanges,userdel -rremoves with the home directory.