Security and hardening · Advanced · 18 min
Use SSH certificates instead of copying keys to every server
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · OpenSSH 10.2p1
You will run a small SSH certificate authority with ssh-keygen, log in to an Ubuntu 26.04 server with an eight-hour user certificate, give the server a host certificate so clients stop asking about fingerprints, and revoke a certificate the server then refuses.
On this page
With plain SSH keys, every server keeps a list of every person's public key in authorized_keys, and every laptop keeps a list of every server's fingerprint in known_hosts. When someone joins you copy their key to each server. When someone leaves you hope you removed it from all of them. When you rebuild a server, everyone sees a scary "host key changed" warning and learns to type yes without reading.
SSH certificates replace both lists with one trusted key each way. A certificate authority (CA), which is only a key pair you guard carefully, signs a person's public key with an expiry date and a list of roles. Servers trust the CA instead of individual keys. The same idea works in reverse for servers, so laptops trust a host CA instead of collecting fingerprints.
You will build this with nothing but ssh-keygen and sshd, both part of OpenSSH 10.2 on Ubuntu 26.04. There are three roles, and every command block says where it runs:
- CA machine: holds the CA private keys. Practise on your laptop first; for real use, move the CA folder to a machine that stays off the network, or an encrypted USB drive.
- Laptop: the person logging in, here called Amara.
- Server:
web1.example.com, accountdeploy. Replace the name with your server's.
All the CA and laptop files live in ~/ssh-ca-demo/, a throwaway folder. Public files (.pub, -cert.pub, the revocation list) can travel by any channel. Private keys never leave the machine they were made on.
Step 1 · Keep your current way in open
You are about to change how the server checks logins. Certificates are added alongside your existing key, not instead of it, so nothing in this tutorial removes your authorized_keys access. A typo in the SSH daemon's configuration can still stop it accepting any login, so work like this:
- Open one SSH session to the server with your existing key and leave it open until the end. Open sessions survive a reload, so you can undo a mistake from there.
- Do every login test from a second terminal.
- Run
sudo sshd -tbefore every reload. It refuses a broken file without touching the running daemon.
On your laptop, open the first session:
ssh deploy@web1.example.comCheck it worked
You get a shell as deploy without an account password. If this is your first connection, SSH asks you to confirm the server's fingerprint. Remember that prompt: by the end of the tutorial it is gone. Keep this terminal open.
Step 2 · Create the user CA on the CA machine
On the CA machine, make a private folder and a CA key pair. Choose a strong passphrase when asked: anyone with this file and its passphrase can sign their way into every server that trusts it.
install -d -m 700 ~/ssh-ca-demossh-keygen -t ed25519 -f ~/ssh-ca-demo/user_ca -C "example user CA"This is an ordinary Ed25519 key. What makes it a CA is how you use it: it only ever signs other keys, and it never logs in anywhere.
Check it worked
ls -l ~/ssh-ca-demo
ssh-keygen -l -f ~/ssh-ca-demo/user_ca.pubYou see user_ca with permissions -rw------- and user_ca.pub readable by all, then one line with 256 SHA256:..., the comment example user CA and (ED25519). Write that fingerprint down somewhere separate. Later, when you copy user_ca.pub to a server, you can compare it to make sure the right file arrived.
Step 3 · Tell the server to trust the CA
The server needs three things: the CA's public key, a rule saying which certificate roles may log in to which account, and a setting that switches it on.
From your laptop (with user_ca.pub copied to it from the CA machine), send the public key to the server:
scp ~/ssh-ca-demo/user_ca.pub deploy@web1.example.com:/tmp/user_ca.pubOn the server, install it where only root can change it:
sudo install -m 644 /tmp/user_ca.pub /etc/ssh/user_ca.pubNext, decide who may log in as deploy. A certificate lists principals, which are names you choose. Here, anyone whose certificate includes the principal ops may log in as deploy. One file per account, named after the account:
sudo mkdir -p /etc/ssh/auth_principals
echo 'ops' | sudo tee /etc/ssh/auth_principals/deploy > /dev/nullTo let a second role in, add another line, such as deploy-bot. Then write a drop-in file that switches certificates on. The %u is replaced by the account name at login, so this one line covers every account:
sudo tee /etc/ssh/sshd_config.d/10-user-ca.conf > /dev/null <<'CONF'
# Accept user certificates signed by this CA.
TrustedUserCAKeys /etc/ssh/user_ca.pub
# A certificate must carry a principal listed in this account's file.
AuthorizedPrincipalsFile /etc/ssh/auth_principals/%u
CONF
sudo sshd -tsshd -t printing nothing means the file is valid. Reload the daemon:
sudo systemctl reload sshCheck it worked
sudo sshd -T | grep -E '^(trustedusercakeys|authorizedprincipalsfile) 'Two lines come back, showing /etc/ssh/user_ca.pub and /etc/ssh/auth_principals/%u. In your second terminal, ssh deploy@web1.example.com still logs in with your old key, because authorized_keys keeps working alongside certificates.
Step 4 · Sign a short-lived certificate
On the laptop, Amara makes her own key pair. Her private key stays on her laptop; only amara.pub goes to the CA machine.
ssh-keygen -t ed25519 -f ~/ssh-ca-demo/amara -C "amara laptop"On the CA machine, sign amara.pub. It asks for the CA passphrase:
ssh-keygen -s ~/ssh-ca-demo/user_ca -I amara-2026-11-26 -n amara,ops -V +8h -z 1001 -O clear -O permit-pty ~/ssh-ca-demo/amara.pubEach option ends up inside the certificate:
-s ~/ssh-ca-demo/user_casigns with the CA's private key.-I amara-2026-11-26is the key ID. It appears in the server's log at every login, so make it say who and when.-n amara,opslists the principals.opsmatches the server's file;amarais there for servers that check personal names.-V +8hmakes it valid from now for eight hours. Nobody has to remember to remove it.-z 1001sets a serial number, which you use to revoke it. Keep a simple log of the serials you issue, and never reuse one.-O clear -O permit-ptydrops every default permission (port forwarding, agent forwarding, X11) and adds back an interactive terminal only. Add-O permit-port-forwardingback if someone needs it.
If your team often works over unreliable mobile data or across time zones, pick a window that covers a working day plus some slack, such as +12h, rather than the 8 hours used here. A certificate that expires mid-task is the main reason people ask for longer ones.
Check it worked
ssh-keygen -L -f ~/ssh-ca-demo/amara-cert.pubThe output says user certificate, shows Key ID: "amara-2026-11-26", Serial: 1001, a Valid: line with today's date and a time eight hours ahead, amara and ops under Principals, and only permit-pty under Extensions. Copy amara-cert.pub back to Amara's laptop, next to her private key.
Step 5 · Log in with the certificate
On the laptop, point ssh at the private key. It looks for a file with the same name plus -cert.pub and sends the certificate automatically. -o IdentitiesOnly=yes matters: without it ssh also offers your other keys (from ~/.ssh or an agent such as the macOS keychain), so the login could quietly succeed with your old key instead of the certificate:
ssh -i ~/ssh-ca-demo/amara -o IdentitiesOnly=yes deploy@web1.example.comAmara's public key is not in authorized_keys on the server, so this login can only succeed through the certificate. For everyday use, save it in ~/.ssh/config:
Host web1
HostName web1.example.com
User deploy
IdentityFile ~/ssh-ca-demo/amara
IdentitiesOnly yesCheck it worked
You get a shell as deploy. On the server, the log names the certificate:
sudo journalctl -u ssh -n 20 --no-pager | grep CERTYou see something like Accepted publickey for deploy from 198.51.100.7 port 51234 ssh2: ED25519-CERT SHA256:... ID amara-2026-11-26 (serial 1001) CA ED25519 SHA256:.... The key ID and serial tell you exactly whose certificate was used, which a plain key never does. After eight hours the same command (with -o IdentitiesOnly=yes, or ssh web1) ends in Permission denied (publickey), and Amara asks the CA for a fresh certificate.
Step 6 · Give the server a host certificate
Now the other direction. On the CA machine, make a second CA just for servers. Keeping it separate means a leak of one CA does not compromise the other.
ssh-keygen -t ed25519 -f ~/ssh-ca-demo/host_ca -C "example host CA"Fetch the server's public host key on your laptop (it is public, so any copy will do), then carry web1_host.pub to the CA machine:
scp deploy@web1.example.com:/etc/ssh/ssh_host_ed25519_key.pub ~/ssh-ca-demo/web1_host.pubOn the CA machine, sign it with -h, which makes a host certificate. The principals are the names clients use to reach the server:
ssh-keygen -s ~/ssh-ca-demo/host_ca -I web1-host -h -n web1.example.com -V +52w ~/ssh-ca-demo/web1_host.pubThis writes ~/ssh-ca-demo/web1_host-cert.pub, valid for a year. Put a renewal date in your calendar. From your laptop (with web1_host-cert.pub copied to it from the CA machine), send it to the server:
scp ~/ssh-ca-demo/web1_host-cert.pub deploy@web1.example.com:/tmp/ssh_host_ed25519_key-cert.pubOn the server, install it beside the host key and tell sshd to offer it:
sudo install -m 644 /tmp/ssh_host_ed25519_key-cert.pub /etc/ssh/ssh_host_ed25519_key-cert.pub
echo 'HostCertificate /etc/ssh/ssh_host_ed25519_key-cert.pub' | sudo tee /etc/ssh/sshd_config.d/10-host-cert.conf > /dev/null
sudo sshd -tsudo systemctl reload sshOn the laptop, trust the host CA for every server under your domain, and remove the fingerprint you accepted by hand in the first step so the certificate is what gets checked:
echo "@cert-authority *.example.com $(cat ~/ssh-ca-demo/host_ca.pub)" >> ~/.ssh/known_hosts
ssh-keygen -R web1.example.comCheck it worked
Connect again from the laptop:
ssh deploy@web1.example.comThere is no fingerprint prompt, even though you just deleted the saved fingerprint. When you add web2.example.com later, sign its host key the same way and no laptop needs to change. If you see the prompt, check that the name you type matches a principal in the host certificate (ssh-keygen -L -f ~/ssh-ca-demo/web1_host-cert.pub).
Step 7 · Revoke a certificate before it expires
Short validity handles most departures on its own, but a stolen laptop cannot wait eight hours. OpenSSH revokes certificates with a key revocation list (KRL), a compact file the server checks at each login.
Read this before editing the configuration: if RevokedKeys names a file the daemon cannot read, it refuses public key logins for every user, including your own authorized_keys key. Create and install the file first, then switch the setting on.
On the CA machine, create an empty list:
ssh-keygen -k -f ~/ssh-ca-demo/revoked_keysFrom the laptop, copy it to the server; then on the server, install it and add the setting. This rewrites the whole drop-in with the new line included:
scp ~/ssh-ca-demo/revoked_keys deploy@web1.example.com:/tmp/revoked_keyssudo install -m 644 /tmp/revoked_keys /etc/ssh/revoked_keys
sudo tee /etc/ssh/sshd_config.d/10-user-ca.conf > /dev/null <<'CONF'
# Accept user certificates signed by this CA.
TrustedUserCAKeys /etc/ssh/user_ca.pub
# A certificate must carry a principal listed in this account's file.
AuthorizedPrincipalsFile /etc/ssh/auth_principals/%u
# Refuse anything on this list.
RevokedKeys /etc/ssh/revoked_keys
CONF
sudo sshd -tsudo systemctl reload sshIn the second terminal, confirm Amara's certificate still logs in with the empty list in place. Now revoke serial 1001. On the CA machine, write a small spec file and update the list with it. -s names the CA that issued the serial, and -u adds to the existing list instead of replacing it:
echo 'serial: 1001' > ~/ssh-ca-demo/revoke-1001.txt
ssh-keygen -k -u -s ~/ssh-ca-demo/user_ca.pub -f ~/ssh-ca-demo/revoked_keys ~/ssh-ca-demo/revoke-1001.txtRevoking a serial blocks that one certificate. If the laptop itself was stolen, also block the key so it cannot be re-signed by mistake: add a line key: ` followed by the contents of `amara.pub to the spec file. Ship the updated list:
scp ~/ssh-ca-demo/revoked_keys deploy@web1.example.com:/tmp/revoked_keyssudo install -m 644 /tmp/revoked_keys /etc/ssh/revoked_keysThe daemon reads the list at each login, so no reload is needed. With many servers, push this file from the same place you push other configuration, and treat a server with a stale list as a finding.
Check it worked
On the CA machine, ask the list about the certificate:
ssh-keygen -Q -f ~/ssh-ca-demo/revoked_keys ~/ssh-ca-demo/amara-cert.pubIt reports the certificate as REVOKED. Then try to log in with it from the laptop, again with -o IdentitiesOnly=yes so ssh offers only the certificate and not your other keys:
ssh -i ~/ssh-ca-demo/amara -o IdentitiesOnly=yes deploy@web1.example.comThe server answers Permission denied (publickey). while your original key, in the other terminal, still works.
Step 8 · Remove the practice setup
If this was a rehearsal, take it all out. Read the commands first: they delete the CA keys, so only run them on the practice folder and the practice server files, never on a CA you use for real.
On the server:
sudo rm /etc/ssh/sshd_config.d/10-user-ca.conf /etc/ssh/sshd_config.d/10-host-cert.conf
sudo rm /etc/ssh/user_ca.pub /etc/ssh/revoked_keys /etc/ssh/ssh_host_ed25519_key-cert.pub
sudo rm -r /etc/ssh/auth_principals
rm -f /tmp/user_ca.pub /tmp/revoked_keys /tmp/ssh_host_ed25519_key-cert.pub
sudo sshd -tsudo systemctl reload sshOn the laptop and the CA machine:
rm -r ~/ssh-ca-demoThen open ~/.ssh/known_hosts in an editor and delete the line that starts with @cert-authority *.example.com, and remove the Host web1 block from ~/.ssh/config if you added it.
Check it worked
sudo sshd -T | grep -cE '^(trustedusercakeys|revokedkeys) /etc/ssh' || true
ls ~/ssh-ca-demo 2>/dev/null || echo "practice folder gone"The first line prints 0, and the second prints practice folder gone. Your next ssh deploy@web1.example.com asks about the fingerprint again, and your original key still logs you in.
Next steps
- If the server still accepts passwords, close that first:
/tutorials/harden-ssh-on-ubuntu-26-04. - The basics of keys, which certificates build on:
/tutorials/log-in-to-a-server-with-ssh-keys. - A CA key that leaks is a secret to rotate, with an overlap while servers move to the new CA:
/tutorials/rotate-a-leaked-secret-without-downtime. - Once everyone on the team logs in with certificates, delete their old lines from each server's
authorized_keys, keeping one break-glass key for yourself.
Related tutorials
- Write custom Fail2ban jails and filters for your own appsYou will have Fail2ban 1.1 banning repeated failed sign-ins to your own app and to an nginx basic-auth area, with filters you tested against sample logs, your own addresses allow-listed, and longer bans for repeat offenders.
- Validate file uploads before you store or serve themYou will take a FastAPI image upload route that trusts everything the browser sends, prove its flaws with pytest, and rebuild it with size limits, type checks from the file's own bytes, Pillow re-encoding, random storage names and safe download headers.
- Understand CodeQL and GitHub code scanningYou will install the CodeQL 2.27.6 bundle, build CodeQL databases for a small Express app and a Flask app, run the security-extended queries, trace a finding from user input to a dangerous call in the SARIF output, fix it and prove the alert is gone, and know what GitHub code scanning adds on top.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials