SSH 2FA With YubiKey or Google Authenticator

I wanted to enable Two Factor Authentication (2FA) on an SSH server, and be able to use either my YubiKey, or Google Authenticator if I don’t have my YubiKey with me.

Setting this up was pretty easy, I started with Google Authenticator

Google Authenticator libpam

  • Install Google Authenticator libpam either from source or use a package provided by your distribution
  • Configure ~/.google_authenticator
# google-authenticator 

Do you want authentication tokens to be time-based (y/n) y
https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@host%3Fsecret%3DXXXXXXXXXXXXXXXX%26issuer%3Ddb
Your new secret key is: XXXXXXXXXXXXXXXX
Your verification code is 123456
Your emergency scratch codes are:
  12345671
  12345672
  12345673
  12345674
  12345675

Do you want me to update your "/root/.google_authenticator" file (y/n) y

Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y

By default, tokens are good for 30 seconds and in order to compensate for
possible time-skew between the client and the server, we allow an extra
token before and after the current time. If you experience problems with poor
time synchronization, you can increase the window from its default
size of 1:30min to about 4min. Do you want to do so (y/n) n

If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting (y/n) n
  • Configure PAM, your milage will vary depending on how your distribution configures PAM, but in my case I changed /etc/pam.d/sshd

from:

# cat /etc/pam.d/sshd 
auth       include      system-remote-login
account    include      system-remote-login
password   include      system-remote-login
session    include      system-remote-login
#

to:

# cat /etc/pam.d/sshd 
auth       include      system-remote-login
auth       required     pam_google_authenticator.so
account    include      system-remote-login
password   include      system-remote-login
session    include      system-remote-login
#
  • Test it and ensure it works (keep a root shell open in case you’re locked out!)
# ssh 192.168.12.21
Password: 
Verification code: 
host ~ #

Yubico pam

  • Install Yubico Pam from source or use a package provided by your distribution
  • Configure PAM, I changed /etc/pam.d/sshd to:
# cat !$
cat /etc/pam.d/sshd
auth       include      system-remote-login
auth       [success=done new_authtok_reqd=ok default=ignore] pam_yubico.so id=16 nullok
auth       required     pam_google_authenticator.so
account    include      system-remote-login
password   include      system-remote-login
session    include      system-remote-login

Et voila, now when I SSH to this server and enter my password, I can use by YubiKey to complete the two factor authentication if I have it to hand:

# ssh 192.168.12.21
Password: 
YubiKey for `root': 
host ~ # 

and if I don’t, I simply hit enter at the YubiKey prompt and complete two factor with Google Authenticator.

# ssh 192.168.12.21
Password: 
YubiKey for `root': 
Verification code: 
host ~ # 

The key to how this works is setting success=done and default=ignore, which means that if Yubico PAM authentication succeeds, the authentication process finishes without going onto pam_google_authenticator.so. If Yubico PAM fails, it is ignored and continues to Google Authenticator.