With two-step login, SSH asks for your password and then for a 6-digit code from an app on your phone. A stolen or guessed password alone is then not enough to log in. Any authenticator app for such codes works, for example Google Authenticator, Microsoft Authenticator, 2FAS or Aegis. The commands were tested on Ubuntu 26.04 LTS.

The code protects logins with a password. Logins with an SSH key are not asked for it. If your server already allows only SSH keys (Log In to Your Linux Server With an SSH Key), you do not need this page: step 3 would let password logins back in, now with a code.

⚠ Do not lock yourself out

Keep your current SSH window open until you have tested the login in a second window (step 4). If something goes wrong, the console in the Client Area still works: How to Use noVNC Remote Management for a Dedicated Server. For a VPS, use Console Access under Information → Console.


1. Install the Module

As root:

apt install libpam-google-authenticator

2. Set Up the Code for Your User

Log in as the user who will use the code (not as root, unless you log in as root with a password) and run:

google-authenticator

Answer the questions:

  1. Do you want authentication tokens to be time-based (y/n): y. A large QR code appears. Scan it with the app on your phone. If the window is too small for the QR code, type the line Your new secret key is: … into the app instead.
  2. Enter code from app (-1 to skip): type the code that the app now shows. The answer is Code confirmed.
  3. Your emergency scratch codes are: five 8-digit codes. Write them down and keep them away from the server: each of them logs you in once if you lose your phone.
  4. Do you want me to update your "/home/…/.google_authenticator" file?: y.
  5. Do you want to disallow multiple uses of the same authentication token?: y. Each code then works only once.
  6. Do you want to do so? (a larger time window): n. The time on the server and on your phone must be correct (How to Update the Time on Your Linux Server).
  7. Do you want to enable rate-limiting?: y. At most 3 attempts every 30 seconds.

Repeat this step for every user who logs in with a password.

3. Switch It On for SSH

As root, open the SSH login settings of PAM:

nano /etc/pam.d/sshd

Find the line @include common-auth and add this line right below it, so that the code is asked after the password:

auth required pam_google_authenticator.so nullok

Save with Ctrl+O, Enter, and leave with Ctrl+X. nullok lets users who have not set up a code yet log in with their password only; step 5 removes it. Then let SSH ask more than one question, check the settings and apply them:

echo "KbdInteractiveAuthentication yes" > /etc/ssh/sshd_config.d/70-2fa.conf
sshd -t && systemctl reload ssh

sshd -t prints nothing when the settings are correct. Your open SSH window stays connected.

4. Test It in a Second Window

Open a new terminal on your computer and log in as the user from step 2, with your own user name and server address:

ssh alex@ds12345.seedhost.eu

SSH asks for Password: and then for Verification code:. Type the code from the app. The right code logs you in. A wrong code is refused, even when the program tries the password alone, and the log shows Invalid verification code for alex:

journalctl -u ssh --since "10 min ago" | grep -i "verification\|Failed"

5. Make the Code Required for Everyone

When every user who logs in with a password has set up a code, remove the word nullok from the line in /etc/pam.d/sshd and run systemctl reload ssh. From then on, a user without a code cannot log in with a password. Such a user is asked for a code and refused.

If You Lose Your Phone

  • Log in with one of the emergency scratch codes instead of the code from the app. Each scratch code works once.
  • Then run google-authenticator again as that user: it creates a new secret and new scratch codes. Scan the new QR code; the old entry in the app stops working.
  • No scratch code left: log in as root with an SSH key or through the console, and run google-authenticator for the user, for example sudo -u alex -H google-authenticator.

Switch It Off

Remove the line from /etc/pam.d/sshd, delete the extra settings file and reload SSH:

rm /etc/ssh/sshd_config.d/70-2fa.conf
systemctl reload ssh

More protection against password guessing: Protect Your Linux Server With a Firewall and Fail2ban.


Related Guides

View all Linux guides

Was this answer helpful? 0 Users Found This Useful (0 Votes)