ssh-keygen
ssh-keygen is the complete OpenSSH toolkit for key pairs, SSH certificates, revocation lists and file signatures. Knowing it well lets you replace copy-pasted authorized_keys with a short-lived, revocable certificate setup.
141.1 What it does, where it runs
- Creates key pairs, changes their passphrase/comment/format, signs certificates and files, and generates DH moduli.
- Windows 10 (1809+) / 11: built in with the OpenSSH Client feature.
- Linux: openssh-client (apt) or openssh-clients (dnf). macOS: already installed.
- Lab: admin 192.168.161.10 holds the user keys and the CA, srv 192.168.161.20 runs sshd (PasswordAuthentication no).
141.2 Generating keys
- -t picks the type, -b the bits, -C the comment, -N the passphrase ("" = none), -f the file name (.pub is added for the public key).
- -q is quiet; ECDSA allows only 256, 384 or 521 bits, so -b 300 gives a real error.
- The private key file is -rw------- (owner only); the .pub file is world-readable.
- -a raises KDF rounds (default 16) to slow brute force on a stolen key file; -A creates all missing host keys.
ssh-keygen -t ed25519 -C dev@n141 \ -N lab-key-141 -f ~/.ssh/id_ed25519ssh-keygen -q -t rsa -b 3072 -C ci@n141 \ -N '' -f ~/.ssh/id_rsa_cissh-keygen -t ecdsa -b 300 -N '' \ -f /tmp/bad_ecdsals -l ~/.sshhead -1 ~/.ssh/id_ed25519$ ls -l ~/.ssh-rw------- 1 root root 444 Nov 9 04:36 id_ed25519-rw-r--r-- 1 root root 90 Nov 9 04:36 id_ed25519.pub-rw------- 1 root root 2590 Nov 9 04:36 id_rsa_ci-rw-r--r-- 1 root root 561 Nov 9 04:36 id_rsa_ci.pub$ head -1 ~/.ssh/id_ed25519-----BEGIN OPENSSH PRIVATE KEY-----<hidden>| Type | Bits | Note |
|---|---|---|
| ed25519 | fixed | Modern default, short keys, fast |
| rsa | -b 3072 default (min 1024) | Widest compatibility; use 3072/4096 |
| ecdsa | 256, 384 or 521 only | NIST curves |
| dsa | exactly 1024 | Legacy, disabled in modern OpenSSH |
| ed25519-sk / ecdsa-sk | - | FIDO hardware key (YubiKey) |
141.3 Managing existing keys
- -l prints the fingerprint; -lv adds randomart that is easy to compare by eye.
- -E md5 gives the old MD5 fingerprint format; -B gives the bubblebabble digest.
- -y reads the private key and prints the public key (useful if the .pub was deleted); a wrong passphrase fails.
- -p changes the passphrase (-P old, -N new); -c changes the comment; -p -m PEM rewrites the key in legacy PEM.
ssh-keygen -l -f ~/.ssh/id_ed25519.pubssh-keygen -lv -f ~/.ssh/id_ed25519.pubssh-keygen -l -E md5 -f ~/.ssh/id_ed25519.pubssh-keygen -B -f ~/.ssh/id_ed25519.pubssh-keygen -y -P lab-key-141 -f ~/.ssh/id_ed25519ssh-keygen -p -P lab-key-141 -N lab-new-141 -f ~/.ssh/id_ed25519ssh-keygen -c -C 'dev laptop n141' -P lab-new-141 -f ~/.ssh/id_ed25519$ ssh-keygen -l -E md5 -f ~/.ssh/id_ed25519.pub256 MD5:3b:61:cd:e4:1e:28:57:b2:7c:c0:a0:b1:8e:e2:77:d7 dev@n141 (ED25519)$ ssh-keygen -B -f ~/.ssh/id_ed25519.pub256 xuvah-lyvyd-cofuv-typun-hezom-kysok-vivym- cehys-lysoc-lyfus-duxux dev@n141 (ED25519)$ ssh-keygen -c -C 'dev laptop n141' -P lab-new-141 -f ~/.ssh/id_ed25519Old comment: dev@n141Comment 'dev laptop n141' applied141.4 Import / export key formats
- -e exports a key for other SSH software (default RFC4716 / SSH2 format); -m PKCS8 or -m PEM give OpenSSL formats.
- -i does the reverse and turns the file back into an OpenSSH line with the same fingerprint.
- In the lab only public keys were exported; never move private keys around without need.
ssh-keygen -e -f ~/.ssh/id_ed25519.pubssh-keygen -e -m PKCS8 -f ~/.ssh/id_rsa_ci.pubssh-keygen -e -m RFC4716 \ -f ~/.ssh/id_rsa_ci.pub > /tmp/ci_ssh2.pubssh-keygen -i -f /tmp/ci_ssh2.pub > /tmp/back.pubssh-keygen -l -f /tmp/back.pub$ head -3 /tmp/ci_ssh2.pub---- BEGIN SSH2 PUBLIC KEY ----Comment: "3072-bit RSA, converted by root@admin from OpenSSH"AAAAB3NzaC1yc2EAAAADAQABAAABgQCekWHAZxkUBOqIK8rVhrO6+90ceuO6MRgIj/7UtS$ ssh-keygen -l -f /tmp/back.pub3072 SHA256:fYh+5d/iEuL+FIcnqFRdK6fbgBd9xD+WNSRNPNkOzis no comment (RSA)141.5 known_hosts and DNS (SSHFP)
- -F host finds a host's line (works with [host]:port entries too); -F host -lv shows its fingerprint.
- -H hashes all host names in the file; -F still finds entries by name or IP afterwards.
- -R host removes an old entry, needed after a server's host key changes; a .old backup is kept.
- -r name prints SSHFP DNS records for a host key; clients use them with VerifyHostKeyDNS. -g gives generic format, -O hashalg picks one hash.
ssh-keygen -F srv -f /tmp/khssh-keygen -F '[srv]:2222' -f /tmp/khssh-keygen -H -f /tmp/khssh-keygen -F 192.168.161.20 -f /tmp/khssh-keygen -R 192.168.161.20 -f /tmp/khssh-keygen -r srv.lab.example -O hashalg=sha256 \ -f /etc/ssh/ssh_host_ed25519_key.pub$ ssh-keygen -R 192.168.161.20 -f /tmp/kh# Host 192.168.161.20 found: line 2/tmp/kh updated.Original contents retained as /tmp/kh.old$ ssh-keygen -r srv.lab.example -O hashalg=sha256 -f /etc/ssh/ssh_host_ed25519_key.pubsrv.lab.example IN SSHFP 4 2 867fc90dadd53e444831a075641d2042 7e73268da6323fc92e83fc1268188031141.6 PKCS#11 and FIDO tokens
- PKCS#11: smart card or HSM (-D library.so lists its public keys). FIDO: security key like a YubiKey; the private key never leaves the token.
- -O resident stores the key handle on the token so a new laptop can download it with -K.
- -O verify-required asks for the PIN on every signature; -O no-touch-required removes the touch.
- The lab had no USB token, so the real errors are shown below.
$ ssh-keygen -KEnter PIN for authenticator:start_helper: helper "/usr/lib/ssh/ssh-sk-helper" unusable: ...Unable to load resident keys: No such file or directory$ ssh-keygen -D /usr/lib/opensc-pkcs11.soprovider /usr/lib/opensc-pkcs11.so is not a PKCS11 librarycannot read public key from pkcs11141.7 SSH certificates: signing and options
- Step 1
1.admin laptop sends id_ed25519.pub to the User CA
- Step 2
2.CA signs it: ssh-keygen -s /ca/user_ca -I dev-laptop -n dev -V +1d
- Step 3
3.Laptop gets id_ed25519-cert.pub (principal, validity, options)
- Step 4
4.At login the laptop sends the certificate to srv
- Step 5
5.srv checks the signature with TrustedUserCAKeys (CA public key) only
- Step 6
6.Login allowed without any authorized_keys entry
The server trusts one CA instead of every individual key
- The CA key is a normal ed25519 key, but its private key is your most important secret.
- -s CA key, -I key ID (shown in the sshd log), -n principals (users or hosts), -V validity, -z serial.
- -O options restrict the cert: no-port-forwarding, force-command, source-address.
- Before signing, the plain key got Permission denied because srv trusts only the CA.
- -h makes a host certificate so clients trust the CA instead of each server's key; -U uses a CA key in ssh-agent.
ssh-keygen -q -t ed25519 -C user-ca@n141 -N '' -f /ca/user_cassh-keygen -k -f /ca/revoked.krl# srv: sshd_config.d/lab.confTrustedUserCAKeys /etc/ssh/user_ca.pubRevokedKeys /etc/ssh/revoked.krlPasswordAuthentication no# sign dev's public keyssh-keygen -s /ca/user_ca \ -I dev-laptop -n dev -V +1d -z 1001 \ -O no-port-forwarding \ -O 'force-command=echo backup-only: $(whoami)@$(hostname)' \ -O source-address=192.168.161.0/24 \ ~/.ssh/id_ed25519.pubssh-keygen -L -f ~/.ssh/id_ed25519-cert.pub$ ssh-keygen -L -f ~/.ssh/id_ed25519-cert.pub Type: ssh-ed25519-cert-v01@openssh.com user certificate Signing CA: ED25519 SHA256:qozqQN+WC2IKXtcEe13c86G... Key ID: "dev-laptop" Serial: 1001 Valid: from 2026-11-09T04:37:00 to 2026-11-10T04:38:17 Principals: dev Critical Options: force-command echo backup-only: $(whoami)@$(hostname) source-address 192.168.161.0/24 Extensions: permit-X11-forwarding permit-agent-forwarding permit-pty permit-user-rcWith the certificate, login worked but the force-command ran instead of hostname (output: backup-only). A -L tunnel was refused because of no-port-forwarding:
[srv]$ grep -iE 'refused|forwarding|prohibit' /tmp/sshd.logrefused local port forward: originator 127.0.0.1 port 45848, target 127.0.0.1 port 22141.8 Certificate options, validity (-V) and RSA signature type
- By default all permit-* extensions are ON: use -O clear and add back only what is needed.
- An unknown critical option makes the certificate rejected.
- -V: + / - are relative times, Z means UTC, always/forever mean no limit; prefer short validity in production.
- With an RSA CA, -t picks the signature: rsa-sha2-512 (default), rsa-sha2-256, or ssh-rsa (SHA-1, avoid).
-O clear all permit-* off-O force-command=cmd run cmd, not the shell-O source-address=192.168.1.0/24 CIDR list-O no-agent-forwarding agent fwd off-O no-port-forwarding -L/-R/-D off-O no-pty terminal off-O no-x11-forwarding X11 off-O permit-port-forwarding allow-V +52w1d now to 52 weeks + 1 day-V -4w:+4w 4 weeks ago to 4 ahead-V 20100101123000Z:20110101123000Z UTC-V always:+30d no start, ends in 30 days-s ca_rsa -t rsa-sha2-512 SHA-512 (default)-s ca_rsa -t ssh-rsa SHA-1 (avoid)A certificate valid only 20260101:20260201 gave the client just Permission denied; the real reason is only in the sshd log:
$ ssh -o BatchMode=yes -i ~/.ssh/id_ed25519 dev@srv hostnamedev@srv: Permission denied (publickey,keyboard-interactive).[srv]$ grep -iE 'expired|invalid|cert' /tmp/sshd.log | tail -2Certificate invalid: expired141.9 Key revocation lists (KRL)
- Step 1
1.dev's laptop is lost, cert serial 1001 still valid +1d
- Step 2
2.Admin writes 'serial: 1001' into a KRL spec file
- Step 3
3.ssh-keygen -k -s user_ca.pub builds revoked.krl
- Step 4
4.revoked.krl is copied to srv (RevokedKeys)
- Step 5
5.Laptop tries to log in with cert 1001
- Step 6
6.srv matches the serial: revoked by file, Permission denied
- -k -f creates a KRL, -u adds to an existing one, -z sets its version.
- -Q tests keys against a KRL (exit code 1 = REVOKED), -Q -l also prints the KRL content.
- Spec lines can revoke by serial (or range), id:, key:, sha1:/sha256: or hash: fingerprint.
echo 'serial: 1001' > /ca/krl_spec.txtssh-keygen -k -f /ca/revoked.krl \ -s /ca/user_ca.pub -z 2 /ca/krl_spec.txtssh-keygen -Q -f /ca/revoked.krl \ ~/.ssh/id_ed25519-cert.pub ~/.ssh/id_rsa_ci.pub# copy the KRL to srv, then log inssh -o BatchMode=yes -i ~/.ssh/id_ed25519 dev@srv hostname$ ssh -o BatchMode=yes -i ~/.ssh/id_ed25519 dev@srv hostnamedev@srv: Permission denied (publickey,keyboard-interactive).[srv]$ grep -i revoked /tmp/sshd.log | tail -2Authentication key ED25519-CERT SHA256:2ot4sKlM9YLgfzyvujVTDvJoNSmmys6hKKDpYGI32eo revoked by file /etc/ssh/revoked.krl141.10 Signing and verifying files (-Y)
- -Y sign -n namespace writes file.sig (Git commit signing uses the same mechanism with namespace git).
- -Y verify checks against an allowed_signers file; find-principals tells who signed; check-novalidate only checks the signature structure.
- allowed_signers line: principal, options (namespaces=, cert-authority, valid-after=, valid-before=), public key.
- Changing one digit in the file gave incorrect signature; a namespace not in namespaces= is refused.
echo 'release v1.4.1 build 7' > release.txtssh-keygen -Y sign -f ~/.ssh/id_ed25519 -n file release.txtssh-keygen -Y verify -f allowed_signers \ -I dev@n141 -n file -s release.txt.sig < release.txtssh-keygen -Y find-principals -f allowed_signers -s release.txt.sigecho 'release v1.4.1 build 8' > hacked.txtssh-keygen -Y verify -f allowed_signers \ -I dev@n141 -n file -s release.txt.sig < hacked.txt$ ssh-keygen -Y verify -f allowed_signers -I dev@n141 -n file \ -s release.txt.sig < hacked.txtSignature verification failed: incorrect signatureCould not verify signature.$ cut -c1-60 allowed_signersdev@n141 namespaces="file" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5A$ ssh-keygen -Y verify -f allowed_signers -I dev@n141 -n git \ -s release.txt.sig < release.txtCould not verify signature.allowed_signers:1: key is not permitted for use in signature namespace "git"141.11 Diffie-Hellman moduli (-M)
- -M generate creates candidate primes for DH group exchange; it is very slow (5 s gave no candidate at 2048 bits).
- -M screen tests candidates as safe primes; 3 groups from /etc/ssh/moduli passed in 8 seconds.
- -O checkpoint saves progress, -O prime-tests raises tests; install by copying to /etc/ssh/moduli.
time timeout 5 ssh-keygen -M generate \ -O bits=2048 -O memory=8 cand.txtgrep ' 2047 ' /etc/ssh/moduli > c2048.txttime timeout 30 ssh-keygen -M screen \ -O lines=3 -f c2048.txt moduli.txt$ time timeout 30 ssh-keygen -M screen -O lines=3 -f c2048.txt moduli.txtMon Nov 9 04:39:39 2026 Found 3 safe primes of 3 candidates in 8 secondsreal 0m 8.07s141.12 Comparison: authorized_keys vs certificates
| Point | authorized_keys | SSH certificates |
|---|---|---|
| Server trusts | Every individual key | One CA public key |
| Adding a user | Copy key to every server | CA signs once |
| Expiry | None | Built in (-V) |
| Restrictions | Per line options | Principals + -O options in cert |
| Removal | Edit every server | One KRL file (RevokedKeys) |
141.13 Common mistakes
- Thinking an SSH cert is an X.509/TLS cert: it is OpenSSH's own simple format, no chain, signed by your own CA.
- Leaving all permit-* extensions on: start with -O clear.
- Long-lived certificates (forever): keep validity short and keep a KRL ready.
- Debugging cert failures on the client only: the reason (expired, revoked) is in the sshd log.
- Forgetting ssh-keygen -R after a legitimate host key change, then disabling host key checking instead.
141.14 Interview tips
- Why certificates over authorized_keys? The server trusts one CA; the cert carries principals and expiry, and one KRL revokes it.
- Developer leaving with a cert valid 3 more days: put the serial into the KRL and update RevokedKeys on all servers.
- Ed25519 is the modern default; RSA needs 3072+ bits; DSA is legacy.
- -l shows a fingerprint, -R cleans known_hosts, -Y signs files and Git commits.