5.1. Understanding Local Identities

The kernel does not know user names. A process carries a user ID (UID), a primary group ID (GID), and a list of supplementary group IDs, all plain integers assigned at login and inherited by every process that login starts. Names are a lookup layer: when ls prints john project, it has asked the system to translate the numbers 1001 and 1002 into those names. Understanding which numbers a process carries, and which database answers the translation, is the foundation for everything else in this chapter.

5.1.1. User IDs, Group IDs, and Supplementary Groups

The most direct view of an identity is id, run here in john's own login shell:

$ id
uid=1001(john) gid=1001(john) groups=1001(john)

Three numbers describe the process. The UID, 1001, is john's user ID; the GID, 1001, is the primary group, assigned to every process john starts; and groups=1001(john) is the complete list of supplementary groups - here only john's own. id accepts options that select pieces of this report, and they are worth knowing because later examples use them:

id option Description
-u name Print only the numeric UID of name.
-g name Print only the numeric primary GID of name.
-G name Print all group IDs of name, primary first.
-n With -u, -g, or -G, print names instead of numbers.
(no argument) Report the calling process's own identity.

Names and numbers are connected by the account databases. getent queries them directly, without going through a login:

$ getent passwd john
john:x:1001:1001::/home/john:/bin/bash

The seven colon-separated fields of a passwd entry are:

Field john's value Meaning
1 john Login name.
2 x Password marker; the hash itself lives in /etc/shadow (Section 5.5.1).
3 1001 UID.
4 1001 Primary GID.
5 (empty) GECOS comment field: full name, office, phone.
6 /home/john Home directory.
7 /bin/bash Login shell.

Group membership is stored the same way:

$ getent group john
john:x:1001:
$ getent group sudo
sudo:x:27:

A group entry has four fields: the group name, the password marker x, the numeric GID, and a comma-separated list of members - accounts for which this group is a supplementary group. The member list is empty in both captures above: john's group has no members because john reaches it as a primary group, and the sudo list is empty at this point because john's account is deliberately outside the sudo group while this chapter's delegation examples run (Section 5.4), so the restricted policy there is the whole of what john can do. Chapter 2 showed this same line ending in :john immediately after installation, where the first user is a member; on such a system the membership grants every command, which is exactly why a restricted policy needs an account that does not have it.

Which groups a login actually receives is worth a separate check, because it combines the primary group with the supplementary list:

$ id -Gn john
john
$ getent initgroups john
john                 

getent initgroups resolves the supplementary groups a login would set up for the named user, using the same NSS path as a real login - the account name appears first, padded to a fixed-width field, followed by the IDs of the groups beyond the primary; here only john's padded name appears, with no IDs after it, because the account has no supplementary groups yet. The distinction matters when things go wrong: id in a running shell reports the groups that shell was given at login, while id -Gn john reports what a new login would receive. Adding a group to an account does not reach into existing sessions; the member needs to log in again.

The boundary between system and human accounts is fixed by ranges in /etc/login.defs:

$ grep -E "^(UID_MIN|UID_MAX|SYS_UID_MIN|SYS_UID_MAX|GID_MIN|GID_MAX|CREATE_HOME|HOME_MODE|UMASK|ENCRYPT_METHOD)" /etc/login.defs
HOME_MODE   0700
UID_MIN          1000
UID_MAX         60000
GID_MIN          1000
GID_MAX         60000
ENCRYPT_METHOD YESCRYPT

Human accounts are allocated from 1000 upward, and the same file sets two other defaults used throughout this chapter: HOME_MODE 0700 is the permission mode for new home directories, and ENCRYPT_METHOD YESCRYPT is the password hashing scheme (Section 5.5.1). Accounts below 1000 are system accounts, conventionally taken from 100 to 999, created by packages with useradd -r rather than by administrators; nothing prevents a human UID of 500 except that the convention keeps the two worlds separable when you audit who may log in.

One consequence of the ranges the captures already show: john is 1001 and john's group is also 1001, and sam in Section 5.2.1 will be UID 1002 with primary group 1003. Debian creates a user private group - a group with the same name as the user - for each human account, so the account's files are group-owned by a group containing only that user. That arrangement, combined with a group-writable umask, lets each user's files stay private by default while shared directories work without per-file chmod; Section 5.3.3 builds on it.

5.1.2. Account Databases, NSS, and System Accounts

getent does not read /etc/passwd directly. Every name lookup - whether from ls, login, sshd, or getent - goes through the Name Service Switch, configured in /etc/nsswitch.conf:

$ grep -E "^(passwd|group|shadow):" /etc/nsswitch.conf
passwd:         files systemd
group:          files systemd
shadow:         files systemd

