Linux local authentication with fapico2

PAM can require the device at login: plug in, enter the PIN, touch — no password. The key is checked first, so a valid touch + PIN is sufficient on its own, and the password line below it is only reached without the device.

What you need

  • A fapico2 board, flashed and working, with a FIDO2 PIN set.
  • libpam-u2f (provides both pam_u2f.so and pamu2fcfg):
sudo apt install libpam-u2f

Register the key

sudo mkdir -p /etc/fapico2
pamu2fcfg -u "$USER" -N | sudo tee /etc/fapico2/u2f_mappings

Touch the device and enter your PIN. The mapping file records your username and the key's credential ID — one line per user. To add a second key for the same user, run pamu2fcfg again and append both handles on that line, comma-separated.

-N skips the user-verification request during registration; the PIN is still required at login, through pinverification=1 below.

Do not add -r. A resident credential stores no key handle in the file (the line reads user:*,publicKey,…), so pam_u2f has nothing to put in an allowList: it asks the device for any discoverable credential under pam://<hostname> and verifies whatever comes back against the single public key in the file. Any other resident credential under that RP can answer instead — a second registration, an older one, a different user handle — the signature check fails, and PAM falls through to the password silently. Non-resident keeps the key handle, so the allowList pins the one credential. nodetect already skips the presence probe, which is the only thing -r was buying.

Require the key at login

Add this to /etc/pam.d/common-auth, above the existing pam_unix.so line:

# 1. Check FIDO2 key first
auth sufficient pam_u2f.so authfile=/etc/fapico2/u2f_mappings cue pinverification=1 nodetect

What the line does:

  • sufficient — a valid touch + PIN authenticates on its own; without the device, the password is still accepted.
  • authfile= — where the mappings live.
  • pinverification=1 — the PIN is required, so a stolen or borrowed key alone is not enough.
  • cue — prints the touch prompt.
  • nodetect — skips the key-presence pre-check and always runs the full authentication.

Test it

Lock the screen or log out and back in — that is the path that has to work. su is a useful first check, but it is setuid root, so it does not prove the greeter or the locker can reach the device:

su "$USER"        # PIN → touch

You should be asked for the PIN and a touch — no password.

Keep a root shell open while you test. A broken common-auth can lock you out of the machine; with a root shell open you can revert the edit. To revert, remove the line you added.

When the key stops authenticating

The login falls back to the password without saying anything, because pam_u2f returns "not handled" rather than an error. Add debug to the pam_u2f.so line, attempt a login, and read what it decided:

grep pam_u2f /var/log/auth.log

Remove debug afterwards — it turns on libfido2's CTAP2 tracing too.

If the file was registered with -r (a user:*,publicKey,… line), clear the stale resident credentials and register again non-resident:

fido2-token -D -r "pam://$(hostname)" /dev/hidraw*   # touch + PIN
pamu2fcfg -u "$USER" -N | sudo tee /etc/fapico2/u2f_mappings

Use the packaged pamu2fcfg from libpam-u2f. A hand-built one in /usr/local/bin shadows it, and a different version can write an authfile the installed pam_u2f.so reads differently — check with type -a pamu2fcfg.

sudo too

To require the device at sudo the same way, add the same line to /etc/pam.d/sudo. Test it with a second terminal before closing one that holds a root shell.