Log inSign up

Require an SSH key and a TOTP code to log in

Install the PAM module (libpam-google-authenticator on Ubuntu and Debian, google-authenticator from EPEL on Rocky and AlmaLinux), run google-authenticator as your user and scan the QR code, then add the module to /etc/pam.d/sshd and set AuthenticationMethods publickey,keyboard-interactive. Keep one session open and a timed undo armed until a new login asks for, and accepts, your code.

Topic: Security · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What does key plus TOTP protect against?

An SSH key proves you hold a file; a time based one time password (TOTP) proves you also hold a phone or hardware token. A stolen laptop backup or a leaked private key alone is then no longer enough to log in. The codes follow RFC 6238, so any standard authenticator app works, including open source ones. The PAM module is called Google Authenticator for historical reasons but needs no account anywhere and makes no network requests; codes are computed from a shared secret and the clock. This guide assumes key logins already work, as set up in SSH keys and hardening, and that you log in as a sudo user such as deploy.

How do I install the PAM module and check the clock?

Install the package for your system:

sudo apt update && sudo apt install libpam-google-authenticator qrencode
sudo dnf install -y epel-release && sudo dnf install -y google-authenticator qrencode

The first line is for Ubuntu and Debian, the second for Rocky Linux and AlmaLinux. TOTP codes only match when the server clock is right, so confirm time sync before anything else:

timedatectl

Look for System clock synchronized: yes. If it says no, fix it with the steps in time zone and time sync.

How do I create my secret and recovery codes?

Run the generator as the user who will log in, not with sudo. Storing the file inside ~/.ssh keeps it readable for sshd under SELinux on Rocky and AlmaLinux, and works the same on Debian and Ubuntu:

google-authenticator -t -d -f -r 3 -R 30 -w 3 -s ~/.ssh/.google_authenticator
chmod 600 ~/.ssh/.google_authenticator
restorecon -Rv ~/.ssh 2>/dev/null || true

The flags choose time based codes (-t), refuse reuse of a code (-d), write the file without questions (-f), allow three attempts per thirty seconds (-r 3 -R 30) and accept one step of clock drift each way (-w 3). Scan the QR code in your app, and copy the five emergency scratch codes to your password manager. Each scratch code works once in place of a TOTP code.

How do I arm a rollback before touching SSH?

Back up the PAM file and schedule an automatic undo. Without a browser console, this is what saves you if PAM or sshd rejects every login:

sudo cp /etc/pam.d/sshd /root/pam-sshd.bak
sudo systemd-run --on-active=10min --unit=mfa-undo /bin/sh -c "cp /root/pam-sshd.bak /etc/pam.d/sshd; rm -f /etc/ssh/sshd_config.d/05-mfa.conf; systemctl restart sshd || systemctl restart ssh"
systemctl list-timers mfa-undo.timer

Keep your current session open the whole time. Restarting sshd does not end connections that already exist, so that session remains your way back if the timer is not enough.

How do I change PAM for SSH?

Add the module at the top of /etc/pam.d/sshd and stop PAM from asking for the account password, because the SSH key already counts as the first factor:

sudo nano /etc/pam.d/sshd
auth required pam_google_authenticator.so secret=${HOME}/.ssh/.google_authenticator nullok

Then disable the password step. On Ubuntu and Debian comment out @include common-auth; on Rocky and AlmaLinux comment out auth substack password-auth. Comment means putting # at the start of the line. The nullok option lets users who have not created a secret log in with the key only, which helps during a rollout; remove it once every account has a secret.

How do I tell sshd to ask for both factors?

Create a drop-in file with a low number. sshd keeps the first value it reads for each setting, and Ubuntu cloud images ship a 50-cloud-init.conf, while Rocky and AlmaLinux have 50-redhat.conf that disables keyboard interactive logins:

sudo tee /etc/ssh/sshd_config.d/05-mfa.conf >/dev/null <<EOF
UsePAM yes
KbdInteractiveAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
EOF
sudo sshd -t && sudo sshd -T | grep -Ei "authenticationmethods|kbdinteractive"

The test prints nothing when the syntax is right, and the second command shows the active values. Restart the daemon: sudo systemctl restart ssh on Ubuntu and Debian, sudo systemctl restart sshd on Rocky and AlmaLinux.

How do I test the new login?

Open a new terminal on your computer and connect:

ssh -v deploy@198.51.100.24

You should see the key accepted with Partial success, followed by a Verification code: prompt. Enter the six digit code from your app. When that works, cancel the undo timer with sudo systemctl stop mfa-undo.timer. If the code is refused, compare the clocks, check that the secret file path in PAM matches the file, and read sudo journalctl -u ssh -u sshd -n 30 for pam_google_authenticator lines. Tools that log in without a human, such as backup jobs or deploy pipelines, cannot type codes: give them a separate user and a Match User block that sets AuthenticationMethods publickey, as explained in advanced OpenSSH settings.

Can sudo ask for a code too?

Yes. Add the same module line at the top of /etc/pam.d/sudo. Keep a root shell open in a second terminal with sudo -i before editing, so a broken file can be restored:

sudo cp /etc/pam.d/sudo /root/pam-sudo.bak
sudo sed -i "1a auth required pam_google_authenticator.so secret=\${HOME}/.ssh/.google_authenticator" /etc/pam.d/sudo
sudo -k && sudo -v

The last command should ask for your password and then the code. Accounts with NOPASSWD in sudoers skip PAM authentication entirely, so they will not see the prompt. If something fails, copy the backup back from the root shell.

Questions about Two-factor SSH logins with TOTP

What if I lose my phone?

Use one of the scratch codes at the verification prompt, then run google-authenticator again to create a new secret and scan it on the new device. Scratch codes are listed at the bottom of the secret file.

Does this work with PuTTY and other clients?

Yes. Any client that supports keyboard interactive authentication shows the code prompt after the key is accepted, including PuTTY, the OpenSSH client and most SFTP tools.

Can I require the code only from unknown networks?

Yes, with a Match Address block in sshd that sets AuthenticationMethods publickey for trusted ranges, while the global setting keeps both factors for everyone else.

Why does SELinux matter here?

On Rocky and AlmaLinux sshd may not read or rewrite files with the generic home directory label. Files in ~/.ssh carry the ssh_home_t label, which sshd is allowed to use.

Can I use a hardware key instead?

OpenSSH supports FIDO2 keys directly with ed25519-sk key types, which need no PAM changes. TOTP remains useful when not every device can use a hardware key.