Disclaimer: These instructions are for Ubuntu 19.10 and older.
1 Introduction
This guide covers how to secure a local Linux login using the HMAC-SHA1 Challenge-Response feature on YubiKeys. This does not work with remote logins via SSH or other methods. The commands in the guide are for an Ubuntu (or Ubuntu based - such as Linux Mint) system, but the instructions can be adapted for any distribution of Linux. See here for an article geared towards Red Hat and its derivatives. Note: If you have a Security Key (blue device) which does not support HMAC-SHA1 challenge-response, you will want to see the Ubuntu Linux Login Guide - U2F article instead.
2 Installing the Required Software
- If you haven’t already, Enable the Yubico PPA.
- Open Terminal.
- Run: sudo apt-get install libpam-yubico yubikey-manager
3 Configuring the YubiKey
Note: This section can be skipped if you already have a challenge-response credential stored in slot 2 on your YubiKey. You will have done this if you used the Windows Logon Tool or Mac Logon Tool.
- Open Terminal.
- Insert your YubiKey.
- Run: ykman otp chalresp -g 2
- Press Y and then Enter to confirm the configuration.
Repeat these steps for any additional YubiKeys you want associated with your account. Note: Setting up additional YubiKeys is strongly recommended so that if your YubiKey is lost or broken you are not locked out of your computer.
4 Associating the YubiKey(s) With Your Account
- Open Terminal.
- Insert your YubiKey.
- Run: ykpamcfg -2
If successful, you will see an output such as Stored initial challenge and expected response in '/home/<USER>/.yubico/challenge-<SERIAL>' where <USER> is your username and <SERIAL> is the serial number printed on the YubiKey.
If you have backup YubiKeys, repeat the steps above to associate them with your account. If you do not have a backup device available at this time, you can add one later using the same steps as long as you still have access to your account. Warning: Having a backup YubiKey is strongly recommended so that if your device is lost or broken, you will not be locked out of your computer.
If you would like to add additional layer of security you can change the output of the challenge-<SERIAL> file to an area of the OS where you'll need sudo permission to edit the file ( e.g. /etc ). After creating a directory named yubico ( sudo mkdir /etc/yubico ). when moving the challenge-response file to /etc/yubico the filename will need to be changed to username-<SERIAL> instead of challenge-<SERIAL>. You can move the file from ~/.yubico to /etc/yubico and rename to the login username by running the command (sudo mv ~/.yubico/challenge-<SERIAL> /etc/yubico/`whoami`-<SERIAL>).
Once the challenge-<SERIAL> file is moved to a safer location the PAM file will need to also be modified for this to function correctly. You will need to append the PAM file's configuration to have " chalresp_path=/etc/yubico " added to the end.
Warning: Please note that once you modify the /etc/pam.d/sudo file to require the YubiKey if you were to lose or misplace the YubiKey you will not be able to modify or change the file to remove the YubiKey requirement.
5 Configuring the System to Use the YubiKeys
5.1 Test Configuration with the Sudo Command
This section covers how to require the YubiKey when using the sudo command, which should be used as a test so that you do not lock yourself out of your computer.
- Open Terminal.
- Run: sudo nano /etc/pam.d/sudo
- Add the line below the “@include common-auth” line.
auth required pam_yubico.so mode=challenge-response
- Press Ctrl+O and then Enter to save the file. Be sure you do not close the Terminal window, otherwise you will not be able to revert the changes.
- Remove your YubiKey from the computer.
- Open a new Terminal.
- In the new Terminal, run: sudo echo test. When prompted, enter your password and press Enter.
- Even with the correct password, the authentication should fail as the YubiKey is not plugged in. If the authentication succeeds without the YubiKey, that indicates the Yubico PAM module was not installed or there is a typo in the changes you made to /etc/pam.d/sudo.
- Insert your YubiKey.
- Open a new Terminal and run sudo echo test again. When prompted, enter your password and press Enter.
Congrats! If the password was accepted this time you have configured the YubiKey and system correctly and can continue on to the next section for requiring the YubiKey to login. Note: if you do not want to require the YubiKey to run the sudo command, remove the line you added to the /etc/pam.d/sudo file.
5.2 Configuring the System to Require the YubiKey for Login
- Open Terminal.
- If your system is Ubuntu 17.10 or newer, run: sudo nano /etc/pam.d/gdm-password. If your system is Ubuntu 17.04 or older, run: sudo nano /etc/pam.d/lightdm
- Add the line below the “@include common-auth” line.
auth required pam_yubico.so mode=challenge-response
- Press Ctrl+X and then Enter to save and close the file.
5.3 Configuring the System to require the YubiKey for TTY terminal
- Open Terminal.
- run: sudo nano /etc/pam.d/login
- Add the line below after the “@include common-auth” line.
auth required pam_yubico.so mode=challenge-response - Press Ctrl+X and then Enter to save and close the file.
Success! You will no longer be able to log in to the computer without the YubiKey.
6 Alternate Configurations
6.1 Login with YubiKey Only (1FA)
If you would prefer to not require a password for login or running sudo, you can use auth sufficient instead of auth required in the configuration files. The location of these lines should be placed above the "@include common-auth" in the relevant configuration files
7 Troubleshooting
7.1 Running “ykpamcfg -2” Fails
If you see the “File … already exists, refusing to overwrite” error when running ykpamcfg -2, this indicates you have already associated this YubiKey with your account. If you want to clear the existing association to start again, you can run the command below in Terminal, where <SERIAL> is the serial of the YubiKey.
rm ~/.yubico/challenge-<SERIAL>
7.2 Enabling Debug Mode
If you are unable to login and are unsure why, you can enable debugging on the Yubico PAM module using the steps below. This provides insight into why the module is not allowing the login.
- Open Terminal.
- Run: sudo touch /var/log/pam_yubico.log
- If your system is Ubuntu 17.10 or newer, run: sudo nano /etc/pam.d/gdm-password. If your system is Ubuntu 17.04 or older, run: sudo nano /etc/pam.d/lightdm
- Add “ debug debug_file=/var/log/pam_yubico.log” to the end of the line that contains pam_yubico.so
Each subsequent login event will have the debug log saved in the /var/log/pam_yubico.log file.