413 Words

7. Linux Login with PAM (sudo, desktop, TTY)

← 6. age encryption · Overview · Next: 8. Troubleshooting →

Goal: require your YubiKey (touch) for sudo, su, the desktop login (GDM) and TTY login, using pam_u2f (FIDO2/U2F).

🚨 You can lock yourself out. Keep a root shell open in a second terminal (sudo -i) while you change PAM files, and test before you log out. If something breaks, remove the line you added.


7.1 Install

sudo apt install libpam-u2f pamu2fcfg     # Debian/Ubuntu
sudo dnf install pam-u2f pamu2fcfg        # Fedora

7.2 Register your YubiKey(s)

mkdir -p ~/.config/Yubico
pamu2fcfg > ~/.config/Yubico/u2f_keys      # touch the key when it blinks

Add a backup YubiKey (insert it, then):

pamu2fcfg -n >> ~/.config/Yubico/u2f_keys

-n leaves out the username so the second key is appended to the same line. The file should contain one line: username:key1...:key2...


7.3 Two modes

ModePAM lineEffect
🔐 2FAauth required pam_u2f.soPassword and YubiKey touch
🛡 Passwordlessauth sufficient pam_u2f.soYubiKey touch or password

Put the line at the top of the PAM file (before the other auth lines).


7.4 Ubuntu / Debian — edit the PAM files

Start with sudo — it’s the easiest to test.

WhatFile
sudo/etc/pam.d/sudo
su/etc/pam.d/su
Desktop login (GNOME)/etc/pam.d/gdm-password
TTY login/etc/pam.d/login
sudo nano /etc/pam.d/sudo

Add at the top:

auth       sufficient   pam_u2f.so cue

(cue prints “Please touch the device.” so you know it’s waiting.)

Test in a new terminal:

sudo -k && sudo ls

Works? Repeat for the other files.


7.5 Fedora — use authselect

Fedora manages PAM through authselect; don’t edit /etc/pam.d/system-auth by hand (it gets overwritten).

authselect current                                # show current profile + features

# Passwordless: YubiKey OR password (sufficient)
sudo authselect enable-feature with-pam-u2f

# — or — 2FA: password AND YubiKey (required)
sudo authselect enable-feature with-pam-u2f-2fa

sudo authselect apply-changes

This enables the YubiKey for everything that uses system-auth (sudo, su, GDM, TTY).

Just want it for sudo only? Add at the top of /etc/pam.d/sudo:

auth       sufficient   pam_u2f.so cue

Undo:

sudo authselect disable-feature with-pam-u2f       # or with-pam-u2f-2fa
sudo authselect apply-changes

7.6 Test before you log out

  1. Keep a root shell open: sudo -i in a second terminal.
  2. Test sudo -k && sudo ls in a new terminal.
  3. Test a TTY login: Ctrl+Alt+F3, log in, then back with Ctrl+Alt+F1/F2.
  4. Only then log out and test the desktop login.

Not working? Remove the line / disable the authselect feature from your open root shell.


← 6. age encryption · Overview · Next: 8. Troubleshooting →