Linux › Section G · System configuration › Topic G.7
User IDs and user access
The kernel does not know names, only numbers. Understanding how a login becomes a set of numeric IDs, how a process can hold more than one of them, and how to see who is on the machine is the key to explaining both everyday permissions and privilege escalation.
Numbers underneath the names
Every process runs with a numeric user ID (UID) and group IDs (GIDs), and every file records its owner and group by number. Names exist for humans and are looked up in tables such as /etc/passwd and /etc/group. This is why a file copied from another machine may show a strange owner number, or the wrong person's name: the number is preserved but the table on this machine maps it differently. The kernel makes every permission decision from the numbers alone. Permission bits are covered in the topic File modes and permissions; this topic is about where the numbers come from.
Looking at identities: id, whoami, groups
id prints the real UID, primary GID and supplementary groups: for example uid=1001(ana) gid=1050(ana) groups=1050(ana),27(sudo),1100(projx) (illustrative). id -u prints only the UID, id -un only the name, and id sam looks at another user. whoami prints the name for the current effective user, and groups lists group names. To look up an account whatever its source, use getent passwd ana or getent group projx. getent follows the same configuration that programs use, so it finds accounts in LDAP or other directories as well as in the local files.
Where lookups come from: nsswitch
Programs call library functions such as getpwnam and getpwuid, which consult the Name Service Switch. /etc/nsswitch.conf lists, for each database, the sources to try in order, for example passwd: files systemd or passwd: files sss. Sources are tried left to right until one answers. This is why a centrally managed account can appear on a machine that has no entry in /etc/passwd for it. It is also why you should never parse /etc/passwd by hand in a script that must work on every system: use getent or the system's tools, which respect the whole configuration.
Worked example: reading id to predict access
A file is listed as -rw-rw---- 1 root projx 4096 notes.txt, and id shows uid=1001(ana) gid=1050(ana) groups=1050(ana),1100(projx). Can Ana write to it? 1. The owner is root and Ana is UID 1001, so the owner set does not apply. 2. The file's group is projx, GID 1100 in the groups list, so the group set applies. 3. The group set is rw-, so Ana can read and write. 4. Now take Lee, who has groups=1060(lee) only: Lee matches neither owner nor group, so the others set applies, which is ---, and access is denied. The kernel uses the first matching set only, so check owner, then group, then others.
Real, effective and saved UIDs
A process carries more than one UID. The real UID identifies who started it. The effective UID is the one the kernel checks when deciding what the process may do. The saved UID remembers a previous effective ID so a process can drop privilege and later regain it. Normally real and effective match. A setuid program breaks the match: ls -l /usr/bin/passwd shows -rwsr-xr-x, the s standing in the owner's execute position (mode 4755), so when Ana runs it her real UID stays 1001 while its effective UID becomes root's 0. That lets her change her own password hash in /etc/shadow. ps -o pid,ruid,euid,comm shows both columns.
Switching users: su and sudo
su USER starts a shell as another user and asks for that user's password, which for root means the root password; su - USER also starts a login shell with that user's environment. sudo COMMAND runs one command with another identity, root by default, after asking for your own password and checking the sudoers policy (see Running commands as the superuser). sudo -u ana COMMAND runs as Ana; sudo -i opens a root login shell. sudo has two practical benefits: individuals never share the root password, and each use is logged, normally in the journal or an authentication log. Prefer sudo for single commands.
Who is on the system: who, w, last
who lists logged-in users with their terminal, login time and origin. w adds a summary line with the time, uptime, user count and load averages, and shows what each user is running. last reads the login history file, /var/log/wtmp, newest first: last -n 10 limits the output, last ana filters by user, and last reboot lists system restarts. lastb reads /var/log/btmp for failed attempts and needs root. lastlog shows each account's most recent login. Because these files are not 2038-safe, Debian 13 (2025) replaced them: wtmpdb's last reads /var/log/wtmp.db, lastlog2 replaces lastlog, and lslogins --failed stands in for lastb. These tools show logins recorded by login, sshd and the graphical session, not every process that a user started.
Worked example: reading login records
Here are illustrative lines from last. ana pts/0 203.0.113.9 Fri Nov 6 09:12 still logged in. sam pts/1 203.0.113.45 Thu Nov 5 22:01 - 22:40 (00:39). reboot system boot Mon Nov 2 07:55 still running. 1. Ana is connected now, over the network from 203.0.113.9 (a documentation address), on pseudo-terminal pts/0. 2. Sam logged in on 5 November at 22:01 and out at 22:40, a session of 22:40 - 22:01 = 39 minutes, matching (00:39). 3. The machine last booted on 2 November at 07:55 and has been running since. 4. If a login appears that nobody can explain, note its source address and time, then look in the authentication log or journalctl -u sshd for that window.
Identify, authenticate, authorise
Access control has three separate questions. Identification: who does the user claim to be, a name typed at a prompt. Authentication: can they prove it, with a password, a key or a token? Authorisation: what is this identity allowed to do? On Linux, login programs and daemons delegate authentication to PAM, covered in the next topic, then set the process's UIDs and groups; after that, file permissions, sudo rules and similar policies authorise each action. Keeping the stages distinct helps debugging: a failed login is an authentication problem, while 'Permission denied' on a file is an authorisation problem.
Common mistakes to avoid
First, assuming names, not numbers, define identity; the same name can have different UIDs on two machines. Second, grepping /etc/passwd to find a user who comes from a directory service; use getent. Third, believing a group change applies to an open session; log in again or use newgrp. Fourth, using su - when you meant a single command; sudo is more auditable. Fifth, reading whoami as the real user in a setuid context. Sixth, treating last as a complete audit trail, since records can be rotated or tampered with by root.
Key takeaways
The kernel checks numeric UIDs and GIDs; names are looked up through nsswitch, so getent finds directory accounts that grep misses. id shows UID, GID and groups. A process has real, effective and saved UIDs, and a setuid program runs with the file owner's effective UID. su asks for the target's password, sudo for yours and logs the use. who and w show who is on now; last, lastb and lastlog show history from wtmp and btmp. Identification, authentication and authorisation are separate steps.
Flashcards
Flashcard
Numeric user and group IDs used by the kernel
Test your understanding
Question 1 of 5
A file is -rw-rw---- owned by root with group projx (GID 1100). id shows uid=1001(ana) gid=1050(ana) groups=1050(ana),1100(projx). What access does Ana have?
Question 2 of 5
Ana runs /usr/bin/passwd, which is setuid root. What are the process's real and effective UIDs?
Question 3 of 5
Which command shows the history of logins, including system reboots, from /var/log/wtmp?
Question 4 of 5
You run sudo systemctl restart nginx. Whose password is requested by default?
Question 5 of 5
A user's account lives in an LDAP directory. Why does grep ana /etc/passwd find nothing while getent passwd ana works?
