Use FIDO2 Security Keys for SSH Authentication
Before you begin
- Applies to:OpenSSH key authentication using a FIDO2 security key.
- Where to start:Start on the client with the security key connected. Keep an approved fallback session or server console available.
- What you need:Have a compatible key and OpenSSH setup, an authorized test account, and a way to verify the new login before changing existing access.
Guide tools
OpenSSH security-key credentials (ecdsa-sk or ed25519-sk) use a FIDO authenticator to sign SSH challenges. The local .pub file is public; the local private-key handle is still needed, while the signing secret remains on the authenticator. A copied handle alone cannot authenticate without the registered hardware key.
A touch proves user presence. Requiring a PIN or biometric adds authenticator user verification, but SSH security-key login is not automatically multi-factor authentication in every policy or threat model. It does not protect a compromised client or replace host-key verification. This guide requires touch and user verification, uses two separately enrolled keys, and only then disables password-based SSH login.
Confirm the Client Supports Security-Key Algorithms
Client PreparationUse an OpenSSH client with FIDO security-key support and an authenticator that supports user verification. Check for an sk- key algorithm before creating a credential. Win32 OpenSSH support is available in version 8.9.0.0 and later; no vendor key-manager application is required for OpenSSH authentication.
ssh -Vssh.exe -Vssh.exe -Q key | findstr /i "sk-ssh-ed25519 sk-ecdsa-sha2-nistp256"❯ Illustrative Output (Results Vary)
OpenSSH_9.xGenerate a Primary Key and a Backup-Key Credential
Authenticator SetupUse a separate FIDO authenticator for the backup. Generate a credential for each device and protect each local key-handle file with a passphrase and appropriate file permissions. Keep the existing administrator session open while setting up and testing both credentials. If your authenticator cannot perform user verification, the verify-required option will fail; use a supported authenticator rather than removing the server requirement.
mkdir -p ~/.ssh && chmod 700 ~/.sshfor key in ~/.ssh/id_ecdsa_sk_primary ~/.ssh/id_ecdsa_sk_primary.pub ~/.ssh/id_ecdsa_sk_backup ~/.ssh/id_ecdsa_sk_backup.pub; do if [[ -e "$key" ]]; then printf 'Key file already exists; choose new paths to avoid replacing credentials: %s\n' "$key" >&2 exit 1 fidonessh-keygen -t ecdsa-sk -O verify-required -f ~/.ssh/id_ecdsa_sk_primary -C "admin-primary-key" || exit 1ssh-keygen -t ecdsa-sk -O verify-required -f ~/.ssh/id_ecdsa_sk_backup -C "admin-backup-key" || exit 1chmod 600 ~/.ssh/id_ecdsa_sk_primary ~/.ssh/id_ecdsa_sk_backup || exit 1$sshDir = Join-Path $env:USERPROFILE '.ssh'New-Item -ItemType Directory -Force -Path $sshDir | Out-Null$primaryKey = Join-Path $sshDir 'id_ecdsa_sk_primary'$backupKey = Join-Path $sshDir 'id_ecdsa_sk_backup'$keyPaths = @($primaryKey, ($primaryKey + '.pub'), $backupKey, ($backupKey + '.pub'))$existingKeyPaths = @($keyPaths | Where-Object { Test-Path -LiteralPath $_ })if ($existingKeyPaths.Count -gt 0) { throw 'A key file already exists; choose new paths to avoid replacing credentials.' }ssh-keygen.exe -t ecdsa-sk -O verify-required -f $primaryKey -C 'admin-primary-key'if ($LASTEXITCODE -ne 0) { throw "Primary key generation failed with exit code $LASTEXITCODE." }ssh-keygen.exe -t ecdsa-sk -O verify-required -f $backupKey -C 'admin-backup-key'if ($LASTEXITCODE -ne 0) { throw "Backup key generation failed with exit code $LASTEXITCODE." }❯ Illustrative Output (Results Vary)
Generating public/private ecdsa-sk key pair.Enter PIN for authenticator:You may need to touch your authenticator to authorize key generation.Your identification has been saved in .../id_ecdsa_sk_primaryYour public key has been saved in .../id_ecdsa_sk_primary.pubInstall Both Public Keys and Verify New Sessions
Access ValidationAdd both public keys to the target account while the current administrative session remains open. Use the matching client commands below, then connect once with each authenticator and confirm the PIN or biometric and touch prompt succeeds. Do not continue until both credentials work and you have a separate recovery path approved by your organization.
cat ~/.ssh/id_ecdsa_sk_primary.pub | ssh [email protected] 'set -eu; umask 077; mkdir -p ~/.ssh; chmod 700 ~/.ssh; touch ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys; IFS= read -r key || exit 1; case "$key" in [email protected]*) ;; *) exit 1 ;; esac; if grep -qxF "$key" ~/.ssh/authorized_keys; then exit 0; else status=$?; [ "$status" -eq 1 ] || exit "$status"; fi; printf "%s\n" "$key" >> ~/.ssh/authorized_keys'cat ~/.ssh/id_ecdsa_sk_backup.pub | ssh [email protected] 'set -eu; umask 077; mkdir -p ~/.ssh; chmod 700 ~/.ssh; touch ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys; IFS= read -r key || exit 1; case "$key" in [email protected]*) ;; *) exit 1 ;; esac; if grep -qxF "$key" ~/.ssh/authorized_keys; then exit 0; else status=$?; [ "$status" -eq 1 ] || exit "$status"; fi; printf "%s\n" "$key" >> ~/.ssh/authorized_keys'
# Test each key in a separate new session; the second command uses the backup authenticator.$sshDir = Join-Path $env:USERPROFILE '.ssh'$primaryPublicPath = Join-Path $sshDir 'id_ecdsa_sk_primary.pub'$backupPublicPath = Join-Path $sshDir 'id_ecdsa_sk_backup.pub'if (-not (Test-Path -LiteralPath $primaryPublicPath -PathType Leaf) -or -not (Test-Path -LiteralPath $backupPublicPath -PathType Leaf)) { throw 'Both public key files must exist before enrollment.' }$primaryPublicKey = (Get-Content -Raw $primaryPublicPath).Trim()$backupPublicKey = (Get-Content -Raw $backupPublicPath).Trim()if ($primaryPublicKey -notmatch '^sk-ecdsa-sha2-nistp256@openssh\.com\s' -or $backupPublicKey -notmatch '^sk-ecdsa-sha2-nistp256@openssh\.com\s') { throw 'The public key files do not contain the expected FIDO ECDSA key type.' }$primaryPublicKey | ssh.exe [email protected] 'set -eu; umask 077; mkdir -p ~/.ssh; chmod 700 ~/.ssh; touch ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys; IFS= read -r key || exit 1; case "$key" in [email protected]*) ;; *) exit 1 ;; esac; if grep -qxF "$key" ~/.ssh/authorized_keys; then exit 0; else status=$?; [ "$status" -eq 1 ] || exit "$status"; fi; printf "%s\n" "$key" >> ~/.ssh/authorized_keys'if ($LASTEXITCODE -ne 0) { throw "Primary public key enrollment failed with exit code $LASTEXITCODE." }$backupPublicKey | ssh.exe [email protected] 'set -eu; umask 077; mkdir -p ~/.ssh; chmod 700 ~/.ssh; touch ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys; IFS= read -r key || exit 1; case "$key" in [email protected]*) ;; *) exit 1 ;; esac; if grep -qxF "$key" ~/.ssh/authorized_keys; then exit 0; else status=$?; [ "$status" -eq 1 ] || exit "$status"; fi; printf "%s\n" "$key" >> ~/.ssh/authorized_keys'if ($LASTEXITCODE -ne 0) { throw "Backup public key enrollment failed with exit code $LASTEXITCODE." }
# Test each key in a separate new session.❯ Illustrative Output (Results Vary)
Authenticated with ecdsa-sk key; user verification and touch completed.Keep the original administrator session open until both test logins succeed.Restrict Accepted Keys and Require User Verification
Server HardeningConfirm your server’s OpenSSH version supports these directives and that every SSH-enabled account that must retain access has both keys enrolled. These directives have global server scope: they accept only OpenSSH security-key algorithms, require user presence and verification, and disable password and keyboard-interactive authentication for every SSH account. Check the effective values with sshd -T -C for every relevant account and client context. Replace the example user, client host, and address in the command below; if effective values differ, resolve configuration include or Match precedence before reloading. Keep your current session and an out-of-band recovery route available while applying the change.
sudo install -d -m 0755 /etc/ssh/sshd_config.d || exit 1if sudo test -e /etc/ssh/sshd_config.d/99-security-key-only.conf; then printf 'Config file already exists; inspect and back it up before changing it.\n' >&2 exit 1fisudo tee /etc/ssh/sshd_config.d/99-security-key-only.conf >/dev/null <<'EOF' || exit 1PubkeyAcceptedAlgorithms [email protected],[email protected]PubkeyAuthOptions touch-required,verify-requiredAuthenticationMethods publickeyPasswordAuthentication noKbdInteractiveAuthentication noEOF
# Validate syntax and inspect the effective global settings before reloading.sudo sshd -t || exit 1sudo sshd -T | grep -E '^(pubkeyacceptedalgorithms|pubkeyauthoptions|authenticationmethods|passwordauthentication|kbdinteractiveauthentication) ' || exit 1sudo sshd -T -C user=admin,host=client.example.com,addr=192.0.2.10 | grep -E '^(pubkeyacceptedalgorithms|pubkeyauthoptions|authenticationmethods|passwordauthentication|kbdinteractiveauthentication) ' || exit 1❯ Illustrative Output (Results Vary)
pubkeyacceptedalgorithms [email protected],[email protected]pubkeyauthoptions touch-required,verify-requiredauthenticationmethods publickeypasswordauthentication nokbdinteractiveauthentication noChoose the service name for your distribution, then reload the daemon:
sudo systemctl reload ssh || exit 1sudo systemctl reload sshd || exit 1Open Fresh Sessions with Both Keys
Final VerificationFrom a second terminal, open a new session with the primary key and then the backup key. Confirm that each requires the intended verification and touch. Only close the original session after both logins succeed and the recovery path is confirmed. Repeat the review for every account affected by the server-wide policy.
$sshDir = Join-Path $env:USERPROFILE '.ssh'❯ Illustrative Output (Results Vary)
Both newly opened sessions authenticated with their separately enrolled security keys.Password and keyboard-interactive SSH authentication are disabled by the effective server policy.Your feedback keeps these production procedures accurate and battle-tested.