Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

How It Works

After this setup, a login needs two things: your SSH private key, and a six-digit code from an authenticator app on your mobile device. The code is a time-based one-time password (TOTP), computed from a shared secret and the current time. On the server, the Google Authenticator PAM module checks the code, and one sshd line asks for the key first and the code second, so bots without your key never reach the code prompt. Any standard TOTP app works. The setup affects SSH only; sudo still asks for the user's password.

Before You Start

  • Key login works: for a user with sudo; see ssh-copy-id.
  • Correct time: codes depend on the clock. timedatectl must report that the clock is synchronized; see how to change the timezone.
  • Keep your session open: test every step from a second terminal and close the first one only at the end.
  • Know the way back: the server console in your provider's control panel does not use SSH and is not affected.

Step 1: Install the PAM Module

# Ubuntu, Debian
sudo apt install libpam-google-authenticator

# AlmaLinux, Rocky Linux (from EPEL)
sudo dnf install epel-release
sudo dnf install google-authenticator

Step 2: Create the Secret for Your User

Log in as the user who will use the codes, not as root, and run:

google-authenticator
  1. Choose time-based tokens.
  2. Scan the QR code with your authenticator app, or type in the secret key shown below it, then enter the current code to confirm.
  3. Copy the emergency scratch codes to a password manager or another safe place off the server. Each one logs you in once without the app.
  4. Agree to update the file, to forbid reuse of a code, and to rate limiting. Keep the default time window unless your clocks drift.

The secret is stored in ~/.google_authenticator. Treat that file, the QR code, and the scratch codes like a private key: never paste them into a ticket, a chat, or a screenshot.

Step 3: Configure PAM

Open /etc/pam.d/sshd and add this line at the end:

auth required pam_google_authenticator.so nullok

This file also asks for the account password. For key plus code only, put a # in front of that line: on Ubuntu and Debian, the line near the top that includes common-auth; on AlmaLinux and Rocky Linux, auth substack password-auth. Leave all other lines as they are.

nullok skips users who have no secret file, but does not log them in: with the password line commented out, such a user cannot log in over SSH at all. Create a secret for every SSH account before you reload, then remove nullok.

Step 4: Configure the SSH Server

Keyboard-interactive login must be on; it carries the code prompt. sshd uses the first value it reads, and the drop-ins in /etc/ssh/sshd_config.d/ are read in name order before the rest of the main file. Find every file that sets these options, including 50-cloud-init.conf on cloud images:

sudo grep -riE 'kbdinteractive|challengeresponse|authenticationmethods' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/

Create /etc/ssh/sshd_config.d/00-2fa.conf, whose name sorts before the others, and change an older KbdInteractiveAuthentication no line, such as one from disabling passwords, to yes so the files agree:

PasswordAuthentication no
KbdInteractiveAuthentication yes
AuthenticationMethods publickey,keyboard-interactive

Password authentication stays off; the code prompt runs through keyboard-interactive and PAM. UsePAM yes, set by these distributions, must stay. Older guides use the deprecated alias ChallengeResponseAuthentication. Check and reload; the second command must show yes for both and the methods line above:

sudo sshd -t
sudo sshd -T | grep -Ei 'kbdinteractive|authenticationmethods|usepam'
sudo systemctl reload ssh     # sshd on AlmaLinux, Rocky Linux

See also how to disable SSH password authentication.

Step 5: Test from a Second Terminal

ssh user@your_server_ip

The key is used first, then the server asks for a verification code from the app. Test one scratch code too; it is used up afterward. If the code is refused, compare the server's time with date, and read the log with sudo journalctl -u ssh -n 50 (-u sshd on AlmaLinux and Rocky Linux). On those systems, also look for an SELinux denial with sudo ausearch -m avc -ts recent; see what is SELinux. Close the first session only after a full login works.

Undo the Change

From the open session or the console, delete 00-2fa.conf, set KbdInteractiveAuthentication no again in the file you changed, delete the line in /etc/pam.d/sshd and restore the line you commented out. Then run sudo sshd -t and reload SSH, as in how to restart the SSH service. Key-only login works as before.

Was this answer helpful?
Loading...