Linux Users, Groups, sudoers, and PAM Access Control
The four account files, the sudoers grammar with aliases and drop-ins, and the PAM stack where logins actually succeed or fail. Includes the flags that lock an account without disabling SSH keys.

Quick Summary
Linux access control is four files and one binary. /etc/passwd holds accounts and their primary group, /etc/shadow holds the password hash and aging fields readable only by root, /etc/group holds membership, and /etc/gshadow holds group passwords that almost nobody uses. useradd, usermod, groupadd, and passwd edit them through the system's locking and PAM hooks; editing the files by hand skips both.
sudo is the delegation layer. A sudoers rule reads as who where = (as whom) what, so deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart app grants one command to one user on one host. The file is parsed by a grammar where a typo can lock every administrator out of sudo, which is why visudo exists: it takes a lock, drops you into an editor, validates the syntax on save, and refuses to write a broken file.
PAM sits underneath both. Every login path — login, sshd, su, sudo, cron — names a service file in /etc/pam.d/ and executes its module stack, which is where password hashing, account expiry, session limits, home-directory creation, and failed-login lockouts actually happen. This guide covers the account and group commands, the sudoers grammar with its aliases and safe drop-ins, PAM stack control flags, and the failure modes that leave a server with no working administrator.
Basic Syntax & Core Signatures
# Accounts
useradd -m -s /bin/bash -G docker,www-data deploy # create with home and groups
useradd -r -s /usr/sbin/nologin svc # system account, no login
usermod -aG sudo deploy # APPEND to supplementary groups
usermod -aG docker deploy # -G alone would REMOVE the others
usermod -L deploy # lock (prefix '!' to the hash)
usermod -U deploy # unlock
usermod -e 2026-12-31 deploy # account expiry date
usermod -d /srv/deploy -m deploy # move the home directory
userdel -r deploy # delete account and home
chage -l deploy # password aging report
chage -M 90 -W 7 deploy # max age, warning days
passwd deploy # set password (as root)
passwd -S deploy # status: P (usable), L (locked), NP
# Groups
groupadd -g 1500 deploy
gpasswd -a deploy www-data
gpasswd -d deploy www-data
getent group www-data
id deploy # uid, gid, all groups
# Read the databases as the tools do (NSS-aware: also covers LDAP/SSSD)
getent passwd deploy
getent shadow deploy # root only
getent group docker# sudoers: always through visudo, never with a plain editor
visudo # /etc/sudoers
visudo -f /etc/sudoers.d/deploy
visudo -c # syntax check all files, write nothing
# who where = (as whom) what
deploy ALL = (root) /usr/bin/systemctl restart app
%developers ALL = (root) NOPASSWD: /usr/bin/docker
backup ALL = (root) NOPASSWD: /usr/bin/rsync, /usr/bin/tar
alice web01 = (postgres) /usr/bin/psql# Aliases (must be defined before use in the same file)
User_Alias ADMINS = alice, bob, %wheel
Cmnd_Alias SERVICES = /usr/bin/systemctl restart app, /usr/bin/systemctl status app
Runas_Alias DB = postgres
ADMINS ALL = (DB) NOPASSWD: SERVICES
# Defaults that matter
Defaults env_reset
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Defaults env_keep += "LANG LC_* EDITOR"
Defaults timestamp_timeout=5
Defaults !visiblepw
Defaults passwd_tries=3
# Inspect what is actually granted
sudo -l
sudo -ll
sudo -U deploy -l # as root, list another user's rules# PAM: stack order and control flags for one service
cat /etc/pam.d/sshd
#auth required pam_env.so
#auth include common-auth
#account include common-account
#password include common-password
#session required pam_limits.so
#session optional pam_mkhomedir.so skel=/etc/skel umask=0022Control flag semantics: required must succeed but failures are reported only at the end of the stack; requisite fails immediately; sufficient succeeds the rest of the stack only if no earlier required failed; optional is ignored unless it is the only module; include inserts another service file; substack is like include but jumps back.
Key Comparison Table
| Aspect | /etc/passwd | /etc/shadow | /etc/group |
|---|---|---|---|
| Readable by | Everyone | root only | Everyone |
| Holds | name:uid:gid:gecos:home:shell | hash, aging, expiry | group:gid:members |
| Edited by | useradd, usermod, vipw | passwd, chage, vipw -s | groupadd, gpasswd |
| Password field | x when shadowed | !/* locked, !! never set | rarely used |
| Mechanism | Grants | Persistence | Notes |
|---|---|---|---|
su - | Full shell as another user | Per invocation | Needs the target's password |
sudo | Per-command, per-host | /etc/sudoers | Audited in syslog/auth.log |
| Supplementary groups | File access | /etc/group | Effective at next login |
| File ACLs | Per-file, per-user | Filesystem | getfacl, setfacl |
| Capabilities | Specific kernel privileges | File xattr | setcap cap_net_bind_service=+ep |
| Command / flag | Effect |
|---|---|
visudo -c | Validate syntax without writing |
sudo -l | Rules for the invoking user |
sudo -U user -l | Rules for another user (root only) |
sudo -i | Login shell as root with root's environment |
sudo -s | Shell as root, keeps your environment |
sudo -E | Preserve environment (needs SETENV) |
sudo -u postgres | Run as another user (needs (postgres) in the rule) |
NOPASSWD: | No password prompt for the commands that follow |
!command | Deny, but later matching rules can still allow |
Practical Edge Cases & Deep Dives
1. usermod -G without -a removes supplementary groups
$ id deploy
uid=1001(deploy) gid=1001(deploy) groups=1001(deploy),27(sudo),998(docker)
$ sudo usermod -G www-data deploy # replaces the whole supplementary list
$ id deploy
uid=1001(deploy) gid=1001(deploy) groups=1001(deploy),33(www-data)-G sets the supplementary group list; -aG appends. Losing sudo this way is a common outage in onboarding scripts, and it takes effect only in new sessions, so the current SSH connection still works while everything new is broken. Check with id or getent group after every membership change, and remember group membership is resolved at login: newgrp or a re-login is required, and a service started before the change keeps its old groups.
2. Wildcards in sudoers grant more than they look like
# Intended: restart the app. Actually: run any systemctl subcommand on any unit.
deploy ALL = (root) NOPASSWD: /usr/bin/systemctl *
# Intended: edit this config. Actually: a root shell via vi's :! command.
deploy ALL = (root) NOPASSWD: /usr/bin/vi /etc/app/config.ymlsudoers argument matching is glob-based on the whole command line, and any program that can spawn a shell or write arbitrary paths defeats the restriction: vi, less, find -exec, awk, tar --to-command, systemctl with edit/link, env, and every interpreter. The workable patterns are a wrapper script owned by root and not writable by the user, or precise argument lists:
Cmnd_Alias APPCTL = /usr/bin/systemctl restart app.service, \
/usr/bin/systemctl status app.service, \
/usr/bin/systemctl is-active app.service
deploy ALL = (root) NOPASSWD: APPCTL3. Drop-in files in /etc/sudoers.d that are silently ignored
$ ls -l /etc/sudoers.d/
-rw-r--r-- 1 root root 120 deploy.conf
-rw-r--r-- 1 root root 98 deploy.conf.bak # ignored: contains a '.'
-rw-r--r-- 1 root root 120 deploy~ # ignored: ends with '~'sudo skips files in the included directory whose names contain a dot or end in a tilde, as a guard against editor backups. Permissions matter too: a file that is group- or world-writable is refused, and on Debian the include line is #includedir /etc/sudoers.d — if that line is commented out, every drop-in is inert while visudo -c still reports success on the files themselves. visudo -c lists which files it parsed, which is how you confirm the drop-in was read.
4. Locking an account: password versus login
passwd -l deploy # '!' prefix on the hash — blocks password auth
usermod --expiredate 1 deploy # account expired 1970-01-02 — blocks all PAM account checks
usermod -s /usr/sbin/nologin deploy # shell refuses interactive loginThese are not equivalent. A locked password still permits SSH key authentication, because key auth never verifies the hash; passwd -l is therefore the wrong tool for "disable this account". --expiredate 1 makes pam_unix's account module fail and is the reliable switch, while nologin gives a clear message and keeps cron jobs working if they do not need a shell. Check the resulting state rather than assuming:
passwd -S deploy # deploy L 11/15/2026 0 99999 7 -1
getent shadow deploy | cut -d: -f1,2 | cut -c1-205. PAM: where a login actually fails
$ sudo tail -n 20 /var/log/auth.log
sshd[2291]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0
sshd[2291]: Failed password for deploy from 203.0.113.7 port 51422 ssh2The pam_unix(sshd:auth) tag names the service and the module group, which localizes the failure to one layer: auth (proof of identity), account (expiry, time restrictions, pam_access), session (limits, home directory, SELinux context), or password (change rules). A login that authenticates and then immediately closes usually failed in account or session — /etc/security/access.conf, an expired account, pam_limits nproc of zero, or a missing home directory with pam_mkhomedir not installed.
# Common additions, each in the right group
session required pam_mkhomedir.so skel=/etc/skel umask=0022
session required pam_limits.so
account required pam_faillock.so # RHEL family; configure /etc/security/faillock.conf
auth required pam_faillock.so preauth silent deny=5 unlock_time=900Editing /etc/pam.d/common-auth wrongly can lock out sudo as well as SSH. Keep a second root session open, test with su - from a non-root account before disconnecting, and prefer distribution-provided helpers (pam-auth-update on Debian) over hand edits.
6. Auditing who can do what
# Who has a usable password and a login shell?
getent passwd | awk -F: '$3>=1000 && $7 !~ /nologin|false/ {print $1, $7}'
# Who is in sudo/wheel/docker? (docker membership is root-equivalent)
for g in sudo wheel docker; do echo "== $g"; getent group $g; done
# What does sudo actually grant, including includes?
sudo -ll | sed -n '1,80p'
grep -rn '' /etc/sudoers /etc/sudoers.d/ 2>/dev/null | grep -v '^.*:#'
# Recent privilege escalations
journalctl -u sudo --since '24 hours ago' 2>/dev/null | tail -20Membership in docker, lxd, disk, adm, and wheel/sudo is effectively root access, since each can mount or read the host filesystem. An audit that only looks at sudoers misses most real escalation paths.
Common Runtime & Configuration Mistakes
| Symptom | Cause | Fix |
|---|---|---|
sudo: parse error in /etc/sudoers and no sudo at all | Hand-edited syntax error | Boot to recovery/console, visudo to fix, keep a root session |
| Drop-in rules have no effect | File name contains . or ends in ~, or the include line is commented | Rename, verify with visudo -c |
sudo asks for the target's password | Defaults targetpw or rootpw | Remove the default, or accept the behaviour |
sudo: sorry, you must have a tty | requiretty on old configurations | Remove Defaults requiretty |
| Command not allowed despite the rule | Path is relative (systemctl not /usr/bin/systemctl) | Use absolute paths |
| Allowed command can still get a shell | Wildcard or shell-capable program in the rule | Wrapper script with exact arguments |
| Group change not effective | Membership is read at login | Re-login, or restart the service |
Removed from sudo after usermod -G | Missing -a | usermod -aG sudo user, verify with id |
| Account disabled but SSH keys still work | passwd -l only blocks password auth | --expiredate 1 or nologin shell |
useradd created no home | CREATE_HOME off in /etc/login.defs | -m, or set the default |
chage shows a password that never expires | 99999 max days | chage -M 90 |
su fails but SSH works | su needs the target's password (or wheel + PAM pam_wheel) | Use sudo -i, or grant via pam_wheel.so |
Accounts, groups, sudoers, and PAM are one access path, not four: a login passes through PAM with the account's shadow entry and groups, and then optionally through sudo with its own grammar and timestamp. Debugging is fastest from the bottom up — id, passwd -S, getent group, sudo -l, then the pam_* line in the auth log — and every change to the top two layers should be made with visudo and tested from a second session that is already open.
Try matching browser tools
All toolsSmall utilities that fit Linux work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Linux guides
All Linux guidesBash Indexed Arrays, Associative Maps, and Parameter Expansion
Quoting decides whether an array stays a list, and the expansion operators replace most external text tools. Includes set -u, mapfile, namerefs, and the bash 3.2 differences that break scripts on macOS.
Parsing JSON and YAML in Shell Pipelines with jq and yq
jq is a filter language, not a pretty-printer. The operators that do the work, --arg versus string interpolation, in-place YAML edits, and how to tell which yq is installed before writing a command.
Linux Memory, Swap, the OOM Killer, vmstat, and sysctl Tuning
MemAvailable versus used, the vmstat columns that prove thrashing, how the OOM killer picks its victim, and the sysctl keys that belong in /etc/sysctl.d instead of a one-off command.
Linux Firewalls: ufw, iptables, and nftables Rules and Persistence
First-match ordering, stateful filtering, and persistence: the three ideas behind every Linux firewall. Plus why Docker-published ports ignore ufw and how DOCKER-USER closes them.
More in Linux
All Linux guidesThe guides filed next to this one in Linux, in library order.