11. PAM Authentication
Before PAM was introduced, every program needing authentication (like login, su, ftp, or sshd) had its own hardcoded authentication mechanism directly compiled into the binary. If an administrator wanted to switch from standard /etc/shadow passwords to LDAP or Kerberos, every single program had to be recompiled.
PAM (Pluggable Authentication Modules) solved this by decoupling applications from authentication mechanisms, providing a flexible, modular architecture for system security.
11.1 PAM Architecture
Section titled “11.1 PAM Architecture”PAM sits directly between user-facing applications and the system’s authentication backend.
Applications (sshd, sudo, login, su, passwd) ↓ libpam (PAM API) ↓ PAM Configuration (/etc/pam.d/) ↓ Pluggable Modules (/lib/security/ or /usr/lib64/security/) ├── pam_unix.so (local /etc/shadow) ├── pam_pwquality.so (password complexity) ├── pam_faillock.so (account lockout) └── pam_ldap.so (central directory)Applications simply make calls to PAM functions (e.g., pam_authenticate()). The system administrator decides how that authentication actually happens by editing text configuration files, without modifying or recompiling the application.
11.2 The Four Management Groups (Module Types)
Section titled “11.2 The Four Management Groups (Module Types)”Every PAM configuration rule belongs to one of four functional groups:
1. auth (Authentication Management)
Section titled “1. auth (Authentication Management)”- Verifies the user’s identity (e.g., prompting for and verifying a password, biometric data, or multi-factor token).
- Can also set credentials (such as Kerberos tickets or group memberships).
2. account (Account Management)
Section titled “2. account (Account Management)”- Checks whether the authenticated user is actually permitted to log in at this moment.
- Validates password expiration, account expiration, time-of-day restrictions, and system access limits.
3. password (Password Management)
Section titled “3. password (Password Management)”- Handles updating authentication tokens (e.g., when a user runs the
passwdcommand). - Enforces password complexity rules, history checks (preventing reuse of recent passwords), and minimum/maximum age.
4. session (Session Management)
Section titled “4. session (Session Management)”- Handles tasks that must occur before a user is given access and after they log out.
- Examples: mounting user home directories, setting resource limits (
ulimit), printing the message of the day (MOTD), and logging audit records.
11.3 PAM Configuration Syntax
Section titled “11.3 PAM Configuration Syntax”PAM configuration files are located in /etc/pam.d/. Each service typically has its own file named after the service (e.g., /etc/pam.d/sshd, /etc/pam.d/sudo, /etc/pam.d/login).
Each line in a configuration file follows this 4-part syntax:
type control_flag module_path [module_arguments]Control Flags
Section titled “Control Flags”Control flags determine how PAM handles the success or failure of a module in the stack:
| Control Flag | Meaning |
|||
| required | Must succeed. If it fails, the overall authentication will fail, but PAM continues executing subsequent modules in the stack so the attacker doesn’t know which module failed. |
| requisite | Must succeed. If it fails, PAM immediately aborts the stack and returns failure without running any remaining modules. |
| sufficient | If it succeeds (and no prior required modules have failed), PAM immediately accepts authentication and stops evaluating remaining modules in that group. If it fails, it is ignored and execution continues. |
| optional | The module’s success or failure is ignored unless it is the only module in the stack for that type. Useful for logging or non-critical tasks. |
| include / substack | Pulls in rules from common configuration files (like common-auth on Debian or system-auth on RHEL). |
11.4 Common PAM Modules
Section titled “11.4 Common PAM Modules”Linux includes standard PAM modules located in /lib/security/ or /usr/lib64/security/:
pam_unix.so
Section titled “pam_unix.so”The standard, traditional Linux authentication module.
- Verifies passwords against
/etc/shadow. - Updates
/etc/shadowwhen passwords change. - Checks basic account expiration.
pam_pwquality.so
Section titled “pam_pwquality.so”Enforces strict password strength and complexity rules (length, uppercase, lowercase, numbers, special characters).
password requisite pam_pwquality.so retry=3 minlen=12 difok=3pam_faillock.so / pam_tally2.so
Section titled “pam_faillock.so / pam_tally2.so”Tracks failed login attempts and temporarily locks the account after a defined threshold to prevent brute-force attacks.
auth required pam_faillock.so preauth silent audit deny=5 unlock_time=900pam_limits.so
Section titled “pam_limits.so”Enforces user-level resource limits defined in /etc/security/limits.conf (e.g., max open file descriptors, max processes, max memory).
session required pam_limits.sopam_wheel.so
Section titled “pam_wheel.so”Restricts access to the su (Switch User) command so that only members of the wheel administrative group can attempt to switch to root.
# In /etc/pam.d/su:auth required pam_wheel.so use_uidpam_nologin.so
Section titled “pam_nologin.so”Prevents non-root users from logging into the system if the /etc/nologin file exists (commonly used during system maintenance).
auth required pam_nologin.so11.5 Practical PAM Example: Inspecting /etc/pam.d/sudo
Section titled “11.5 Practical PAM Example: Inspecting /etc/pam.d/sudo”Take a look at how sudo authenticates a user:
auth include system-authaccount include system-authpassword include system-authsession optional pam_keyinit.so revokesession required pam_limits.so- When you run
sudo command,sudocalls PAM. - PAM looks at
/etc/pam.d/sudo. - It includes
system-authfor authentication, account validation, and password handling. - If authentication passes, it loads
pam_limits.soin session management to enforce process and file limits. - Sudo grants execution.
[!WARNING] Always keep a secondary root terminal open when editing PAM configuration files in
/etc/pam.d/. A single syntax error or incorrect control flag can completely lock you out ofsudo,su, and SSH access.