Users and groups on Linux¶
Practical 5.1 users-and-groups-on-linux-annex.pdf · PDF
Open a terminal in deb-lab and type two letters: id. The machine answers with something like uid=1000(student). Your name is there, in brackets. But what is that 1000 in front of it — and why does it come first?
Monday, 9:00, Tramuntana Apps. Four people start today: marta and pau in development, joan in design, aina in administration. Each of them needs an account on the new machine, the right department, and a home folder nobody else can snoop in. You are the sysadmin. By the end of this page you can do it — and you will know the classic mistake that silently throws people out of their groups.
What you will learn
- What a user account is for the system: a number (UID) with a name, a home and a shell
- Read a line of
/etc/passwd,/etc/groupand/etc/shadowfield by field, and say who may read each one - Create users and groups with
adduserandgroupadd, and check them withidandgetent - Add a user to a group with
usermod -aG— and why forgetting the-aremoves all their other groups - Tell
sufromsudo: whose password is asked, and for how long you are someone else - Lock, unlock and delete an account
You are a number¶
Back in Unit 2 the operating system was the kitchen manager, and one of its jobs was managing users. Here is a secret about that manager: it does not know names. Every cook wears a number on the apron, and the manager only reads numbers.
A user account is a number the system uses to tell people (and programs) apart, plus a name, a home folder and a shell for the humans.
That number is the UID (user identifier). Every file and every running program is stamped with one. The names are only a list on the kitchen wall so that humans can read the numbers.
| Part | Meaning |
|---|---|
uid=1000(student) |
Your UID is 1000; its name is student |
gid=1000(student) |
Your primary group (more on that below) |
groups=...,27(sudo),... |
Every group you belong to, number and name |
There are three kinds of account, told apart by the UID:
| UID | Who | Shell |
|---|---|---|
| 0 | root, the administrator: the only account the kernel never says no to | /bin/bash |
| 1–999 | System accounts: not people but services (printing, time, a web server), each with its own number so that one going wrong cannot touch the files of the others | /usr/sbin/nologin |
| 1000 and up | People | /bin/bash |
The odd one: nobody
On a fresh deb-lab you will also see an account called nobody with UID 65534. It is a special account that owns nothing — a cook with an apron but no station. It is not a person.
The list on the wall: /etc/passwd¶
The list of numbers and names lives in a plain text file: /etc/passwd. One line per account, seven fields separated by :.
| # | Field | Meaning |
|---|---|---|
| 1 | marta |
User name (the login) |
| 2 | x |
Where the password used to be. Today x means "look in /etc/shadow" |
| 3 | 1001 |
UID: the number of this user |
| 4 | 1001 |
GID (group identifier): the number of the user's primary group |
| 5 | Marta Vidal |
Comment: the real name |
| 6 | /home/marta |
Home directory: her own folder |
| 7 | /bin/bash |
The program started at login: her shell |
It is a table, just like staff.csv in Back to the shell, only with : instead of ,. So the same tools work:
grep "^$USER:" /etc/passwd # your own line
cut -d: -f7 /etc/passwd | sort | uniq -c # how many accounts use each shell
cut -d: -f1,3 /etc/passwd | sort -t: -k2 -n | tail -4 # the four highest UIDs
The second line shows the surprise: most accounts use /usr/sbin/nologin. Most accounts on a Linux system are not people.
Careful: /etc/passwd does not contain passwords
It did, decades ago, and the name stayed. Today the second field is just an x. Anyone can read /etc/passwd; only root can read the file where the real secrets are.
Three files, three jobs¶
| File | What it holds | Who can read it |
|---|---|---|
/etc/passwd |
Who exists: name, UID, GID, home, shell | Everyone (-rw-r--r--) |
/etc/shadow |
Password hashes and expiry dates | Only root (-rw-r-----, owner root, group shadow) |
/etc/group |
The groups, their GIDs and their members | Everyone |
A hash is a scrambled fingerprint of the password: the system can check a password against it, but nobody can turn it back into the password. On Debian 13 the hash in /etc/shadow starts with $y$, which means yescrypt, the method used. You will open this file properly in Account and password policies.
ls -l /etc/passwd /etc/shadow
cat /etc/shadow # Permission denied
sudo grep "^$USER:" /etc/shadow # works: field 2 starts with $y$
A group is a list of users that gets rights together: one decision for the whole dev team instead of seven.
A line of /etc/group has four fields: name, x, GID, members.
That is the group whose members may use sudo. Check yours with getent group sudo. The command getent ("get entries") reads these account lists for you; getent passwd marta and getent group dev print just the line you ask for.
Creating users and groups¶
Back to Monday morning. One complete example, for marta:
sudo groupadd dev # a new, empty group
sudo adduser marta # asks for a password and the real name
id marta # uid=1001(marta) gid=1001(marta) groups=1001(marta),100(users)
sudo usermod -aG dev marta # -a append, -G to these groups
id marta # ... groups=1001(marta),100(users),1002(dev)
ls -ld /home/marta # drwx------ marta marta
Read what adduser did, from its own messages:
- It created the user
martawith the next free UID. - It created a group with the same name,
marta: her primary group. - It created
/home/marta, copying the starter files from/etc/skel, and closed it to everybody else (drwx------). - It added her to the group
users.
adduser is Debian's friendly tool. Underneath it calls useradd, the bare one, which creates no home and sets no password unless you tell it to. groupadd creates a group; usermod modifies an existing user.
Careful: 'adduser: command not found'
It is installed, in /usr/sbin — a folder that is not in the PATH of normal users on Debian. The message really means "you forgot sudo".
adduser asks for a room number and phone numbers. Press Enter to skip them, then Y. If you make a typo in the user name, delete the user and create it again; never edit /etc/passwd by hand.
Primary and supplementary groups¶
Every user has:
- exactly one primary group — field 4 of
/etc/passwd. On Debian it is a private group with the user's own name. New files the user creates get this group. - any number of supplementary groups — listed in
/etc/group. These are the departments:dev,design,admin.
flowchart LR
subgraph USER ["The account marta"]
M["<b>marta</b><br>UID 1001"]
end
M -->|"primary group<br>field 4 of /etc/passwd"| PG["<b>marta</b><br>GID 1001"]
M -->|"supplementary"| U["<b>users</b><br>GID 100"]
M -->|"supplementary"| D["<b>dev</b><br>shared with pau"]
The -a trap
usermod -G design paumeans "the list of extra groups is now this one". Pau leavesdev(andusers) without any warning.usermod -aG design paumeans "add design to the list". Pau keepsdev.
Real-life version: someone runs usermod -G docker marta on a server, and marta silently leaves the group sudo. Nobody notices until she needs it.
To repair, add the lost group back with -aG. To take someone out of one group without touching the others, use gpasswd -d:
One more surprise: after usermod, id marta shows the new group at once, but a session of marta that was already open does not have it. Group lists are read at login. Log out and in again.
Careful: the group admin is not for administrators
At Tramuntana Apps, admin is the administration department — aina does the invoices. On Debian the name gives no special power. The powerful group is sudo.
Becoming someone else: su and sudo¶
There are two ways to act as another user.
su - marta |
sudo command |
|
|---|---|---|
| What it does | Starts a session as marta, until exit |
Runs one command as root |
| Whose password | Marta's | Yours |
| Who may | Anyone who knows that password | Only members of the group sudo |
| Trace left | Little | Every command is logged with your name |
The - in su - marta matters: it gives marta's own environment and home, as in a real login.
Before every command, read the prompt: it tells you who you are. Forgetting exit and running the next admin command as marta is the most common mistake of the day.
When sudo asks for your password, it shows nothing while you type — not even dots. That is normal. The whole story of sudo comes in sudo and least privilege.
Locking and deleting¶
Aina goes on leave. Her account should stop working, but her files must stay.
sudo passwd -l aina # lock
sudo passwd -S aina # status: aina L ... (L = locked)
su - aina # su: Authentication failure, even with the right password
sudo passwd -u aina # unlock: passwd -S now shows aina P ... (P = usable password)
passwd -l puts a ! in front of the hash in /etc/shadow, so no password can ever match it. A lock closes the door for the user, not for the administrator: root can still enter with sudo su - aina.
When someone really leaves, delete the account and its home:
| Action | The account | The files |
|---|---|---|
Lock (passwd -l) |
Still exists | Still there |
Delete (deluser --remove-home) |
Gone | Home deleted |
Practical 5.1: onboarding Tramuntana Apps¶
Open the Practical 5.1 sheet (the button at the top) and do it in deb-lab. Part A has you read the three files on your own machine. Then the onboarding: three groups (dev, design, admin), four users (marta, joan, aina, pau, lab password tramuntana — weak on purpose; the system itself will refuse it soon), each in their department. You will break pau's groups on purpose and repair them, lock aina, and delete a temp user.
Write practical-5.1-surname.md as you go: commands and outputs in code blocks, plus one line of explanation for the su refusals, the -a mistake and the lock.
Keep the users and groups
Do not delete marta, joan, aina, pau or the three groups. You need them again in the next sessions on sudo, passwords and permissions.
Key takeaways¶
- The system knows you by your UID, not your name. root is UID 0, system accounts are 1–999, people start at 1000.
/etc/passwdlists who exists (seven fields, readable by all);/etc/shadowkeeps the password hashes (root only);/etc/grouplists the groups and their members.addusercreates the user, a primary group with the same name, and a private home directory;groupaddcreates groups;idandgetentcheck the result.usermod -Greplaces the list of supplementary groups;usermod -aGadds to it. Forgetting-asilently removes groups.su -opens a session as another user with their password;sudoruns one command as root with yours.- Locking (
passwd -l) stops the password from matching but keeps the account;deluser --remove-homedeletes it.
Quick check¶
1. Which file keeps the password hashes on Linux?
- /etc/passwd
- /etc/group
- /etc/shadow
- /etc/skel
/etc/shadow holds the hashes and expiry dates and only root can read it. Despite its name, /etc/passwd only has an x in the password field, and everyone can read it.
2. In marta
1001:1001:Marta Vidal:/home/marta:/bin/bash, what is the first 1001?
- Her UID
- Her password
- The size of her group
- The size of her home
Field 3 is the UID, the number the system really uses to identify marta. Field 4, the second 1001, is the GID of her primary group.
3. Pau is in dev. After sudo usermod -G design pau, which groups is he in?
- dev and design
- dev only
- No group at all
- design, and no longer dev
Without -a, -G replaces the whole list of supplementary groups. Pau keeps his primary group but loses dev. The safe version is usermod -aG, which appends.
4. What is the UID of root?
- 0
- 1
- 1000
- 65534
root is always UID 0, the only account the kernel never refuses. People start at 1000, and 65534 is nobody, a special account that owns nothing.
5. Why does sudo ask for your password, while su - marta asks for marta's?
- sudo checks that you are you; su needs to prove you know marta's secret
- sudo does not know the root password; su stores marta's in /etc/passwd
- sudo only works for root; su only works for users above UID 1000
- sudo encrypts your password; su sends marta's password unencrypted
sudo runs one command as root for members of the group sudo, so it only checks your own identity, and logs the command with your name. su - opens a whole session as another user, so it needs that user's password.
6. You lock aina with sudo passwd -l aina. Select the two that are true.
- Her account and her files still exist
- Root can still become aina with sudo su - aina
- Her home folder is deleted to free space
- She can still log in with her right password
Locking puts a ! in front of her hash, so no password matches. Nothing is deleted, and the lock stops the user, not the administrator.
7. Laia, a new designer, must also join dev. After sudo adduser laia, which command is right?
- sudo usermod -G dev laia design
- sudo usermod -aG design,dev laia
- sudo groupadd laia dev design
- su - laia design dev
usermod -aG takes a comma-separated list of groups and appends them, keeping laia's existing groups. groupadd creates new groups, and su only switches user.
An account answers the question "who are you?". To create accounts on the other side of the classroom you need the other shell you will meet at work: PowerShell first contact.