1. Blog
  2. Manuals

Two-Factor SSH Authentication: Set Up 2FA on Your Server

Two-Factor SSH Authentication: Set Up 2FA on Your Server

Two-factor authentication adds a one-time code from an app on your phone to the SSH login. Even if a password or private key leaks, nobody gets into the server without the second factor. Setup takes about ten minutes.

How it works

The server and the authenticator app share a secret and derive a six-digit code from it that changes every 30 seconds (the TOTP standard). Any compatible app works: Google Authenticator, Aegis, 2FAS, Bitwarden. No internet connection is needed to generate codes, but the clocks on the server and the phone must be accurate.

Step 1. Install the module

Ubuntu and Debian:

sudo apt update
sudo apt install libpam-google-authenticator

AlmaLinux, Rocky Linux:

sudo dnf install epel-release
sudo dnf install google-authenticator qrencode

Step 2. Create the secret

Run the command as the user you log in with. Each user gets their own secret:

google-authenticator

Answer the questions:

  1. Do you want authentication tokens to be time-based? — y.
  2. A QR code appears. Scan it with your authenticator app and enter the code from the app to confirm.
  3. Save the emergency scratch codes somewhere safe. Each works once and lets you in if the phone is lost.
  4. Update your .google_authenticator file? — y.
  5. Disallow multiple uses of the same token? — y: protects against replaying an intercepted code.
  6. Increase the window? — n if the server and phone clocks are in sync.
  7. Enable rate-limiting? — y: at most three login attempts every 30 seconds.

Step 3. Configure PAM

Open the file:

sudo nano /etc/pam.d/sshd

Add this line at the end:

auth required pam_google_authenticator.so

If the server has several users and not all of them have set up the app yet, use the nullok variant for now — users without a secret can still log in as before:

auth required pam_google_authenticator.so nullok

Step 4. Configure SSH

The next steps depend on how you log in.

Open /etc/ssh/sshd_config and set:

KbdInteractiveAuthentication yes
UsePAM yes
PasswordAuthentication no
AuthenticationMethods publickey,keyboard-interactive

To have the server ask only for the code after the key, not for a password, comment out the standard password check in /etc/pam.d/sshd:

#@include common-auth

On AlmaLinux and Rocky Linux comment out the auth substack password-auth line instead.

Option B: password plus code

KbdInteractiveAuthentication yes
UsePAM yes

Leave /etc/pam.d/sshd unchanged apart from the line from step 3. The server asks for the password, then the code.

Check the files in sshd_config.d

On Ubuntu and Debian, settings in /etc/ssh/sshd_config.d/ take precedence over the main file. Make sure they do not override yours:

grep -rE "KbdInteractive|ChallengeResponse|PasswordAuthentication|AuthenticationMethods" /etc/ssh/sshd_config.d/

Fix or comment out any conflicting lines.

Step 5. Apply the settings

Test the configuration and restart the service:

sudo sshd -t
sudo systemctl restart ssh      # Ubuntu, Debian
sudo systemctl restart sshd     # AlmaLinux, Rocky Linux

Step 6. Test

Without closing the current session, open a new terminal window and connect:

ssh user@203.0.113.10

The server prompts for Verification code: — enter the code from the app. If you get in, you are done.

If something goes wrong

  • The code is rejected. Usually the clocks differ. Check the server time with timedatectl and enable synchronisation: sudo timedatectl set-ntp true.
  • The server does not ask for a code. Check that KbdInteractiveAuthentication yes is not overridden in sshd_config.d and that the service was restarted.
  • The phone is lost. Use one of the scratch codes instead of the one-time code.
  • You cannot log in at all. Use the console in the control panel, remove the pam_google_authenticator.so line from /etc/pam.d/sshd and restart SSH. Details are in the journal: journalctl -u ssh -n 50.

Exceptions for automation

Backup scripts and deployment systems cannot type codes. You can allow key-only logins for them from a specific address. Add to the end of sshd_config:

Match User backup Address 198.51.100.7
    AuthenticationMethods publickey

Moving to a new phone

The secret lives in ~/.google_authenticator on the server. To enrol a new device, run google-authenticator again and scan the new QR code — the old secret stops working.

More hardening

Two-factor authentication is the last line of defence, not the only one. On a server, combine it with key-based logins and keep your credentials in a password manager such as your own Vaultwarden.