User and group accounts

Accounts and groups: the four files that hold them, the commands that edit those files safely, and what actually happens to a home directory when an account is created or removed.

Lesson 1 of 3 in objective 107. Administrative tasks, part of LPIC-1 Exam 102-500.

The seven fields of an /etc/passwd line. dennis:x:1000:1000:Dennis:/home/dennis:/bin/sh — part 1, dennis: login name; part 2, x: a placeholder. The hash is in /etc/shadow; part 3, 1000: UID. 0 is root whatever the name says; part 4, 1000: primary GID — not listed in /etc/group; part 5, Dennis: the comment, or GECOS, field; part 6, /home/dennis: home directory; part 7, /bin/sh: login shell. /sbin/nologin refuses one. 1 dennis : 2 x : 3 1000 : 4 1000 : 5 Dennis : 6 /home/dennis : 7 /bin/sh 1 login name 2 a placeholder. The hash is in /etc/shadow 3 UID. 0 is root whatever the name says 4 primary GID — not listed in /etc/group 5 the comment, or GECOS, field 6 home directory 7 login shell. /sbin/nologin refuses one
The seven fields of an /etc/passwd line.

Four files, and which one holds the password

/etc/passwd has one line per account with seven colon-separated fields: name, an x standing in for the password, UID, GID, the comment or GECOS field, home directory, and login shell. It is world-readable, which is exactly why the password is not in it. /etc/shadow holds the hashed password and the ageing fields — last change, minimum and maximum age, warning period, inactivity, expiry — and is readable only by root.

/etc/group lists groups with their GIDs and their supplementary members, and /etc/gshadow holds group passwords and administrators. A user's PRIMARY group is the GID in their passwd line and is not listed in /etc/group as a member; supplementary groups are. id prints both, groups prints the names, and getent looks an entry up through whatever name service is configured rather than only in the local file — which matters the moment LDAP is involved.

Where each piece of user and group data lives across the four files. Left column, What you are looking for; right column, The file that holds it. A UID, a primary GID and A home directory, a login shell both point at /etc/passwd (World-readable, which is exactly why no password is in it). The hashed password and Ageing: last change, minimum, maximum, expiry both point at /etc/shadow (Readable only by root). A group's own GID and Supplementary group members both point at /etc/group (A primary group is not listed here as a member). Group passwords and Group administrators both point at /etc/gshadow. What you are looking for The file that holds it A UID, a primary GID A home directory, a login shell /etc/passwd World-readable, which is exactly why no password is in it The hashed password Ageing: last change, minimum, maximum, expiry /etc/shadow Readable only by root A group's own GID Supplementary group members /etc/group A primary group is not listed here as a member Group passwords Group administrators /etc/gshadow
Where each piece of user and group data lives across the four files.

The commands, and what they do to the filesystem

useradd creates an account, with -m to create the home directory (copied from /etc/skel), -s for the shell and -G for supplementary groups; adduser on Debian systems is an interactive wrapper over it. usermod changes an existing account — and the trap is that usermod -G REPLACES the supplementary group list, so adding a group means usermod -aG, with the -a for append. userdel removes the account and userdel -r also removes the home directory and mail spool.

groupadd, groupmod and groupdel do the same for groups. passwd sets a password; passwd -l locks an account by prefixing the hash with a character that can never match, and passwd -u unlocks it. chage manages the ageing fields — chage -l lists them — and setting a shell of /sbin/nologin or /bin/false is the other way to stop an account being used interactively while leaving it functional for a service.

gpasswd is the one that maintains /etc/gshadow, and it is how group membership is delegated without handing out root or writing a sudo rule: gpasswd -A alice developers makes alice an ADMINISTRATOR of that group, recorded in /etc/gshadow, after which she may add and remove members herself. Its other flags act on the membership rather than the delegation — -a adds one member and -d removes one, -M replaces the member list outright, and gpasswd with only a group name sets the group password that newgrp asks for. /etc/gshadow is to /etc/group what /etc/shadow is to /etc/passwd: the root-only companion file.

The two fields a user might reasonably want to change themselves have their own commands. chfn -f "Alice Okafor" alice writes the fifth field of the /etc/passwd line — the GECOS or comment field, conventionally full name, room, work phone and home phone, and where a mail client picks up a display name — and chsh -s /bin/bash alice writes the seventh, the login shell. Both are setuid so that a user may correct their own entry. usermod does the same two jobs from the administrator's side with -c and -s, replacing the whole field rather than editing named parts of it, so usermod -c and chfn -f are two correct answers to the same question.

A UID of 0 means root, whatever the account is called; there is nothing else special about the name. System accounts conventionally take low UIDs, with the boundary set in /etc/login.defs.

Four commands in the life of an account, and what each one changes. In order: useradd -m dennis (a new /etc/passwd line; -m copies a home from /etc/skel), then passwd dennis (the hash goes to /etc/shadow, never to /etc/passwd), then usermod -aG sudo dennis (appends a group; plain -G replaces the list), then userdel -r dennis (-r also takes the home directory and mail spool). useradd -m dennis a new /etc/passwd line; -m copies a home from /etc/skel passwd dennis the hash goes to /etc/shadow, never to /etc/passwd usermod -aG sudo dennis appends a group; plain -G replaces the list userdel -r dennis -r also takes the home directory and mail spool
Four commands in the life of an account, and what each one changes.

Worth carrying in

/etc/passwd
name:x:UID:GID:comment:home:shell. World-readable; no password in it.
/etc/shadow
Hashed passwords and ageing. Root-only.
useradd -m -s /bin/bash
Create an account with a home directory from /etc/skel.
usermod -aG group user
ADD a supplementary group. Without -a, the list is replaced.
userdel -r
Delete the account AND its home directory and mail spool.
passwd -l / -u
Lock and unlock an account.
chage -l user
Password ageing settings for an account.
getent passwd user
Look up through the name service, not just the local file.
id / groups
UID, primary GID and supplementary groups for a user.
gpasswd -A user group
Delegate group administration. Written to /etc/gshadow; -a and -d add and remove members.
chfn -f "Name" user
Set the GECOS field. chsh -s sets the shell; usermod -c and -s do both as root.

What the exam does with this

Objective
107. Administrative tasks
Share of the exam
20% (the whole objective)
Questions in this lesson
25
Signed for by a person
0

Partly checked. None of the 25 questions here has been read against the cited source by a person. 25 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.

Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.

How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.

Drill this lesson

A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.

Practise User and group accounts

Questions in this lesson

Practise User and group accounts

The rest of objective 107