As an example, we will configure how to use PAM to disable root user access to a system via SSH and login programs.
Here, we want to disable root user access to a system, by restricting access to login and sshd services.
We can use the /lib/security/pam_listfile.so module which offers great flexibility in limiting the privileges of specific accounts.
Open and edit the file for the target service in the /etc/pam.d/ directory as shown.
sudo vim /etc/pam.d/sshd
or
sudo vim /etc/pam.d/login
Add this rule in both files.
auth required pam_listfile.so \ onerr=succeed item=user sense=deny file=/etc/ssh/deniedusers
Create the file /etc/ssh/deniedusers and add the name root in it:
sudo vim /etc/ssh/deniedusers
Save the changes and close the file.
sudo chmod 600 /etc/ssh/deniedusers
From now on, the above rule will tell PAM to consult the /etc/ssh/deniedusers file and deny access to the SSH and login services for any listed user.