Each line names a database and the sources to consult, in order. On this system files comes first: /etc/passwd, /etc/group, and /etc/shadow answer before anything else. The systemd source after it resolves users and groups that systemd manages dynamically - a service unit with DynamicUser= gets a synthetic account, visible to lookups while it runs, without any entry in /etc/passwd. Chapter 8 uses that mechanism; for account administration the practical point is that a name resolving through getent may be real and local, or provided by a later source, and getent is the tool that shows you what all callers currently see.

The databases themselves are four files, and their permissions are part of the security design:

File Contents Mode and owner
/etc/passwd Names, UID/GID, home, shell. 0644 root:root, world-readable.
/etc/group Group names, GIDs, member lists. 0644 root:root, world-readable.
/etc/shadow Password hashes and aging fields. 0640 root:shadow.
/etc/gshadow Group administrator and member passwords. 0640 root:shadow.

Separating the hash from the public account record is what allows passwd to be world-readable: names, IDs, and shells are not secrets, and dozens of programs from ls to mail daemons read them on every call. The hashes are readable only by root and the shadow group, and Section 5.5.1 shows both sides of that boundary. The numbers bear this out - a stock installation carries far more accounts than people:

$ getent passwd | wc -l
39
$ getent passwd | grep ':/home/'
vagrant:x:1000:1000::/home/vagrant:/bin/bash
john:x:1001:1001::/home/john:/bin/bash

Thirty-nine accounts exist, and two have homes under /home: the machine's provisioning account and john. The rest are system accounts, and two of them are worth recognizing:

$ getent passwd daemon
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
$ getent passwd nobody
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin

daemon (UID 1) is the historical unprivileged service account; nobody (UID 65534) is the conventional account for processes that should map to "no one" - NFS file ownership with unknown IDs, for example, lands here. Both use the shell /usr/sbin/nologin, which prints one line and exits: it exists precisely so that an account can own files and run daemons but never host an interactive session. Section 5.5.3 uses it deliberately. Figure 5.1 shows how the pieces connect.

Account names, NSS, and the account databases

Figure 5.1. Every caller resolves names through NSS; the kernel authorizes by numeric ID. Account tools write the files, and the next lookup - not any running process - sees the change.

Two properties follow from this arrangement. First, account changes are immediately visible to new lookups: add a group, and the very next getent group reflects it. Second, nothing changes for processes already running: their UID and group list were fixed at exec time, which is why group membership takes effect at the next login and why a locked account (Section 5.2.3) does not kill existing sessions.

5.1.3. Home Directories and Login Shells

A new account's home directory is created with the mode from HOME_MODE - 0700 on this installation - and the privacy that implies is real, not decorative:

$ stat -c "%A %a %U %G %n" /home/john
drwx------ 700 john john /home/john

stat -c selects fields with a format string: %A is the symbolic mode, %a the octal mode, %U and %G the owning user and group names, and %n the file name. The mode 700 gives john full control and everyone else nothing - not even the right to enter the directory, which is the more subtle point. Permission bits on a directory mean different things than on a file, and one of them, execute, means traverse: the right to pass through the directory on the way to something inside it. With drwx------, no other account can reach /home/john/anything regardless of what the inner permissions are. namei -l walks a path component by component and shows this directly:

$ namei -l /home/john/debian-handbook/accounts-permissions
f: /home/john/debian-handbook/accounts-permissions
drwxr-xr-x root root /
drwxr-xr-x root root home
drwx------ john john john
drwxr-xr-x john john debian-handbook
drwxr-xr-x john john accounts-permissions

Every step grants the traversal it needs - until /home/john, whose 700 lets only john through. Any shared workspace must therefore live outside the homes; that is why the collaborative examples in this chapter use /srv/handbook-project rather than a directory under /home/john. Section 5.5.4 returns to namei -l as a diagnostic.

Create the chapter's private workspace now; mkdir -p prints nothing on success:

$ mkdir -p ~/debian-handbook/accounts-permissions

The namei -l output above already confirms it exists (drwxr-xr-x john john accounts-permissions). The parent ~/debian-handbook was introduced in Chapter 3 and is reused here so each chapter's practice files stay separate.

The last two passwd fields describe where a login lands and what it runs:

$ getent passwd john | cut -d: -f6,7
/home/john:/bin/bash
$ echo "$SHELL"
/bin/bash

$SHELL records the login shell the session was started with. Changing it is a normal account operation with a built-in safety: an ordinary user runs chsh, which asks for their password and accepts only shells listed in /etc/shells - a file that exists so that a typo or a malicious entry cannot leave an account with a broken or trapped shell:

$ grep -v "^#" /etc/shells
/bin/sh
/usr/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/usr/bin/dash

An administrator can change any account's shell without that account's password; Section 5.2.2 demonstrates it and Section 5.5.3 uses it to close an account to interactive logins entirely.