Sub topic 13
PAM, Password Policy, and sudo
This chapter covers the CIS Benchmark section on authentication: how PAM (Pluggable Authentication Modules) decides who may log in, how to enforce password rules and lockouts, and how to restrict and log sudo. It assumes you can edit files under /etc, you know what a PAM service file looks like at a basic level, and you have a test server with a second root session open. The examples use RHEL 9 and note Debian and Ubuntu differences where they matter.
How PAM works
PAM is a library that programs such as sshd, login, su and sudo call to make authentication decisions. Each program has a service file in /etc/pam.d/. Each line in that file names a module type, a control flag, a module and its arguments.
| Type | Question it answers |
|---|---|
| auth | Is this user who they claim to be? |
| account | Is this user allowed in now (expired, locked, restricted)? |
| password | How is a password changed and what rules apply? |
| session | What setup or logging happens around the session? |
The control flag decides what a module's result means for the whole stack.
| Flag | Effect |
|---|---|
| required | Failure makes the stack fail, but remaining modules still run. |
| requisite | Failure ends the stack immediately. |
| sufficient | Success ends the stack early, if no earlier required module failed. |
| optional | Result matters only if it is the only module. |
Order matters. A sufficient line placed too early can skip a lockout check that comes after it.
Do not edit PAM files by hand
On RHEL, /etc/pam.d/system-auth and password-auth are generated by authselect. Hand edits are overwritten the next time a profile is applied. Debian and Ubuntu use pam-auth-update for the same reason. Use the supported tool and keep the change in a custom profile.
# Show the active profile and its enabled features
authselect current
# Build a custom profile from sssd so local changes survive updates
sudo authselect create-profile cis-custom --base-on sssd
sudo authselect select custom/cis-custom with-faillock without-nullok --force
# Confirm the result
authselect check
grep -E 'pam_faillock|nullok' /etc/pam.d/system-authThe with-faillock feature adds account lockout. The without-nullok feature stops PAM accepting empty passwords. Both correspond to CIS recommendations.
Account lockout with faillock
CIS asks you to lock an account after repeated failed logins. The pam_faillock module counts failures and the settings live in /etc/security/faillock.conf.
# /etc/security/faillock.conf
deny = 5
fail_interval = 900
unlock_time = 900
even_deny_root
root_unlock_time = 60This locks an account for 15 minutes after 5 failures within 15 minutes. even_deny_root applies the rule to root as well, which protects against guessing but means a remote attacker can lock root out. Pair it with root_unlock_time and make sure you can still reach the console. Check and clear a user's counter like this:
sudo faillock --user alice
sudo faillock --user alice --resetPassword quality and aging
Password complexity is enforced by pam_pwquality. Its settings are in /etc/security/pwquality.conf.
# /etc/security/pwquality.conf
minlen = 14
minclass = 4
maxrepeat = 3
maxsequence = 3
dictcheck = 1
enforce_for_rootCurrent NIST guidance favors length over forced character classes, and the CIS values have changed between versions. Use the numbers in the benchmark version you scan against, so your results match the scanner.
Password history stops users cycling back to old passwords. On RHEL it is set with pam_pwhistory, and the password hash must be strong. Verify the hashing method in /etc/login.defs:
# /etc/login.defs
ENCRYPT_METHOD YESCRYPT
PASS_MAX_DAYS 365
PASS_MIN_DAYS 1
PASS_WARN_AGE 7login.defs applies only to accounts created afterward. Existing accounts keep their old aging values until you change them with chage.
# Apply aging to an existing account, then inspect it
sudo chage --maxdays 365 --mindays 1 --warndays 7 alice
sudo chage --list alice
# Find accounts with no password set
sudo awk -F: '($2 == "") {print $1}' /etc/shadowThe last command should print nothing. Any name it lists has an empty password and needs fixing with passwd -l or a real password.
Restricting and logging sudo
sudo runs commands with another user's privileges. CIS recommendations focus on four things: sudo is installed, it uses a pseudo terminal, it writes its own log, and users must re-authenticate for privilege escalation.
Always edit with visudo, which checks syntax before saving. A broken sudoers file can lock out every administrator. Put changes in a drop-in file under /etc/sudoers.d/.
sudo visudo -f /etc/sudoers.d/60-cis# /etc/sudoers.d/60-cis
Defaults use_pty
Defaults logfile="/var/log/sudo.log"
Defaults timestamp_timeout=5
Defaults !authenticateDo not copy the last line. !authenticate turns off password prompts and is the opposite of what CIS wants. Benchmarks check for it and fail the host if it appears. Remove it, and also remove any NOPASSWD entries unless an exception is documented (see the final chapter).
# Validate syntax of all sudoers files
sudo visudo -c
# Search for settings the benchmark flags
sudo grep -rE 'NOPASSWD|!authenticate' /etc/sudoers /etc/sudoers.d/The use_pty option makes sudo run commands in a pseudo terminal. This stops a background process started by a privileged command from keeping control of your terminal after sudo exits. The timestamp_timeout value controls how long a password is remembered.
Restricting su
CIS also asks that su be limited to a named group. Create an empty group and require it in the su PAM file.
sudo groupadd sugroup# /etc/pam.d/su (add near the top of the auth section)
auth required pam_wheel.so use_uid group=sugroupBecause the group has no members, nobody can use su. Administrators use sudo instead, which gives a per-user audit trail.
Common errors
- Locking yourself out. Test in a second open session before closing the first. Never apply a PAM change to a remote server with only one connection.
- Overwritten edits. If
authselect checkreports a modified file, your hand edits will be lost. Move them into a custom profile. - Wrong order.
pam_faillockneeds apreauthline beforepam_unixand anauthfailline after it. Reordering them breaks counting. - Scanner mismatch. A setting in a drop-in can be overridden by one in the main file. Re-run the scan after each change to see what the scanner reads.
- Service accounts. Rules such as lockout and aging can break automation accounts. Handle them as documented exceptions, not by weakening the rule for everyone.
Exercise
You add Defaults !authenticate to a sudoers drop-in so a script can run without prompts. Which tool should you have used to edit it, which command checks every sudoers file, and why will a CIS scan fail this host?
Show answer
Edit with visudo -f /etc/sudoers.d/<name> so syntax is checked before saving. Check all files with visudo -c. The scan fails because !authenticate disables password re-authentication for privilege escalation, which CIS requires. Remove it and, if the script needs passwordless access, grant a narrow NOPASSWD rule for one command and record it as an exception.
Exercise
After running authselect select sssd, your hand-edited pam_faillock lines in /etc/pam.d/system-auth are gone. Why, and what should you do instead?
Show answer
authselect regenerates system-auth and password-auth from the selected profile, so manual edits are overwritten. Enable the feature through authselect (with-faillock), or create a custom profile with authselect create-profile and select that. Put the lockout values in /etc/security/faillock.conf.