Configure SSH two-factor authentication with Google Authenticator TOTP

Intermediate 25 min May 03, 2026 102 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Add an extra layer of security to SSH logins by requiring both a password and a time-based one-time password (TOTP) generated by Google Authenticator or compatible apps.

Prerequisites

  • Root or sudo access
  • Smartphone with authenticator app
  • Active SSH connection for testing

What this solves

SSH password-only authentication is vulnerable to brute force attacks and credential theft. Two-factor authentication (2FA) adds a second authentication layer using time-based one-time passwords (TOTP) from your smartphone. Even if attackers obtain your password, they cannot access your server without the constantly changing 6-digit code from your authenticator app.

Step-by-step configuration

Update system packages

Start by updating your system to ensure you have the latest security patches and package repositories.

sudo apt update && sudo apt upgrade -y
sudo dnf update -y

Install Google Authenticator PAM module

The Google Authenticator PAM module integrates TOTP authentication with the Linux authentication system.

sudo apt install -y libpam-google-authenticator
sudo dnf install -y google-authenticator

Configure PAM for SSH authentication

Edit the PAM configuration to require Google Authenticator for SSH logins. This adds the TOTP requirement to the authentication chain.

sudo nano /etc/pam.d/sshd

Add this line at the top of the file, right after the initial comments:

auth required pam_google_authenticator.so
Note: The required keyword means SSH login will fail if TOTP verification fails. Use sufficient instead if you want to allow fallback to password-only authentication during testing.

Configure SSH daemon for 2FA

Modify the SSH daemon configuration to enable both password authentication and challenge-response authentication required for 2FA.

sudo nano /etc/ssh/sshd_config

Find and modify these lines in the SSH configuration:

ChallengeResponseAuthentication yes
PasswordAuthentication yes
AuthenticationMethods password,keyboard-interactive

If any of these lines don't exist, add them. The AuthenticationMethods line requires both a password and the TOTP code.

Restart SSH service

Apply the SSH configuration changes by restarting the SSH daemon.

sudo systemctl restart sshd
sudo systemctl status sshd
Warning: Keep your current SSH session open while testing. Open a new terminal to test the 2FA configuration before closing your existing connection.

Set up TOTP for your user account

Run the Google Authenticator setup for your user account. This generates the secret key and QR code for your smartphone app.

google-authenticator

Answer the prompts as follows:

  • "Do you want authentication tokens to be time-based?" → Yes (Y)
  • "Do you want to disallow multiple uses of the same authentication token?" → Yes (Y)
  • "By default, tokens are good for 30 seconds..." → No (N) for better security
  • "Do you want to enable rate-limiting?" → Yes (Y)

Scan QR code with authenticator app

The setup process displays a QR code. Scan it with Google Authenticator, Authy, or any compatible TOTP app on your smartphone. The app will start generating 6-digit codes that change every 30 seconds.

Save the backup codes displayed during setup. Store them in a secure location separate from your phone.

Note: If you cannot scan the QR code, manually enter the secret key shown below the QR code into your authenticator app.

Set up 2FA for additional users

Each user who needs SSH access must configure their own TOTP setup. Switch to each user account and run the authenticator setup.

sudo -u username google-authenticator

Each user will get their own QR code and secret key to add to their authenticator app.

Test two-factor authentication

Open a new SSH session to test the 2FA configuration. You should be prompted for both your password and verification code.

ssh username@203.0.113.10

You'll see prompts like this:

Password: [enter your regular password]
Verification code: [enter 6-digit code from authenticator app]

The login succeeds only after providing both valid credentials.

Verify TOTP configuration files

Check that the Google Authenticator configuration was created correctly for your user.

ls -la ~/.google_authenticator
cat ~/.google_authenticator

This file contains your secret key, backup codes, and configuration options. Keep this file secure and backed up.

Advanced configuration options

Configure emergency access

Create an emergency access method in case you lose your phone. Add a backup user or configure key-based authentication for emergency access.

# Allow key-based auth for emergency user
Match User emergency
    AuthenticationMethods publickey

This configuration allows the emergency user to log in with SSH keys, bypassing 2FA requirements.

Configure time synchronization

TOTP requires accurate time synchronization. Install and configure NTP to prevent authentication failures due to time drift.

sudo apt install -y chrony
sudo systemctl enable --now chrony
sudo dnf install -y chrony
sudo systemctl enable --now chronyd

Verify your setup

# Check SSH service status
sudo systemctl status sshd

Verify PAM configuration

grep google_authenticator /etc/pam.d/sshd

Check SSH configuration

grep -E "ChallengeResponse|AuthenticationMethods" /etc/ssh/sshd_config

Verify user TOTP setup

ls -la ~/.google_authenticator

Check time synchronization

timedatectl status

Common issues

SymptomCauseFix
"Verification code" prompt not appearingSSH config not properly setCheck ChallengeResponseAuthentication yes in sshd_config
Code rejected as invalidTime synchronization issueInstall and configure chrony/ntp on server
Cannot access server at allPAM misconfigurationBoot from rescue console, edit /etc/pam.d/sshd
QR code not scanningTerminal display issuesUse the manual secret key displayed below QR code
Lost phone with authenticatorNo backup access methodUse backup codes saved during setup or console access
Multiple failed attemptsRate limiting enabledWait 5 minutes or check ~/.google_authenticator settings

Security considerations

Store backup codes in a secure location separate from your authenticator device. Consider using multiple devices or backup authentication methods for critical servers. Regular monitoring of SSH logs helps detect unauthorized access attempts.

For enhanced security, combine 2FA with SSH key authentication and consider implementing automated intrusion prevention for comprehensive protection.

Next steps

Running this in production?

Want this handled for you? Setting up SSH 2FA once is straightforward. Keeping it monitored, maintaining emergency access procedures, and managing user onboarding across environments is the harder part. See how we run infrastructure like this for European teams.

Automated install script

Run this to automate the entire setup

Need help?

Don't want to manage this yourself?

We handle infrastructure security hardening for businesses that depend on uptime. From initial setup to ongoing operations.