Log in with either a password or a public key. But not both at the same time, and limit the PAM authentication to the root user

3 min read 24-10-2024
Log in with either a password or a public key. But not both at the same time, and limit the PAM authentication to the root user

In today's digital age, ensuring secure access to systems is paramount. One common requirement is to allow users, specifically the root user, to log in using either a password or a public key, but not both at the same time. This method enhances security and provides flexibility for system administrators.

Problem Scenario

The challenge is to configure Pluggable Authentication Modules (PAM) such that the root user can authenticate using either a password or a public key, but not both concurrently. The initial pseudo code for this requirement may look like this:

auth required pam_unix.so
auth required pam_ssh.so

However, this configuration does not impose the necessary restriction that allows authentication via either method, but not both at once.

Corrected Code

To achieve the desired login behavior, you need to modify the PAM configuration for the root user in the appropriate PAM service file, usually located at /etc/pam.d/sshd for SSH access. The corrected code should look like this:

# Allow root login via password or public key, but not both
auth required pam_unix.so
auth optional pam_ssh.so

# Reject if both methods are used
auth [default=1 success=ok ignore=ignore] pam_ssh.so
auth requisite pam_succeed_if.so uid eq 0

Breakdown of the Code

  1. pam_unix.so: This module is required for password authentication. It checks the user credentials against the local /etc/passwd and /etc/shadow files.

  2. pam_ssh.so: This optional module handles SSH public key authentication.

  3. pam_succeed_if.so: The requisite module ensures that the root user (UID 0) is checked, and if they attempt both methods, the login fails.

Why Limit PAM Authentication to the Root User?

Limiting PAM authentication to just the root user serves several purposes:

  • Security: The root user has elevated privileges, making it a prime target for attackers. By restricting authentication methods, you reduce the attack surface.

  • Simplicity: Having a clear authentication method for root can simplify security audits and compliance with security policies.

  • Prevent Confusion: When multiple authentication methods are available simultaneously, it can lead to confusion during login and troubleshooting.

Practical Example

Consider a system administrator who manages a server through SSH. They prefer the flexibility of using either a password or public key for root access. Here's how they might set up the PAM configuration:

  1. Update /etc/ssh/sshd_config to ensure that public key authentication is enabled:

    PubkeyAuthentication yes
    PermitRootLogin yes
    
  2. Modify the PAM configuration as shown above to limit the root user to one authentication method at a time.

  3. Restart the SSH service to apply the changes:

    sudo systemctl restart sshd
    

Now, the root user can log in with either a password or a public key, but they cannot use both methods simultaneously.

Additional Tips for Enhanced Security

  • Use SSH Keys: Whenever possible, prefer using public key authentication over passwords, as it provides a higher level of security.

  • Disable Root Login via Password: Consider setting PermitRootLogin without-password in /etc/ssh/sshd_config, which would require the use of SSH keys for root login, enhancing security.

  • Monitor Access Logs: Regularly check /var/log/auth.log to monitor failed login attempts and identify any potential security breaches.

Conclusion

Configuring your system to allow root access via either a password or a public key is essential for maintaining security while offering flexibility. By implementing the correct PAM configuration, you can help protect your systems from unauthorized access and simplify the authentication process for root users.

For further reading on PAM and security best practices, check out these resources:

By following these guidelines, you can ensure a more secure and manageable environment for your systems.