2FA for SSH with Google Authenticator
How to enable two-factor authentication for SSH on Linux using Google Authenticator or Authy
2FA adds a second layer of security: even if someone gets your SSH password, they can't access without the OTP code.
Before applying this configuration, open a second separate SSH session so you can recover if there are errors. Don't close your current session until you've verified it works.
Installation
sudo apt update
sudo apt install libpam-google-authenticator -yUser Configuration
Run this command as the user you want to protect (e.g., root or your user):
google-authenticatorAnswer the questions:
- Do you want authentication tokens to be time-based? →
y - Scan the QR code with Google Authenticator, Authy, or any TOTP app
- Do you want me to update your "~/.google_authenticator" file? →
y - Do you want to disallow multiple uses of the same token? →
y - By default, tokens are good for 30 seconds... →
n(oryif you have synchronization issues) - Do you want to enable rate-limiting? →
y
Save the emergency scratch codes in a safe place. They allow you to access if you lose your phone.
PAM Configuration
sudo nano /etc/pam.d/sshdAdd this line at the top of the file:
auth required pam_google_authenticator.soSSH Configuration
sudo nano /etc/ssh/sshd_configModify or add these lines:
# Enable challenge/response (necessary for 2FA)
ChallengeResponseAuthentication yes
# Or in recent OpenSSH versions:
KbdInteractiveAuthentication yes
# Accepted authentication methods
AuthenticationMethods publickey,keyboard-interactive
# This requires SSH key FIRST, then OTP code
# To require only password + OTP (less secure):
# AuthenticationMethods keyboard-interactivesudo systemctl restart sshVerification
Open a new SSH session (don't close the current one):
ssh root@185.100.xxx.xxxYou should see:
Verification code: ← enter OTP code from app
root@185.100.xxx.xxx:~#Configure 2FA for Specific Users Only
To apply 2FA only to certain users (not all):
sudo nano /etc/ssh/sshd_config# Require 2FA only for user "admin"
Match User admin
AuthenticationMethods publickey,keyboard-interactive2FA with SSH Key (Most Secure Flow)
The recommended flow is: SSH key + OTP code:
- User presents SSH key → authenticated
- User enters OTP code → authenticated
AuthenticationMethods publickey,keyboard-interactiveThis makes unauthorized access practically impossible.
Add 2FA to a New User
Each user must run google-authenticator with their own account:
# As regular user
su - regularuser
google-authenticatorTemporarily Disable 2FA
In case of emergency (e.g., lost phone):
# Access via VNC Console (VirtFusion panel)
# Comment out the PAM line
sudo sed -i 's/^auth required pam_google_authenticator.so/#auth required pam_google_authenticator.so/' /etc/pam.d/sshd
sudo systemctl restart sshThen reconfigure when you regain access.
Recommended TOTP Apps
- Google Authenticator (Android/iOS)
- Authy (Android/iOS/Desktop: recommended for backup)
- Bitwarden Authenticator (open source)
- andOTP (Android, open source)