What is Yubikey?
YubiKeys are small USB dongles that you can plug into your computer. They can simulate keyboard input, allowing you to enter One Time Passwords (OTPs) with the press of a button to authenticate with services like Google, Dropbox and GitHub.
YubiKeys can also be used when logging into a remote server. This guide will show you how to configure your server so that a YubiKey must be plugged in and tapped in order to log in to your server using ssh
. Depending on your needs, you can also configure a password in addition to the YubiKey for an extra level of security.
If you want to work through this guide but donât have a YubiKey, you can find one at this link. As of this writing, any key that supports âYubico OTPâ will support two-factor SSH authentication.
Before You Begin
- This guide will useÂ
sudo
 wherever possible. You should create a standard user account, harden SSH access and remove unnecessary network services. - Update your local system. Then update your server using the following:
sudo apt-get update && sudo apt-get upgrade
- Test your YubiKey at demo.yubico.com to make sure itâs working correctly.
Configure Your YubiKey
If your YubiKey still has its default configuration, you can skip this step. If youâve made some changes, this section will tell you how to put the appropriate configuration for this guide onto slot 1 of your YubiKey. If you want to use a different slot, make sure you select it instead of slot 1 in the following instructions.
- Install the YubiKey Personalization Tool for your system and open it.
- Click on the âYubico OTPâ menu in the top-left corner, and select âQuickâ. Your screen should look like the one below.
- Click âWrite Configurationâ. Click âCancelâ on the pop-up window that asks where to save the log file.
- Now select âUpload to Yubicoâ. In the web form that opens, fill in your email address. Select the field asking for an âOTP from the YubiKeyâ and touch the button on your YubiKey (or touch and hold if you programmed slot 2). This should fill the field with a string of letters. Complete the captcha and press âUpload AES keyâ.NoteThe page will respond with a table containing your key information. You should keep this data in a safe place. Should you ever lose your YubiKey, you will need this data to reconfigure a new one with the same settings.
- Test that your key works by following the instructions for single-factor authentication on demo.yubico.com. If it doesnât, you may need to wait up to 15 minutes for your key to process on their servers.
Install the Authentication Software
- Register for an API key here, by entering your email address and (with the âYubiKey one time passwordâ field selected) touching the button on your YubiKey. Keep the Client ID and Secret Key returned by the website.NoteOn Ubuntu, you may need to installÂ
software-properties-common
 andÂpython-software-properties
 to add the repository. - On your Server, install theÂ
pam_yubico
 package.
On Ubuntu:sudo add-apt-repository ppa:yubico/stable sudo apt-get update sudo apt-get install libpam-yubico
On Debian (Wheezy):sudo apt-get install libpam-yubico
On Fedora/EPEL/Arch Linux:sudo yum install pam_yubico
Yubicoâs documentation also has instructions on how to buildÂpam_yubico
 from source.
Note
You may need to moveÂpam_yubico.so
 to wherever PAM modules are stored on your system (usuallyÂlib/security
). The Ubuntu package will automatically install the module in the appropriate location, but you can check to see whether itâs in the right location withÂls /lib/security
. It may also be stored inÂ/usr/local/lib/security
, in which case you will need to move it manually. - Create the fileÂ
/etc/ssh/authorized_yubikeys
:sudo touch /etc/ssh/authorized_yubikeys
- Populate this file with the usernames for which you want to enable two-factor authentication and their YubiKey IDs. You can obtain the ID by opening a text editor and touching the button on the YubiKey, and selecting only the first 12 characters. The first line below would be a typical configuration. The subsequent lines show a configuration where usersÂ
user2
,Âuser3
, andÂuser4
 use multiple YubiKeys and plan to access the server with all of them./etc/ssh/authorized_yubikeysuser1:vvklhtiubdcu user2:ccurrufnjder:ccturefjtehv:cctbhunjimko user3:ccdvnvlcbdre:vvvglinuddek user4:vvddhfjjasui:vvfjidkflssd
- AddÂ
auth required pam_yubico.so id=<client id> key=<secret key> authfile=/etc/ssh/authorized_yubikeys
 to the start ofÂ/etc/pam.d/sshd
. ReplaceÂ<client id>
 with the ID you retrieved when applying for an API key, andÂ<secret key>
 with the secret key. If you only want single-factor authentication (either a YubiKey or a password), changeÂrequired
 toÂsufficient
 to tell the system that a valid YubiKey will be enough to log in./etc/pam.d/sshd# PAM configuration for the Secure Shell service # Add your line below this one # v v v v v v auth required pam_yubico.so id=client id key=secret key authfile=/etc/ssh/authorized_yubikeys # ^ ^ ^ ^ ^ ^ # Add your line above this one # Standard Un*x authentication. @include common-auth
Note
On some systems, like Arch Linux, you will need to editÂ/etc/pam.d/system-remote-login
 instead ofÂ/etc/pam.d/sshd
. - InÂ
/etc/ssh/sshd_config
, add or edit the following settings:/etc/ssh/sshd_configChallengeResponseAuthentication yes
UsePAM yes
If you want to only use a YubiKey for single-factor authentication, setÂPasswordAuthentication no
. - Restart the sshd daemon to allow the changes to take effect:
sudo systemctl restart sshd
Test the YubiKey
Now that this process is done, you can test your login by logging out and back in:
exit
ssh [email protected]
Depending on your setup, you may be prompted for your YubiKey. All you need to do is touch the button; it will enter the key for you. Then, type in your password if you are using multi-factor authentication. It will look something like the image below.

You can now log into your server.
Troubleshoot YubiKey
If you encounter any problems, make sure youâve followed all of the steps in this guide and restarted your server. If these steps donât solve your issues, you can enable logging, by following these steps:
- Add the wordÂ
debug
 to the end of the line you added inÂ/etc/pam.d/sshd
:/etc/pam.d/sshdauth required pam_yubico.so id=<client id> key=<secret key> authfile=/etc/ssh/authorized_yubikeys debug
- Create a debug log file:
sudo touch /var/run/pam-debug.log sudo chmod go+w /var/run/pam-debug.log
- Log data to this file:
sudo journalctl -f -l tail -f /var/run/pam-debug.log
- Log in again and analyze this file for clues as to what is causing the problem.
- Once youâre done, disable debugging by removing theÂ
debug
 flag fromÂ/etc/pam.d/sshd
. Then, delete the log file.