Strovent SystemsSign in
Reference

SSH host certificates and @cert-authority in known_hosts

How a @cert-authority line in known_hosts lets SSH trust every host certificate signed by one CA, how to sign a host key, and why a client still prompts.

Normally an SSH client trusts each host on first use: it shows a fingerprint, you accept it, and the key is saved in known_hosts. With host certificates, a certificate authority (CA) signs each host's key, and the client trusts the CA instead. Any host with a valid certificate from that CA is accepted without a prompt.

These are OpenSSH certificates, a simpler format than the X.509 certificates used by TLS. User certificates, which prove you to a host, are covered in OpenSSH certificates.

On the client: a @cert-authority line

Add one line to ~/.ssh/known_hosts (or the system-wide /etc/ssh/ssh_known_hosts). It starts with the marker @cert-authority, then the host name patterns it applies to, then the CA's public key:

@cert-authority *.example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...

The patterns are a comma-separated list, and * and ? work as wildcards. They are matched against the host name you typed, or the HostKeyAlias if one is set. Hashed host names cannot use wildcards, so write this line in plain text.

When the server presents a certificate, OpenSSH accepts it if a matching line holds the key of the CA that signed it. Only lines with the @cert-authority marker are trusted as CAs.

On the host: sign the host key

The CA operator signs each host's public key with -h, which makes a host certificate rather than a user certificate:

ssh-keygen -s host_ca -I host1 -h -n host1.example.com -V +52w /etc/ssh/ssh_host_ed25519_key.pub
  • -s host_ca is the CA's private key.
  • -I host1 is the key identity, written to logs.
  • -n lists the principals. For a host certificate these are host names, and they may contain wildcards. List every name clients use for the host.
  • -V +52w makes the certificate valid for 52 weeks from now.

This writes /etc/ssh/ssh_host_ed25519_key-cert.pub. Point sshd at it in sshd_config:

HostCertificate /etc/ssh/ssh_host_ed25519_key-cert.pub

The certificate's public key must match a private key already named by HostKey. By default sshd loads no certificates. Restart or reload sshd after the change. ssh-keygen -L -f /etc/ssh/ssh_host_ed25519_key-cert.pub prints the type, principals and validity.

Why you still get the first-connection prompt

When no CA matches, OpenSSH drops the certificate and checks the host's plain key the normal way. With ssh -v you see:

debug1: No matching CA found. Retry with plain key

The usual reasons:

  • No line matches the host name. The pattern on the @cert-authority line does not cover the name you typed, for example *.example.com and a connection by IP address or short name.
  • The host does not send a certificate. HostCertificate is missing from sshd_config, or sshd was not reloaded.
  • The name is not a principal. OpenSSH prints Certificate invalid: name is not a listed principal. Connect with a name that was listed in -n when the key was signed, or sign it again with that name.
  • The certificate has expired or is not valid yet. OpenSSH prints Certificate invalid: expired or Certificate invalid: not yet valid. Check the dates with ssh-keygen -L and the clocks on both machines.
  • The client does not support host certificates. Not every SSH client or library checks @cert-authority. Such a client falls back to asking about the plain host key.

Revoking a key

Put @revoked at the start of a known_hosts line to mark a key that must never be accepted, either as a host key or as a CA. OpenSSH prints a warning when it meets one.

Sources

Spotted a mistake? Email [email protected].