Skip to content

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.

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:

  • 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).
  • 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.
  • Handles updating authentication tokens (e.g., when a user runs the passwd command).
  • Enforces password complexity rules, history checks (preventing reuse of recent passwords), and minimum/maximum age.
  • 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.

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

Linux includes standard PAM modules located in /lib/security/ or /usr/lib64/security/:

The standard, traditional Linux authentication module.

  • Verifies passwords against /etc/shadow.
  • Updates /etc/shadow when passwords change.
  • Checks basic account expiration.

Enforces strict password strength and complexity rules (length, uppercase, lowercase, numbers, special characters).

password requisite pam_pwquality.so retry=3 minlen=12 difok=3

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

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

Prevents non-root users from logging into the system if the /etc/nologin file exists (commonly used during system maintenance).

auth required pam_nologin.so

11.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:

%PAM-1.0
auth include system-auth
account include system-auth
password include system-auth
session optional pam_keyinit.so revoke
session required pam_limits.so
  1. When you run sudo command, sudo calls PAM.
  2. PAM looks at /etc/pam.d/sudo.
  3. It includes system-auth for authentication, account validation, and password handling.
  4. If authentication passes, it loads pam_limits.so in session management to enforce process and file limits.
  5. 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 of sudo, su, and SSH access.