Passwords leak and get brute-forced, and private key files get copied. Two-factor authentication adds one more step on top: when you log in, you also type a 6-digit number from your phone that changes every 30 seconds. The Verification code or MFA code many bastion hosts ask for is the same thing. Here’s where those 6 digits come from, how to turn this on for your own Linux server, and the problems you’re likely to run into afterwards.
Where the 6 digits come from
These codes are TOTP (Time-based One-Time Passwords), defined in RFC 6238.
When you set it up, the server generates a random secret and shows it as a QR code, or as a string of Base32 characters like JBSWY3DPEHPK3PXP. You scan it with an authenticator app on your phone (Google Authenticator, Microsoft Authenticator, 1Password and the like), and the app keeps the secret. From then on, whenever a code is needed, the server and the app each do the same sum: divide the current time by 30 seconds to get a count, compute an HMAC-SHA1 of that count with the secret, and cut 6 digits out of the result. Same secret, same time, same number.
Two things follow:
- The app needs no network, but the clocks must be right. Your phone works the code out by itself, so it works in airplane mode. For the same reason, if the phone’s or the server’s clock is a minute or two off, the numbers don’t match. Servers usually allow a little slack, accepting the codes from 30 seconds either side as well. Run
timedatectlon the server and check thatSystem clock synchronizedsaysyes. - The secret is everything. Anyone who has it can work out your codes, so guard it like a password. Setup also gives you a few emergency scratch codes, each good once, for logging in if you lose your phone; write them down and keep them safe.
You can work codes out on the command line too: with oath-toolkit installed, oathtool --totp -b JBSWY3DPEHPK3PXP prints the current 6-digit code, handy for checking against the app.
Turning it on for a server
The usual way is Google’s PAM module. The steps below are for Debian and Ubuntu; on RHEL and its relatives, install the google-authenticator package from EPEL, and the rest is the same.
Step 1: install the module.
sudo apt install libpam-google-authenticator
Step 2: generate the secret. Run this as the user who’ll be logging in, without sudo:
google-authenticator
Answer y to the first question, Do you want authentication tokens to be time-based. A QR code appears, and below it the secret after Your new secret key is: and five emergency scratch codes. Scan the code with your app, and write down the secret and the scratch codes. For the questions after that: y to updating ~/.google_authenticator; y is a good answer to disallowing multiple uses of the same code; usually n to widening the time window; and y to rate-limiting.
Step 3: have SSH’s PAM configuration use it. Add this line at the end of /etc/pam.d/sshd:
auth required pam_google_authenticator.so nullok
nullok lets users who haven’t generated a secret yet log in as before, so you can bring people over one at a time; remove it once everyone is set up.
Step 4: turn on keyboard-interactive authentication in sshd. Make sure /etc/ssh/sshd_config has these two:
KbdInteractiveAuthentication yes
UsePAM yes
Older OpenSSH versions call the first one ChallengeResponseAuthentication. Ubuntu and some other systems read the files in /etc/ssh/sshd_config.d/ first, and values set there win, so check what actually takes effect with sudo sshd -T | grep -i kbdinteractive. Then reload:
sudo systemctl reload ssh
Before changing anything, keep a window open that’s already logged in, and try logging in from another window; close it only once you’re sure you can get in. If something is misconfigured, that window is your way back.
Why logging in with a key doesn’t ask for the code
Set up as above, a password login asks for Password: and then Verification code:. But log in with a key and you’re often straight in, with no code asked for at all.
That’s because SSH has several ways to authenticate, and each takes its own path:
- publickey: logging in with a key, checked by sshd itself, without PAM.
- password: the client sends one password, and that’s the only question it can answer.
- keyboard-interactive: the server asks a series of questions (
Password:,Verification code:), the client shows them, and you answer each in turn. This is the path PAM’s modules ask their questions on.
The code is asked for by PAM, and a key login doesn’t go through PAM, so nobody asks. To require both a key and a code, put this in sshd_config:
AuthenticationMethods publickey,keyboard-interactive
It means publickey first, then keyboard-interactive. keyboard-interactive will then also ask for the password, following the PAM configuration; for “key + code” only, comment out the @include common-auth line in /etc/pam.d/sshd (on RHEL, it’s auth substack password-auth). For “password + code”, keep PAM’s default configuration and leave this option out.
SFTP and scripts stop connecting
The code can only be given through keyboard-interactive, so tools that only log in with a password or a key can’t get in any more: some SFTP apps, rsync from cron, CI deployment scripts, Ansible and so on. The usual ways round it:
-
Make an exception for automation. At the end of
sshd_config, let one user, or one internal network, in with a key alone:Match User deploy AuthenticationMethods publickeyMatch Address 10.0.0.0/8does the same for connections from the internal network. AMatchapplies until the end of the file or the nextMatch, so it goes last. -
Reuse a connection that’s already logged in. OpenSSH’s connection sharing lets later ssh, scp and sftp commands use the connection the first login made, so you type the code once:
Host myserver ControlMaster auto ControlPath ~/.ssh/cm-%r@%h:%p ControlPersist 10mThe OpenSSH that comes with Windows doesn’t support this.
One more problem you may hit: with “disallow multiple uses” on, logging in twice within 30 seconds (opening a terminal and SFTP at the same time, say) gets the second login rejected, and you have to wait for the next code.
In ShellRS
ShellRS is a free, open-source SSH client. When the server asks for a code, ShellRS asks you, as it does for any keyboard-interactive question; or it can fill the code in for you:
- In the host dialog, under “Advanced”, check “Fill in one-time codes” and enter the Base32 secret from setup as the “OTP secret”. So write the secret after
Your new secret key isdown when you set things up; many apps don’t show it again once scanned. - “Prompt to match” can usually stay empty: ShellRS knows the usual wordings, such as Verification code, OTP, MFA and 2FA, and google-authenticator’s
Verification code:is among them. If your server asks in some unusual way, enter what it says. - Terminals, SFTP, port forwarding, the external CLI and “Test connection” all fill the code in, and so does the host when it serves as a jump host, so the “SFTP can’t connect” problem above doesn’t come up. When the server accepts each code only once, ShellRS waits for the next code before sending it, so that login takes a little longer.
- Codes are worked out from this computer’s clock, so a computer with the wrong time gets them rejected. The secret is kept only in the system keychain, and syncs if you use sync.
One thing to keep in mind: with the secret stored on your computer, that computer alone can work out the codes. Lose the computer and you lose the key and the codes together, which two-factor authentication can’t protect against; what it protects against is a password or a key leaking on its own.
More in the ShellRS manual, under “Advanced” in the Hosts section. ShellRS is available from the download page.