OGOPSEC // Dispatch
Two-Factor SSH: Adding a Hardware Key to Remote Access
Key-based SSH is a big step up from passwords, but it concentrates all of your security into one file: the private key. Two-factor SSH closes that gap by requiring something you have — a physical device — in addition to something you know or hold. If your key file is copied, it is no longer enough; the attacker also needs the hardware in their hand. This post covers the two solid, well-supported ways to add a second factor to SSH on Linux, and is clear about which threats each one does and does not address.
Option 1: FIDO2 security keys, built into OpenSSH
Since OpenSSH 8.2, the simplest form of two-factor SSH needs no extra modules at all. You generate a key whose private half is bound to a FIDO2 security key:
ssh-keygen -t ed25519-sk -C "you@workstation"
The -sk suffix means "security key." The private key material lives on the token, and authentication requires the device to be present — usually with a physical touch, and optionally a PIN. Install the resulting public key on the server exactly like any other:
ssh-copy-id you@server
To require this style of key and reject plain software keys, you can set verification options on the server side and use an authorized-keys policy that demands a verified security key. The practical effect: a copied key file is inert without the physical token.
Strengths: no PAM edits, no lockout risk beyond ordinary key management, and the "second factor" is genuinely hardware-bound. Limit: it protects the key, not the session — once authenticated, the session is as trusted as any other.
Option 2: A PAM second factor on top of keys
The other approach adds an independent second step through PAM, so a user must present a key and satisfy a second module. A common pairing is public-key auth plus pam_u2f (a FIDO2 touch) or a TOTP module.
First, register your security key:
pamu2fcfg > ~/.config/Yubico/u2f_keys
Then reference that module in the SSH PAM stack (/etc/pam.d/sshd) and tell the SSH daemon to actually run the keyboard-interactive and account stages so the second factor is enforced:
# /etc/ssh/sshd_config.d/99-2fa.conf
PubkeyAuthentication yes
KbdInteractiveAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
The AuthenticationMethods line is the heart of it: both factors are required, not either-or.
The warning that always comes with PAM: a misconfigured PAM stack can lock out everyone. Keep a separate root session open, and test from a new connection before you close it. Have your provider's console recovery path ready just in case.
Which should you choose?
For most people, ed25519-sk is the cleaner win: it is two-factor in spirit (key file plus physical token) with none of the PAM lockout hazard. Reach for the PAM approach when you need a second factor that is fully independent of the key, or when you want the requirement enforced across console and local logins too.
Anchoring the whole identity to hardware
Both options share a goal: make the credential something that cannot be silently copied off a disk. A device-bound credential such as Dire Wolf takes this to its conclusion — the key is anchored to a TPM on a USB token, never exported, and its PAM policy is fail-closed, so no authorized device means no session on any login path. Whether you assemble it yourself with ed25519-sk keys or use a turnkey token, the principle is the same.
What two-factor SSH protects against — and what it doesn't
Two-factor SSH defeats the scenario that key-only auth leaves open: a stolen or copied private key used from somewhere else. Without the physical device, the key is useless. That is a meaningful upgrade to your remote-access threat model.
It does not protect a session that is already open and unlocked on a compromised client, it does not stop an attacker who has physically stolen both your laptop and your token, and it is not a substitute for patching the SSH daemon or encrypting data at rest. Two-factor SSH raises the cost of a stolen credential from "trivial" to "needs the hardware" — a large and worthwhile jump, stated plainly for what it is.