This is a guide for setting up system U2F authentication on PureOS. All of the files used for this configuration are stored outside of the home folder, so home folder encryption should be compatible with this approach. While I am using a YubiKey, any U2F-compatible device should do the trick.
Using this configuration, I have been prompted to authenticate with U2F in the following situations:
- system login
- unlocking lock screen
- sudo commands
- unlocking Settings panels
- installing software from Software
- unlocking attached LUKS drives
- âŚthere are probably other situations I have not run into yet.
Read this in itâs entirety before proceeding, as it is possible to lock yourself out partially or wholly.
1. Update PureOS and install dependencies:
-
sudo apt-get update && sudo apt-get full-upgrade
- All lines formatted as above are to be entered in the terminal.
-
If updates were installed, reboot before proceeding.
-
sudo apt-get install libpam-u2f scdaemon pamu2fcfg yubikey-manager
- While I need yubikey-manager, you may need something else if using a different make of U2F device. Refer to the documentation for your device as needed.
Do not proceed until you have these successfully installed.
2. Enable Linux udev rules for U2F devices:
udev is the Linux device manager which handles events when USB devices are added and removed. Custom rules need to be added to properly identify U2F devices and provide applications access. The rules file linked below supports multiple makes and models of U2F devices. If yours is not included in the list, see the device manufacturer about the udev rules needed for U2F.
-
sudo mkdir -p /etc/udev/rules.d/
- You may get an error that the folder exists. You can safely ignore that error.
-
Open in new window (ctrl+click link): https://raw.githubusercontent.com/Yubico/libu2f-host/master/70-u2f.rules
-
Select all and copy content (ctrl+a, ctrl+c)
-
nano 70-u2f.rules
-
Paste and save (ctrl + shift + v, ctrl + x, y, return)
-
sudo mv 70-u2f.rules /etc/udev/rules.d/
-
sudo udevadm control --reload-rules && sudo udevadm trigger
-
lsusb
If this was successful, you will see something describing your U2F device as an attached device. If you do not, stop here and troubleshoot this issue.
3. Register your U2F device(s):
Here we will tell the system which U2F devices can be used for authentication.
Primary U2F device:
- Connect U2F device.
mkdir u2f
pamu2fcfg --origin=pam://localhost > u2f/keys
- Touch the device when it lights up or you are otherwise prompted.
- Note that some U2F devices do not require a touch and do not light up.
- Note that some U2F devices do not require a touch and do not light up.
Backup U2F device(s):
When setting up security like this, it is always a good idea to have at least one backup U2F device that you store somewhere safe â and almost always a bad idea to not have a spare. If you do not have a spare to configure now, you can do so later.
- Connect backup U2F device.
pamu2fcfg -n --origin=pam://localhost >> u2f/keys
- If needed, touch the device when it lights up or you are otherwise prompted.
- Repeat for all backup devices.
Move registration to /etc:
sudo mv u2f /etc
4. Make U2F an authentication requirement:
As far as I have experienced so far, this method requires U2F for literally every situation in which you must enter your password. It is possible to require it for only certain situations, such as login, sudo, unlocking the screen, etc. See @lipuâs guide and the other sources listed at the bottom of this guide for more information on that approach.
-
sudo nano /etc/pam.d/common-auth
-
Add (or, if adding new keys after initial setup, un-comment) this to the bottom of the file:
auth required pam_u2f.so cue no detect origin=pam://localhost authfile=/etc/u2f/keys
- Configuration options from most secure to least:
-
Omit âcue no detectâ to prevent prompting of any kind. Incorrect password and absent key failures both appear to be an incorrect password to the user.
-
Leave âcue no detectâ in the above line if you want to be prompted to touch the device only when it is present. An absent key appears to be an incorrect password.
-
Omit âno detectâ to always be prompted to touch the device, even when the key is not present. Arguably the least secure setup, as it reveals that a second factor exists when a key is absent.
-
- Configuration options from most secure to least:
-
Save and exit (ctrl+x, y, return).
-
DO NOT CLOSE TERMINAL WINDOW
5. CRITICAL: Test U2F Authentication:
Without closing current terminal, open a new terminal window.
-
sudo echo success
-
Enter account password, as usual for sudo.
-
If so configured, you will be prompted to touch the device, and the U2F device will then blink (if supported); touch it.
If you are not prompted to touch the device when expected (depends on configuration outlined above), or the device does not blink when expected, your password fails to be accepted, or authentication otherwise fails:
-
Close test terminal window (in which you ran echo), and return to the previous terminal window (in which you edited common-auth).
-
sudo nano /etc/pam.d/common-auth
-
Remove the line you added above:
auth required pam_u2f.so cue no detect origin=pam://localhost authfile=/etc/u2f/keys
-
Save and exit (ctrl+x, y, return).
-
sudo rm -r /etc/u2f
-
Close terminal, and re-read this before trying again.
If everything was done correctly, you will see âsuccessâ.
- Repeat the test phase for each registered U2F device.
- You can now safely close terminal and go about your day with the added peace of mind knowing that you have system U2F authentication!
Appendix: Adding additional U2F device(s) later:
-
sudo nano /etc/pam.d/common-auth
-
Comment out (add â#â to front of the line):
auth required pam_u2f.so cue no detect origin=pam://localhost authfile=/etc/u2f/keys
-
Save and exit (ctrl+x, y, return).
-
sudo rm -r /etc/u2f
-
Begin again at Step 3, above.
Sources & Inspirations:
- Using U2F for Two-factor Authentication Kudos @lipu!!
- Using Your U2F YubiKey with Linux
- Enable YubiKey U2F Support
- Ubuntu Linux Login Guide - U2F
- Two-factor auth for local logins in Debian using U2F keys
- GitHub: Yubico / pam-u2f: Adds ânodetectâ option to skip detection of suitable key
Updates:
- Added pam configuration options in section 4 based on an option I learned about.
- Clarified section on adding additional keys later.
- De-YubiKeyâd the guide; added some notes about other device manufacturers.
- Changed method of obtaining udev rules from curl to copy, paste, move.