5.2. Managing the Account Lifecycle

Accounts are created, adjusted, suspended, and removed with a small set of tools from the passwd package (shadow-utils): useradd, usermod, userdel, groupadd, groupdel, gpasswd, passwd, and chage. All of them require root, because they write /etc/passwd, /etc/group, and /etc/shadow. Enter a root shell for this section as covered in Chapter 3 - sudo -i from an account with sudo rights, or su - where the root password is known - and leave it with exit at the end; the # marker below denotes that shell. Read-only checks that need no privilege appear with $ in john's shell.

Debian also ships the adduser wrapper, which prompts interactively, sets a password, and creates the user-private group by default. The two fronts cover the same ground:

useradd adduser
Package passwd (shadow-utils) adduser
Style Options only, no prompts Interactive questions and defaults
Home directory Only with -m Created by default
Password None set; account is locked until passwd Prompts for one
Typical use Scripts, exact control Quick one-off creation by hand

This chapter uses the low-level tools because their options map exactly onto the passwd fields being changed.

5.2.1. Creating Users and Shared Groups

The collaborative examples need one shared group and two members. Create the group first; groupadd is silent on success:

# groupadd project
# getent group project
project:x:1002:

The group received GID 1002, the next free value above the human floor of 1000. Now create sam as a member:

# useradd -m -s /bin/bash -G project sam
useradd option Description
-m Create the home directory and copy /etc/skel into it.
-s shell Set the login shell.
-G list Set supplementary groups, comma-separated.
-c comment Set the GECOS comment field.
-d dir Set the home directory (without -m, no directory is created).
-e date Set the account expiry date (Section 5.2.3).
-r Create a system account: UID below 1000, no home, no expiry.

useradd also prints nothing on success; verify both the record and the home it created:

# getent passwd sam
sam:x:1002:1003::/home/sam:/bin/bash
# stat -c "%A %a %U %G %n" /home/sam
drwx------ 700 sam sam /home/sam

The entry shows the user-private-group arrangement from Section 5.1.1: sam is UID 1002, and the automatic group sam took the next free GID, 1003, as sam's primary group. project (1002) is supplementary, and the home was created 0700 per HOME_MODE. The account exists but cannot log in yet - useradd sets no password, leaving the account locked. Set one:

# passwd sam
New password:
Retype new password:
passwd: password updated successfully

The prompts accept the password without echoing it; the confirmation line means the hash is stored. passwd -S reports the resulting state:

# passwd -S sam
sam P 2026-11-18 0 99999 7 -1

The fields, in order: the account name; the state letter (P - a usable password; Section 5.2.3 shows the others); the date the password last changed; the minimum and maximum age in days (Section 5.5.1); the days of warning before expiry; and the days of inactivity after password expiry before the account is disabled, -1 meaning never. Membership and identity can now be checked without privilege:

$ id sam
uid=1002(sam) gid=1003(sam) groups=1003(sam),1002(project)

Add john to the shared group as well:

# usermod -aG project john

The -a is not decoration. Without it, -G list replaces the complete supplementary-group list, silently dropping any group not named - a self-inflicted lockout when the dropped group is sudo. Always pair -a with -G.

$ id -Gn john
john project
# getent group project
project:x:1002:sam,john

The group now lists both members. john's existing sessions keep only john; the next login picks up project, which is why the shared examples below use fresh logins. (gpasswd -a john project and gpasswd -d john project are the dedicated membership tool for the same job.)

With membership settled, create the shared workspace itself. install -d creates a directory with explicit owner, group, and mode in one step:

# install -d -o john -g project -m 2775 /srv/handbook-project

The mode deserves attention: 2775 means the usual rwxrwxr-x plus the setgid bit, the leading 2. Section 5.3.3 explains what the bit does; the point here is that the directory is owned by john, group-writable by project, and readable by everyone else, which is the standard shape of a team directory:

$ stat -c "%A %a %U %G %n" /srv/handbook-project
drwxrwsr-x 2775 john project /srv/handbook-project

5.2.2. Changing Account Details and Moving Home Directories

Every field of the account record can be changed after creation. The GECOS comment is the simplest:

# usermod -c "Sam Rivera" sam
# getent passwd sam
sam:x:1002:1003:Sam Rivera:/home/sam:/bin/bash

The shell changes the same way. An administrator may set any shell listed in /etc/shells without the account's involvement - the reason Section 5.1.3 introduced that file:

# chsh -s /bin/sh sam
# getent passwd sam | cut -d: -f7
/bin/sh

chsh prints nothing when it succeeds. Sam's next login will run sh (dash on Debian); a running bash session is unaffected. Restore the original before continuing so the later examples behave as written:

# chsh -s /bin/bash sam
usermod option Description
-c comment Replace the GECOS comment field.
-s shell Replace the login shell.
-G list Replace the supplementary-group list; with -a, append instead.
-d dir Point the account at a new home directory.
-m With -d, move the existing home to the new path.
-e date Set the account expiry date; empty disables it.
-L / -U Lock / unlock the password field (see the note below).

