With two-factor authentication, logging in via SSH requires two things: your SSH key (or your password) and a 6-digit code that changes every 30 seconds on your phone. Even if someone steals your private key, they won’t be able to log in without your phone. This guide sets it up on Ubuntu and Debian using the Google Authenticator PAM module, which works with any TOTP app, such as Google Authenticator, Aegis, or 2FAS.
Before you touch anything
- Leave an SSH session open throughout the entire process and run the tests in another terminal. If something goes wrong, you can undo the changes from that session.
- Keep the panel's VNC console handy—it doesn't go through SSH, and it's your way in if you get locked out.
- Check the server time using
timedatectl. The codes depend on the clock: if it is more than one minute fast or slow, the server will reject them.
1. Install the module
sudo apt updatesudo apt install -y libpam-google-authenticator2. Generate the secret for your user
Run it as the user you log in with via SSH, without sudo: the secret is stored in your home directory.
google-authenticatorAnswer the questions as follows:
- Time-based codes: yes. Scan the QR code with your app.
- Update the
~/.google_authenticatorfile: yes. - Prevent the same code from being used twice: yes.
- Extend the time window: no, unless the server's clock is unreliable.
- Limit login attempts: yes.
Write down the emergency codes listed at the end and store them off the server. Each one can only be used once, in case you lose your phone.
3. Request the code in PAM
Edit /etc/pam.d/sshd and add this line at the end:
auth required pam_google_authenticator.soThis way, the module requires a password from all users. Before continuing, run google-authenticator on every account that logs in via SSH: any account without a password will be locked out.
If you log in using an SSH key, also comment out line @include common-auth in the same file. That way, after entering the SSH key, the server will only ask for the code and not the password.
There is the nullok option, which allows users who haven't configured it yet to log in without a code, but it only works if you keep common-auth enabled and log in with a password. With the SSH key enabled and common-auth commented out, those accounts are still locked out.
4. Enable two-factor authentication for SSH
Create your own file in /etc/ssh/sshd_config.d/. SSH uses the first value it finds. In Ubuntu 22.04 or later and in Debian 12, the main file includes that folder at the beginning and reads its files in alphabetical order, so the prefix 00- ensures that yours is read before the others in the folder:
sudo tee /etc/ssh/sshd_config.d/00-2fa.conf > /dev/null <<'EOF'UsePAM yesKbdInteractiveAuthentication yesAuthenticationMethods publickey,keyboard-interactiveEOFWith publickey,keyboard-interactive, the server first requests the SSH key and then the app code. If you still log in with a password, leave only keyboard-interactive and do not comment out common-auth: it will ask for your password and then the code.
In older versions, such as Ubuntu 20.04, the option KbdInteractiveAuthentication is called ChallengeResponseAuthentication.
Check the syntax and the values that SSH will actually apply to your user before restarting it. Replace YOUR_IP with the public IP address you're connecting from, so that the Match blocks affecting your connection are also taken into account. The restart will only be executed if both checks pass:
sudo sshd -t \ && sudo sshd -T -C user=$USER,host=YOUR_IP,addr=YOUR_IP | grep -Ei 'usepam|kbdinteractive|authenticationmethods' \ && sudo systemctl restart ssh5. Try it without logging out of your current session
Open a new terminal and connect. After accepting your SSH key, Verification code: should appear. Enter the code from the app.
Verification code:has appeared, you've entered the 6-digit code, and the new session has opened: only then can you close the backup session.- The server rejects the 6-digit code: Check the time using
timedatectland make sure the TOTP secret file,~/.google_authenticator, has 600 permissions. - SSH doesn't ask for the code: use this command to check if another file defines the same setting:
sudo grep -rn AuthenticationMethods /etc/ssh.
If you lose access to the server
Log in via the panel's VNC console. In /etc/pam.d/sshd, comment out the line containing pam_google_authenticator and uncomment @include common-auth; then delete /etc/ssh/sshd_config.d/00-2fa.conf and restart the service with systemctl restart ssh. You'll be logged in again as you were before you started.
ext step
Would you rather test it on a clean server before deploying it to production? Deploy a Cloud VPS and practice risk-free.