Setting up two factor Authentication for SSH enhances the security of your SSH server by requiring a second authentication factor. Two factor authentication for SSH adds an additional security layer by requiring users to provide more than just the correct username and password. They must also supply a second piece of information: a temporary numeric code generated separately by both the server and a mobile device like a smartphone or tablet.

Here’s a step-by-step guide on how to do it:
Prerequisites
- A running OpenSSH server.
- Root or sudo access to the server.
libpam-google-authenticatorinstalled on the server.

Step-by-Step Guide
1. Install Google Authenticator PAM Module
First, you need to install the libpam-google-authenticator package. The package name may vary slightly depending on your distribution.
On Ubuntu/Debian:
sudo apt update sudo apt install libpam-google-authenticator
On CentOS/RHEL:
sudo yum install epel-release
sudo yum install google-authenticator

2. Configure Google Authenticator for the User
Run the google-authenticator command for the user you want to set up 2FA for (usually your own user account). This will generate a QR code, secret key, and emergency scratch codes.
google-authenticator
Follow the prompts:
- Answer
yto the question about time-based tokens. - Answer
yornto the rate-limiting options as per your preference. - Save the secret key, verification code, and emergency scratch codes in a safe place.
3. Configure PAM to Use Google Authenticator
Edit the SSH PAM configuration file to include the Google Authenticator module.
On Ubuntu/Debian:
sudo nano /etc/pam.d/sshd
On CentOS/RHEL:
sudo nano /etc/pam.d/sshd
Add the following line at the top of the file:
auth required pam_google_authenticator.so
4. Configure SSH to Use Challenge-Response Authentication
Edit the SSH server configuration file to enable Challenge-Response Authentication.
sudo nano /etc/ssh/sshd_config
Find and modify (or add) the following lines:
ChallengeResponseAuthentication yes UsePAM yes
Make sure PasswordAuthentication is set to no if you do not want to allow password authentication.
5. Restart the SSH Service
After making these changes, restart the SSH service to apply the new settings.
On Ubuntu/Debian:
sudo systemctl restart ssh
On CentOS/RHEL:
sudo systemctl restart sshd
6. Test the Configuration of two factor authentication for SSH
Open a new terminal and try to log in to your SSH server. You should be prompted for your password (if applicable) and then a verification code from the Google Authenticator app use two factor authentication for SSH.
ssh your_user@your_server_ip

You will be prompted for:
- Password (if applicable).
- Verification code from your Google Authenticator app.
Troubleshooting
- If you are locked out: You can regain access to the server by accessing it via console or recovery mode and disabling the PAM module or reversing the changes made in
sshd_config. - Logs and debugging: Check
/var/log/auth.log(Ubuntu/Debian) or/var/log/secure(CentOS/RHEL) for any errors or issues.
By following these steps, you should have successfully set up Google 2FA for your OpenSSH server, providing an extra layer of security to your login process.

Two factor authentication for SSH (Secure Shell) is required to enhance the security of remote access to servers and systems. Here are the key reasons why 2FA is crucial for SSH:
1. Enhanced Security
- Additional Layer of Protection: Two factor authentication for SSH adds an extra layer of security beyond just a password. Even if an attacker manages to obtain the password, they would still need the second factor (e.g., a mobile device, hardware token, or biometric verification) to gain access.
- Reduction of Risk from Password Compromise: Passwords can be compromised through various means such as phishing, brute force attacks, or data breaches. With 2FA, a compromised password alone is not enough to breach the system.
2. Mitigates Common Attack Vectors
- Brute Force and Dictionary Attacks: These attacks rely on systematically guessing passwords. 2FA requires a second form of authentication that cannot be easily guessed or intercepted, making these attacks ineffective.
- Phishing Attacks: Even if a user is tricked into providing their password, the attacker would still need the second authentication factor, which is not typically available through phishing.
3. Compliance and Best Practices

- Regulatory Compliance: Many regulations and standards (such as PCI DSS, HIPAA, and GDPR) require the implementation of strong authentication methods, including Two factor authentication for SSH, to protect sensitive data.
- Industry Best Practices: Implementing 2FA is widely regarded as a best practice for securing access to critical systems and data. It demonstrates a commitment to robust security measures and can enhance an organization’s security posture.
4. Protection Against Insider Threats
- Insider Threats: Even trusted users can pose a security risk, either maliciously or accidentally. Two factor authentication for SSH helps ensure that access is controlled and verified, reducing the risk of unauthorized actions by insiders.
5. Securing Remote Access
- Remote Work Security: With the increasing prevalence of remote work, securing remote access to systems is more critical than ever. Two factor authentication for SSH ensures that even if remote workers’ devices are compromised, the SSH access remains protected.
- Mobile and On-the-Go Security: Users accessing systems from various locations and devices benefit from the added security of Two factor authentication for SSH, which helps protect against vulnerabilities inherent in less controlled environments.
6. User Accountability and Logging
- Improved Accountability: Two factor authentication for SSH often includes mechanisms for better logging and tracking of authentication attempts. This can help in monitoring and responding to suspicious activities.
- Detailed Auditing: It provides detailed records of who accessed the system and when, which is useful for forensic analysis and auditing.
Implementing Two factor authentication for SSH significantly enhances security by requiring an additional form of verification beyond a password. It mitigates various attack vectors, ensures compliance with regulatory requirements, protects against insider threats, and secures remote access effectively. Given the increasing sophistication of cyber threats, 2FA is a necessary measure to safeguard sensitive systems and data.