2FA for SSH with Google Authenticator

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.

02

Installation

bash
sudo apt update
sudo apt install libpam-google-authenticator -y
03

User Configuration

Run this command as the user you want to protect (e.g., root or your user):

bash
google-authenticator

Answer the questions:

Save the emergency scratch codes in a safe place. They allow you to access if you lose your phone.

  • 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 (or y if you have synchronization issues)
  • Do you want to enable rate-limiting? → y
04

PAM Configuration

bash
sudo nano /etc/pam.d/sshd

Add this line at the top of the file:

auth required pam_google_authenticator.so
05

SSH Configuration

bash
sudo nano /etc/ssh/sshd_config

Modify 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-interactive
bash
sudo systemctl restart ssh
06

Verification

Open a new SSH session (don't close the current one):

ssh root@185.100.xxx.xxx

You should see:

Verification code: ← enter OTP code from app root@185.100.xxx.xxx:~#
07

Configure 2FA for Specific Users Only

To apply 2FA only to certain users (not all):

bash
sudo nano /etc/ssh/sshd_config
# Require 2FA only for user "admin" Match User admin AuthenticationMethods publickey,keyboard-interactive
08

2FA with SSH Key (Most Secure Flow)

The recommended flow is: SSH key + OTP code:

AuthenticationMethods publickey,keyboard-interactive

This makes unauthorized access practically impossible.

  • User presents SSH key → authenticated
  • User enters OTP code → authenticated
09

Add 2FA to a New User

Each user must run google-authenticator with their own account:

bash
# As regular user
su - regularuser
google-authenticator
10

Temporarily Disable 2FA

In case of emergency (e.g., lost phone):

bash
# 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 ssh

Then reconfigure when you regain access.

11

Recommended TOTP Apps

  • Google Authenticator (Android/iOS)
  • Authy (Android/iOS/Desktop: recommended for backup)
  • Bitwarden Authenticator (open source)
  • andOTP (Android, open source)

DeluxHost, opgericht in 2023, biedt hoogwaardige hostingoplossingen voor diverse digitale behoeften. Wij bieden gedeelde hosting, VPS en dedicated servers met geavanceerde beveiliging en wereldwijde datacenters.

© DeluxHost, Alle rechten voorbehouden. | BTW-nummer: IT17734661006
Alle systemen operationeel