An engineer joins on Monday and you append her key to 200 authorized_keys files. She leaves in eighteen months and you go find those 200 lines again — assuming the inventory is accurate, assuming nobody built a host outside Ansible, assuming you remember the two jump boxes provisioned by hand. That key never expires and there is no way to call it back. OpenSSH has shipped the answer since version 5.4 in 2010: certificates. You run two small certificate authorities, sign host keys and user keys with them, and the fleet’s authorized_keys files stop mattering. Onboarding becomes one signature; offboarding becomes not signing. This guide builds both CAs with ssh-keygen, wires up sshd and the client, and covers principals, validity windows, certificate options, and KRL revocation.
What you’ll accomplish
- Stand up a Host CA and a User CA, and understand why they must be two separate keys.
- Sign host keys so clients stop seeing “The authenticity of host … can’t be established” forever.
- Sign short-lived user certificates carrying principals (who you are) and a validity window (how long you stay).
- Configure
sshdwithTrustedUserCAKeysandHostCertificate, and clients with@cert-authority. - Revoke a certificate before it expires using a key revocation list, and constrain certificates with
force-commandandsource-address.
Prerequisites
- OpenSSH 6.5 or newer on hosts and clients (
ssh -V). Certificates landed in 5.4; Ed25519 keys landed in 6.5. Anything from the last decade is fine.AuthorizedPrincipalsCommandneeds 6.9+, and-O verify-requiredneeds 8.2+. - Use Ed25519 for the CA keys. OpenSSH 8.8 (2021) disabled the
ssh-rsasignature algorithm by default. An RSA CA still works if you sign withssh-keygen -t rsa-sha2-256 -s ..., but you inherit a compatibility problem you did not need. Ed25519 has none of it. - Root on the hosts, and a plan for where the CA private keys live before you generate them.
- Distro differences are real. Debian, Ubuntu, and RHEL 9 ship an
Include /etc/ssh/sshd_config.d/*.confat the top ofsshd_config, so drop-in files work. Older RHEL 8, Amazon Linux 2, and Alpine may not — check withgrep -i ^Include /etc/ssh/sshd_configand edit the main file directly if there is no hit. The service issshon Debian family,sshdon RHEL family.
How it works
A certificate is a public key plus some metadata — an identity, a list of principals, a validity window, options — signed by a CA key. When a certificate is presented, the verifier does not look the key up in a list. It checks one signature against one CA public key it already trusts, then checks that the metadata permits what is being asked. That is the whole idea, and it is the reason the model scales: trust is anchored once per host and once per client, not once per key per host.
The symmetry is what most people miss on the first read. There are two independent directions of trust and they mirror each other exactly:
- The User CA signs engineers’ public keys. Hosts trust that CA through
TrustedUserCAKeysinsshd_config. This replacesauthorized_keys. - The Host CA signs hosts’ public keys. Clients trust that CA through an
@cert-authorityline inknown_hosts. This replaces trust-on-first-use — no more blind “yes” at the fingerprint prompt.
Keep them as two distinct keys. If one key signed both, anyone who could get a host certificate could mint themselves a user certificate, and your bastion would become a machine that issues credentials.
Step-by-step
1. Generate the two CAs. Do this somewhere that is not a production host — ideally an offline machine or a workstation you then wipe.
mkdir -p ~/ssh-ca && cd ~/ssh-ca
ssh-keygen -t ed25519 -f host_ca -C "host CA - example.com"
ssh-keygen -t ed25519 -f user_ca -C "user CA - example.com"
Generating public/private ed25519 key pair.
Your identification has been saved in host_ca
Your public key has been saved in host_ca.pub
The key fingerprint is:
SHA256:8Qf1x2n0dEo9sQKpTn6bWj3LyVzR4cUaHkGm7ZtPxYs host CA - example.com
Say the quiet part out loud: the CA private keys are the entire security model. Anyone holding user_ca can mint a certificate for any principal on every host that trusts it — a skeleton key that leaves no trace in any authorized_keys file. Passphrase-protect them at minimum. Better: a hardware token (ssh-keygen -t ed25519-sk), a Vault SSH secrets engine, or an offline machine that only comes out for signing. That custody burden is the honest cost of the model. If you cannot answer “where does user_ca live and who can touch it,” stop here and solve that first.
2. Install the User CA public key on hosts. Only the .pub file leaves your CA machine. Never copy the private keys to a host.
scp user_ca.pub root@web-01.example.com:/etc/ssh/user_ca.pub
ssh root@web-01.example.com 'chmod 644 /etc/ssh/user_ca.pub'
3. Tell sshd to trust the User CA.
# on web-01, /etc/ssh/sshd_config.d/10-ca.conf
TrustedUserCAKeys /etc/ssh/user_ca.pub
Validate and reload — sshd -t catches typos before you lock yourself out:
sudo sshd -t && sudo systemctl reload ssh
sudo sshd -T | grep -i trustedusercakeys
trustedusercakeys /etc/ssh/user_ca.pub
sshd -T prints the effective config. If your line does not appear here, sshd never read it, and nothing below will work.
4. Sign the host’s key with the Host CA. Copy the host’s public key back to the CA machine, sign it there, and send only the certificate back.
scp root@web-01.example.com:/etc/ssh/ssh_host_ed25519_key.pub ./web-01.pub
ssh-keygen -s host_ca -I web-01.example.com -h -n web-01.example.com,web-01,10.0.5.11 -V +52w -z 11 ./web-01.pub
Signed host key ./web-01-cert.pub: id "web-01.example.com" serial 11 for web-01.example.com,web-01,10.0.5.11 valid from 2026-07-16T09:14:00 to 2027-07-15T09:15:32
The flags: -s is the signing CA key, -I is the key ID that shows up in logs, -h makes it a host certificate rather than a user one, -n lists the principals — for a host these are the names clients will type, so include every hostname and IP anyone actually uses. -V +52w sets the validity window and -z 11 sets a serial number you can revoke by later.
5. Point sshd at its certificate.
scp ./web-01-cert.pub root@web-01.example.com:/etc/ssh/ssh_host_ed25519_key-cert.pub
# on web-01, appended to /etc/ssh/sshd_config.d/10-ca.conf
HostCertificate /etc/ssh/ssh_host_ed25519_key-cert.pub
The certificate’s public key must match a private key already named by a HostKey line, or sshd refuses to start. Reload and confirm:
sudo sshd -t && sudo systemctl reload ssh
6. Pin the Host CA on clients. One line in known_hosts replaces every host fingerprint you would otherwise accumulate. The pattern after @cert-authority limits which hostnames this CA is allowed to vouch for — do not use a bare *.
printf '@cert-authority *.example.com,10.0.5.* %s' "$(cat ~/ssh-ca/host_ca.pub)" | sudo tee -a /etc/ssh/ssh_known_hosts
Put it in /etc/ssh/ssh_known_hosts and ship it with your config management — then new laptops trust the fleet on day one, and you can delete the pile of per-host entries in every user’s ~/.ssh/known_hosts.
7. Sign a user certificate. This is the onboarding step, and it is the whole point: one command, no hosts touched.
ssh-keygen -s user_ca -I alice@example.com -n alice,ops -V +8h -z 1001 ./alice_id_ed25519.pub
Signed user key ./alice_id_ed25519-cert.pub: id "alice@example.com" serial 1001 for alice,ops valid from 2026-07-16T09:31:00 to 2026-07-16T17:31:32
Here -n alice,ops are the principals — the local usernames this certificate may log in as. -V +8h means it dies at the end of the workday. Short windows are the mechanism that makes offboarding trivial: revocation becomes the exception, expiry is the default. Give Alice back only alice_id_ed25519-cert.pub, which she saves next to her private key.
8. Map principals to accounts explicitly (recommended). By default a certificate with principal ops can log in as the Unix user ops. Coupling principal names to account names gets awkward fast. AuthorizedPrincipalsFile decouples them: per local account, list which principals may become it.
# on web-01, appended to /etc/ssh/sshd_config.d/10-ca.conf
AuthorizedPrincipalsFile /etc/ssh/auth_principals/%u
sudo mkdir -p /etc/ssh/auth_principals
printf 'ops\nsre\n' | sudo tee /etc/ssh/auth_principals/ubuntu
sudo sshd -t && sudo systemctl reload ssh
Now any certificate carrying principal ops or sre can log in as ubuntu on this host. Alice’s role changes are a property of her certificate, not of 200 files.
9. Constrain certificates where it matters. -O adds certificate options that the host enforces, regardless of what the client asks for.
ssh-keygen -s user_ca -I deploy-bot -n deploy -V +15m \
-O source-address=203.0.113.0/24 \
-O clear -O permit-pty \
-O force-command=/usr/local/bin/deploy-runner \
./deploy_bot.pub
-O source-address refuses the certificate from anywhere else. -O clear strips the default permissions (X11, agent, port forwarding, pty, user-rc) so you can add back only permit-pty. -O force-command runs that command and ignores whatever the client requested. This is how a CI runner gets a credential that cannot become an interactive shell — enforced by the certificate itself, not by an authorized_keys prefix somebody will forget to copy.
Validate it works
Read the certificate before you trust it. ssh-keygen -L is the tool; if the metadata is wrong, nothing else will make sense.
ssh-keygen -L -f ./alice_id_ed25519-cert.pub
./alice_id_ed25519-cert.pub:
Type: ssh-ed25519-cert-v01@openssh.com user certificate
Public key: ED25519-CERT SHA256:1yQpVn8sK0dLxTgWm4cRb7ZoJhEuP2aFqXtNvY6iBrM
Signing CA: ED25519 SHA256:Ke9zT2wQd0nVxRlYm6bJ8sHcPfA4uGtXqZoE1iNrV5g (using ssh-ed25519)
Key ID: "alice@example.com"
Serial: 1001
Valid: from 2026-07-16T09:31:00 to 2026-07-16T17:31:32
Principals:
alice
ops
Critical Options: (none)
Extensions:
permit-X11-forwarding
permit-agent-forwarding
permit-port-forwarding
permit-pty
permit-user-rc
Check the Signing CA fingerprint against ssh-keygen -l -f user_ca.pub — that is how you confirm it was signed by the CA you think it was.
Connect and watch the handshake. ssh -v shows both halves of the trust model in one run.
ssh -v -i ~/.ssh/alice_id_ed25519 ubuntu@web-01.example.com true
debug1: Server host certificate: ssh-ed25519-cert-v01@openssh.com SHA256:0pQ... , serial 11 ID "web-01.example.com" CA ssh-ed25519 SHA256:Ke9... valid from 2026-07-16T09:14:00 to 2027-07-15T09:15:32
debug1: Host 'web-01.example.com' is known and matches the ED25519-CERT host certificate.
debug1: Found CA key in /etc/ssh/ssh_known_hosts:1
debug1: Offering public key: /home/alice/.ssh/alice_id_ed25519 ED25519-CERT SHA256:1yQ... explicit
debug1: Server accepts key: /home/alice/.ssh/alice_id_ed25519 ED25519-CERT SHA256:1yQ... explicit
Four things to confirm: the server presented a host certificate; the client matched it via the CA key in known_hosts (no fingerprint prompt, first connection ever); the client offered ED25519-CERT rather than plain ED25519; the server accepted it. If any line reads ED25519 without -CERT, the client is not using the certificate at all — see troubleshooting below.
Prove that expiry is real. Sign a certificate whose window is already in the past and watch the host refuse it.
ssh-keygen -s user_ca -I expired-test -n ops -V -2h:-1h ./alice_id_ed25519.pub
ssh -i ~/.ssh/alice_id_ed25519 ubuntu@web-01.example.com true
ubuntu@web-01.example.com: Permission denied (publickey).
The host’s log names the reason exactly:
sudo journalctl -u ssh -n 5 --no-pager
sshd[3312]: error: Certificate invalid: expired
Revoke before expiry with a KRL. Sometimes eight hours is too long — a laptop goes missing at 10 a.m.
ssh-keygen -k -f ~/ssh-ca/revoked.krl ./alice_id_ed25519-cert.pub
ssh-keygen -Q -f ~/ssh-ca/revoked.krl ./alice_id_ed25519-cert.pub
Revoking from ./alice_id_ed25519-cert.pub
./alice_id_ed25519-cert.pub (alice@laptop): REVOKED
The name in parentheses is the comment on Alice’s key, so yours will differ. -Q also exits non-zero when the certificate is revoked, which is what makes it usable in a check script; an unrevoked certificate prints ok and exits 0.
Add -u to update an existing KRL instead of replacing it. To revoke by serial or key ID without holding the certificate file, write a spec and name the CA with -s:
printf 'serial: 1001\nserial: 1010-1020\nid: bob@example.com\n' > revoke.spec
ssh-keygen -k -f ~/ssh-ca/revoked.krl -s ~/ssh-ca/user_ca.pub revoke.spec
Ship the KRL to every host and point sshd at it:
# on web-01, appended to /etc/ssh/sshd_config.d/10-ca.conf
RevokedKeys /etc/ssh/revoked.krl
This is the one part of the model that still touches all 200 hosts — which is exactly why short validity windows matter. They keep the KRL a rare tool, not a daily one.
Troubleshooting
“Certificate invalid: name is not a listed principal”. The certificate’s principals do not include the account you are logging in as, or the principal is not listed in that account’s AuthorizedPrincipalsFile. Run ssh-keygen -L -f cert.pub, read the Principals block, and compare it to what you typed after the @. The same message appears client-side when a host certificate does not list the name you typed — connecting to 10.0.5.11 when the certificate only lists web-01.example.com drops you back to a fingerprint prompt. Re-sign with every name people actually use in -n.
The client offers ED25519 instead of ED25519-CERT. The certificate must sit beside the private key with the exact name <keyname>-cert.pub. If the key is ~/.ssh/alice_id_ed25519, the certificate must be ~/.ssh/alice_id_ed25519-cert.pub. Rename it, or name it explicitly:
# ~/.ssh/config
Host *.example.com
IdentityFile ~/.ssh/alice_id_ed25519
CertificateFile ~/.ssh/alice-work-cert.pub
This is the most common “it just does not work,” and it produces no error — the client silently falls back to the plain key.
sshd -T does not show your TrustedUserCAKeys line. Either the drop-in directory is not included (grep -i ^Include /etc/ssh/sshd_config) or an earlier directive won — sshd takes the first value for most keywords, so a TrustedUserCAKeys further up the file beats yours. Also check permissions: the file must be readable by root and neither it nor its parents may be group- or world-writable. The log is explicit:
sshd[2201]: error: Unable to open file /etc/ssh/user_ca.pub: Permission denied
“Certificate invalid: not yet valid” on a fresh certificate. Clock skew. A certificate signed at 09:31 on a CA machine that is 90 seconds ahead is not valid yet on the host. Fix NTP (timedatectl status, look for System clock synchronized: yes), and give signing a small backdate: -V -5m:+8h.
sshd will not start after adding HostCertificate. The certificate’s public key must match a private key already declared by HostKey. If you signed ssh_host_ed25519_key.pub but the config only lists an RSA HostKey, sshd rejects it at startup. Compare ssh-keygen -l -f /etc/ssh/ssh_host_ed25519_key.pub with the Public key line from ssh-keygen -L -f /etc/ssh/ssh_host_ed25519_key-cert.pub. Run sshd -t before every reload, and keep a second session open.
When this is the wrong approach
Certificates are not free, and some fleets are better off without them.
- Small, stable fleets. With eight hosts and three engineers,
authorized_keysmanaged by Ansible is genuinely fine — the maintenance you save does not cover the CA custody you take on. The pain starts around “more hosts than I can name from memory.” - No custody story. A CA private key sitting in a Git repository or on a shared jump box is a worse outcome than the sprawl you replaced — it is a skeleton key with no audit trail. If you cannot keep it offline, in an HSM, or behind Vault with an approval flow, do not create it.
- You need per-session audit and recording. Certificates authenticate; they do not record. If the requirement is “show me every command run on the database host last Tuesday,” you want Teleport, or session recording on your bastion, or both.
- A managed control plane fits better. AWS SSM Session Manager, Tailscale SSH, and Teleport all remove key distribution without you running a CA — they run one for you, or bypass keys entirely. If you are already deep in one cloud, its native answer is usually less work.
- Legacy clients and appliances. Network gear and vendor appliances often speak only classic public-key auth. You will run both models side by side for a while; plan for it rather than being surprised by it.
The authoritative reference is the CERTIFICATES section of the official ssh-keygen documentation, with the server-side directives in sshd_config(5). Certificates pair naturally with the access topology: see SSH ProxyJump and bastions for reaching private hosts through a jump host, and setting up a bastion host correctly for hardening the entry point — a bastion that trusts a User CA and presents a Host CA certificate is the combination worth aiming at.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.