Moving a home directory is the one change that touches data rather than metadata, so do it while the account is logged out: usermod -m renames the directory, and anything the user has open through the old path breaks.

# usermod -d /home/sam-projects -m sam
# getent passwd sam | cut -d: -f6
/home/sam-projects
# ls -ld /home/sam-projects
drwx------ 2 sam sam 4096 Nov 18 16:11 /home/sam-projects

The record now names /home/sam-projects, the content moved there with its 0700 mode intact, and /home/sam no longer exists - -m renames, it does not copy. The ownership shown (sam sam) is the point of doing this with usermod: a manual mv would work too, but the tool keeps the database and the filesystem consistent in one step.

All changes in this section share one property: they take effect at the next login. A session that is already running keeps its shell, its group list, and its notion of the home directory until it ends.

5.2.3. Locking, Expiring, and Removing Accounts

Suspending an account has two distinct tools because there are two distinct reasons to bar someone: their password may be compromised, or the account itself may be finished. To work through both, create the disposable account:

# useradd -m -s /bin/bash tempuser
# passwd tempuser
New password:
Retype new password:
passwd: password updated successfully
# passwd -S tempuser
tempuser P 2026-11-18 0 99999 7 -1

The account is usable; prove it from john's shell. su - tempuser starts a login for the named user and asks for that user's password - tempuser's, not john's:

$ su - tempuser -c 'id -un'
Password:
tempuser

A password lock disables password authentication for the account:

# passwd -l tempuser
passwd: password changed.
# passwd -S tempuser
tempuser L 2026-11-18 0 99999 7 -1

The state letter is now L. The message passwd: password changed. is passwd's stock confirmation for lock and unlock alike - nothing else about the password changed. What changed is visible in the hash itself, which root can inspect:

# getent shadow tempuser | cut -d: -f2 | cut -c1-3
!$y

passwd -l prepends ! to the stored hash (here !$y...), so no password can ever match it; passwd -u removes the marker again. The lock therefore blocks exactly the password path:

$ su - tempuser -c 'id -un'
Password:
su: Authentication failure

The password is unchanged - typing it correctly still fails, because the stored value is prefixed. Two properties follow from the mechanism. A lock does not kill running sessions; their identities were fixed at login. And a lock does not block other authentication methods: an account that authenticates by SSH key can still log in while locked, because the key check never consults the password field. If the goal is to bar every method, change the shell (Section 5.5.3) or expire the account entirely.

Unlocking is the mirror image:

# passwd -u tempuser
passwd: password changed.
# passwd -S tempuser
tempuser P 2026-11-18 0 99999 7 -1
$ su - tempuser -c 'id -un'
Password:
tempuser

An account expiry is the heavier tool: it disables the account as a whole, independent of how the user might authenticate. usermod -e (or chage -E, Section 5.5.1) records a date, after which the account check refuses the account:

# usermod -e 2020-01-01 tempuser
# chage -l tempuser
Last password change                    : Nov 18, 2026
Password expires                    : never
Password inactive                   : never
Account expires                     : Jan 01, 2020
Minimum number of days between password change      : 0
Maximum number of days between password change      : 99999
Number of days of warning before password expires   : 7

The date is in the past, so the account is expired now. The login attempt shows the difference from a lock - the password itself is fine, and the refusal comes from the account check with a distinct message:

$ su - tempuser -c 'id -un'
Password:
Your account has expired; please contact your system administrator.
su: Authentication failure

Clearing the expiry reactivates the account; an empty date means "never":

# usermod -e "" tempuser
# chage -l tempuser
Last password change                    : Nov 18, 2026
Password expires                    : never
Password inactive                   : never
Account expires                     : never
Minimum number of days between password change      : 0
Maximum number of days between password change      : 99999
Number of days of warning before password expires   : 7
$ su - tempuser -c 'id -un'
Password:
tempuser

Removal is destructive and unrecoverable: userdel -r deletes the account record and the home directory with everything in it. The practice account here holds nothing of value, which is what disposable accounts are for.

# userdel -r tempuser
userdel: tempuser mail spool (/var/mail/tempuser) not found

The mail-spool note on standard error is a complaint about something that never existed, not a failure of the removal - tempuser never received mail. Verify that both the record and the home are gone:

# getent passwd tempuser

No output, and getent exits with status 2 when the name is unknown: the account no longer resolves through NSS. Without -r, userdel would leave the home directory behind, owned by an ID that no longer has a name.

The same trio of levers - lock for a compromised password, expiry for a finished account, removal to erase it - scales from this disposable example to real ones. One further rule is worth stating while accounts are the subject: userdel refuses to remove a user who is currently logged in, and it is right to refuse; log the sessions out first, or schedule the removal.

(usermod also has -L and -U for locking and unlocking; they use the same ! mechanism. The passwd forms are the better-known ones and the demonstrated path here, so the chapter stays with them.)