basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    SSH certificates: access with an expiration date

    A regular key in authorized_keys works forever until someone removes it by hand from every server. An SSH certificate is the same public key signed by your own certificate authority (CA) key for a set window: once time runs out, the server rejects it on its own.

    How it works

    CA key (ssh_user_ca) ── signs for 8 h ──→ id_ed25519-cert.pub (on client)
           │
           └─ ssh_user_ca.pub ──→ on server: TrustedUserCAKeys /etc/ssh/ssh_user_ca.pub

    The server no longer needs a list of every person’s key in authorized_keys: it trusts the public key of your certificate authority (ssh_user_ca.pub). When a client connects with id_ed25519 + id_ed25519-cert.pub, sshd checks the CA signature, the username inside the certificate, and the validity window. Once that window closes, the server refuses login on its own without any changes on the server.

    Issue a certificate and turn on verification

    Create a separate key pair for the certificate authority (CA) on your machine:

    ssh-keygen -t ed25519 -f ~/.ssh/ssh_user_ca -C "ssh-user-ca"

    Copy only the public half ssh_user_ca.pub to /etc/ssh/ssh_user_ca.pub on the server (mode 644, owner root) and add this line to /etc/ssh/sshd_config.d/00-hardening.conf (sshd_config):

    TrustedUserCAKeys /etc/ssh/ssh_user_ca.pub

    Test the config with sudo sshd -t and reload ssh while keeping your current session open (Keep the old session open).

    Now sign the user’s public key for 8 hours:

    ssh-keygen -s ~/.ssh/ssh_user_ca \
      -I "contractor-2026-11" \
      -n example-user \
      -V -5m:+8h \
      ~/.ssh/id_ed25519.pub

    This creates id_ed25519-cert.pub next to id_ed25519.pub. The OpenSSH client picks it up automatically when its filename matches the private key.

    • -I — certificate ID recorded in /var/log/auth.log on the server on every login.
    • -n — allowed server usernames (comma-separated, e.g. example-user). If the login name doesn’t match, sshd rejects the certificate.
    • -V -5m:+8h — validity window: 5 minutes in the past (so minor clock skew doesn’t break login) and 8 hours forward. You can also pass explicit dates (-V 20261101:20261201).

    When to use it and when to skip it

    For one or two personal servers, regular keys in authorized_keys are enough (SSH keys): removing one line on two machines takes less work than maintaining a CA key. Certificates pay off in two cases:

    • you manage several servers and distributing or removing keys on each one by hand gets tedious;
    • you need temporary access (for a contractor, a colleague, or a debugging session) that closes on its own.

    The main risk in this setup is the private ssh_user_ca key. Anyone who holds it can sign a certificate for any server that has TrustedUserCAKeys. Never copy this key to servers: keep it offline under a long passphrase or on a hardware security key (SSH key on a security key).

    If it doesn’t work

    Certificate invalid: expired or not yet valid

    Cause
    The certificate expired, or the clock on the server and the signing machine drifted apart.
    Fix
    Sign the key again with a backward buffer (-V -5m:+8h) and check time sync on the server (timedatectl, Swap, journal and time).

    Permission denied (publickey) even though the certificate is valid and the CA is configured

    Cause
    The server username isn’t listed in the certificate’s -n (principals), or the client couldn’t find the *-cert.pub file next to the private key.
    Fix
    Check Principals: with ssh-keygen -L -f ~/.ssh/id_ed25519-cert.pub and make sure the certificate file sits next to id_ed25519 (or point to it with CertificateFile).

    You need to revoke access before the certificate expires

    Cause
    The key isn’t in authorized_keys, so there’s nothing to delete there.
    Fix
    Add the key to a revocation list (ssh-keygen -k -f /etc/ssh/revoked_keys id_ed25519.pub) and set RevokedKeys /etc/ssh/revoked_keys in sshd_config.

    How to check

    Inspect the issued certificate locally:

    ssh-keygen -L -f ~/.ssh/id_ed25519-cert.pub

    Check Valid: (time window), Principals: (username), and Signing CA: in the output. Then log in to the server with a key that isn’t in authorized_keys:

    ssh -i ~/.ssh/id_ed25519 [email protected]

    journalctl -u ssh -n 5 on the server will log a certificate login with the -I label and the CA fingerprint.