Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readLinux
#Linux#Security#SysAdmin
3D illustration of user and group badges passing a layered PAM authentication gate to a sudo privilege lock

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

bash
# 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
bash
# 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
bash
# 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
bash
# 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=0022

Control 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 byEveryoneroot onlyEveryone
Holdsname:uid:gid:gecos:home:shellhash, aging, expirygroup:gid:members
Edited byuseradd, usermod, vipwpasswd, chage, vipw -sgroupadd, gpasswd
Password fieldx when shadowed!/* locked, !! never setrarely used
MechanismGrantsPersistenceNotes
su -Full shell as another userPer invocationNeeds the target's password
sudoPer-command, per-host/etc/sudoersAudited in syslog/auth.log
Supplementary groupsFile access/etc/groupEffective at next login
File ACLsPer-file, per-userFilesystemgetfacl, setfacl
CapabilitiesSpecific kernel privilegesFile xattrsetcap cap_net_bind_service=+ep
Command / flagEffect
visudo -cValidate syntax without writing
sudo -lRules for the invoking user
sudo -U user -lRules for another user (root only)
sudo -iLogin shell as root with root's environment
sudo -sShell as root, keeps your environment
sudo -EPreserve environment (needs SETENV)
sudo -u postgresRun as another user (needs (postgres) in the rule)
NOPASSWD:No password prompt for the commands that follow
!commandDeny, but later matching rules can still allow

Practical Edge Cases & Deep Dives

1. usermod -G without -a removes supplementary groups

bash
$ 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

sudoers
# 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.yml

sudoers 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:

sudoers
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: APPCTL

3. Drop-in files in /etc/sudoers.d that are silently ignored

bash
$ 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

bash
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 login

These 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:

bash
passwd -S deploy # deploy L 11/15/2026 0 99999 7 -1 getent shadow deploy | cut -d: -f1,2 | cut -c1-20

5. PAM: where a login actually fails

bash
$ 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 ssh2

The 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.

bash
# 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=900

Editing /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

bash
# 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 -20

Membership 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

SymptomCauseFix
sudo: parse error in /etc/sudoers and no sudo at allHand-edited syntax errorBoot to recovery/console, visudo to fix, keep a root session
Drop-in rules have no effectFile name contains . or ends in ~, or the include line is commentedRename, verify with visudo -c
sudo asks for the target's passwordDefaults targetpw or rootpwRemove the default, or accept the behaviour
sudo: sorry, you must have a ttyrequiretty on old configurationsRemove Defaults requiretty
Command not allowed despite the rulePath is relative (systemctl not /usr/bin/systemctl)Use absolute paths
Allowed command can still get a shellWildcard or shell-capable program in the ruleWrapper script with exact arguments
Group change not effectiveMembership is read at loginRe-login, or restart the service
Removed from sudo after usermod -GMissing -ausermod -aG sudo user, verify with id
Account disabled but SSH keys still workpasswd -l only blocks password auth--expiredate 1 or nologin shell
useradd created no homeCREATE_HOME off in /etc/login.defs-m, or set the default
chage shows a password that never expires99999 max dayschage -M 90
su fails but SSH workssu 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 tools

Small utilities that fit Linux work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Linux guides

More in Linux

All Linux guides

The guides filed next to this one in Linux, in library order.