Articles / Security

  • fido2
  • sudo
  • pam
  • linux
  • hardware-key
  • security

How to Require a FIDO2 Key for sudo Without Locking Yourself Out

Making sudo demand a hardware key takes one line of PAM config. Get that line wrong, though, and the tool you would use to fix it is the tool you just broke. So most of this guide is about the order you do things in.

The goal is password and key touch, using Yubico's pam_u2f module. The commands and paths are for Ubuntu 26.04, which ships sudo-rs and pam-u2f 1.4.0. Other distributions use different package names and PAM files.

Check that this machine is a sensible target

The key must be plugged into the machine running sudo. If you SSH into a box and touch the key on your laptop, nothing happens, because this PAM module looks for a local authenticator. If it's a headless server you only reach over SSH, stop here.

You also want:

  • two FIDO authenticators, one for daily use and one spare
  • a way to boot rescue media, plus your disk encryption passphrase if you have one
  • pam-u2f 1.3.1 or newer, or a distribution package with the fix for CVE-2025-23013, a partial authentication bypass
sudo apt install libpam-u2f pamu2fcfg pamtester

Register both keys with a fixed origin

Plug in only the main key and run the first command below as your normal user, touching the key when it blinks. Then swap to the spare and run the second. If your key has a PIN, you may be asked for it.

main=$(pamu2fcfg -o pam://my-sudo)

# swap keys now
spare=$(pamu2fcfg -o pam://my-sudo -n)

printf '%s%s\n' "$main" "$spare" > u2f_mappings
cat u2f_mappings

You should see one line: your username, then two credentials separated by colons. The -n flag leaves the username off the second registration so it can be appended. Don't run pamu2fcfg under sudo, or you will register the keys for root.

Quick detour, because -o is doing something interesting. A FIDO credential is bound to a relying party ID, the thing that stops a phishing site using your login for a real one. pam_u2f defaults it to pam:// plus the hostname. Rename the machine and your perfectly good key no longer matches. Pick a fixed origin and the problem goes away.

Keep the key list owned by root

The default list lives at ~/.config/Yubico/u2f_keys. That means anything running as you can rewrite the list of keys that protect your route to root, which is a bit daft. Use a central, root-owned file instead.

sudo install -o root -g root -m 0644 u2f_mappings /etc/u2f_mappings

The central file holds one line per user. It also avoids a nasty trap with encrypted home directories, where the mapping cannot be read until something else has unlocked them.

Test on a PAM service that nothing uses

PAM picks its config by service name, so you can invent one. Prove the module works there before going near sudo. Create /etc/pam.d/u2f-test with sudoedit /etc/pam.d/u2f-test:

auth    required  pam_u2f.so authfile=/etc/u2f_mappings origin=pam://my-sudo cue
account required  pam_permit.so

Then ask pamtester to authenticate against it:

pamtester u2f-test "$USER" authenticate

Run it three times:

  • with the main key, which should succeed after a touch
  • with the spare key, which should also succeed
  • with no key, which must fail

The last test catches configurations that quietly let everyone through. If it passes without a key, fix that first. Adding debug to the module line will show you what it is doing.

Arm a rollback before editing sudo

Open a second terminal, run sudo -s, and leave that root shell open until the very end. In it, back up both files and schedule their restoration:

cp -a /etc/pam.d/sudo /root/pam-sudo.bak
cp -a /etc/pam.d/sudo-i /root/pam-sudo-i.bak

systemd-run --unit=pam-revert --on-active=15min /bin/sh -c \
  'cp -a /root/pam-sudo.bak /etc/pam.d/sudo
   cp -a /root/pam-sudo-i.bak /etc/pam.d/sudo-i'

This is a dead man's switch: unless you cancel it, the old config comes back in 15 minutes, even if you have closed every terminal. It is a temporary timer, so it will not survive a reboot. Don't reboot mid-change.

Add the line to both sudo services

From the root shell, edit /etc/pam.d/sudo and /etc/pam.d/sudo-i. The second is used for sudo -i, so changing only the first leaves a password-only route. In each, add this immediately above @include common-auth:

auth    required  pam_u2f.so authfile=/etc/u2f_mappings origin=pam://my-sudo cue
@include common-auth

required means this check has to pass, and the rest of the stack still runs. You will be asked to touch the key, then for your password. cue prints the reminder to touch it, without which a waiting sudo looks rather like a hung one.

Two options deserve care:

  • sufficient in place of required means a successful key check is enough on its own, with the password as fallback. That is convenient, but it does not require the key.
  • nouserok lets authentication succeed for users with no usable entry, including when the file is missing or malformed. Leave it out here.

Without nouserok, every account that uses sudo needs a line in the central file. Register those users before you switch this on.

Verify from a fresh terminal, then disarm

Open a third terminal. sudo -k throws away cached credentials, which otherwise make a broken setup look healthy.

sudo -k
sudo true       # touch, then password

sudo -k
sudo -i true    # same again

Repeat with the spare, then with no key, where both commands should refuse you. Once all of that behaves, go back to the root shell and cancel the rollback:

systemctl stop pam-revert.timer
rm /etc/pam.d/u2f-test

If the timer fired while you were still testing, your "no key" test will suddenly succeed with just a password. Check that the new line is still in both files. Then run one last sudo -k and sudo true before closing the root shell.

Look for the ways round it

A NOPASSWD rule skips authentication, so your shiny key is never consulted for commands it covers. Search from a root shell:

grep -r NOPASSWD /etc/sudoers /etc/sudoers.d

Cached credentials also mean you won't be asked every time. If you want a touch on each invocation, add Defaults timestamp_timeout=0 using visudo.

Hang on, is sudo even the only door? No, and these have their own rules:

  • su, which uses its own PAM service
  • polkit, behind pkexec, run0 and many graphical admin prompts
  • membership of the docker group, which is effectively root access

So what you have built is a key requirement for sudo, which falls short of a key requirement for root. It still stops a stolen password being enough at the sudo prompt. To go further, audit each of those routes and test it separately, with the same rollback habit.

If you lose a key, or lock yourself out anyway

If you lose one key, use the spare to run sudo, register a replacement, and rewrite the line without the lost credential. Do it promptly, because you are now one pocket accident from recovery media.

If both are gone and no root shell is open, boot a live USB, unlock and mount the root filesystem, and delete the pam_u2f.so line from both files. Polkit may also still let an administrator in with a password, but test that before relying on it.

Anyone with your unencrypted disk can make exactly the same edit from a live USB. The key protects the running system; disk encryption has to protect it when it is switched off.