SSH Key Rotation Best Practices

Published
Length
3 min read

Luis Felipe Dias

All posts

To follow ssh key rotation best practices, replace static public keys with short-lived OpenSSH certificates. You sign each user key with a central authority key and set an expiration time. Many teams copy static public keys to every server by hand. Some teams use scripts to push key files across machines. Over time, old keys pile up on production hosts. When an engineer leaves the team, their key often stays behind. Broad permissions let anyone with a valid key run any command on the box. Cor

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

To follow ssh key rotation best practices, replace static public keys with short-lived OpenSSH certificates. You sign each user key with a central authority key and set an expiration time.

Many teams copy static public keys to every server by hand. Some teams use scripts to push key files across machines. Over time, old keys pile up on production hosts. When an engineer leaves the team, their key often stays behind. Broad permissions let anyone with a valid key run any command on the box.

Core ssh key rotation best practices with OpenSSH certificates

The best way to rotate keys is to stop pushing static files to hosts. OpenSSH supports native certificate login for both users and hosts. An OpenSSH certificate is much simpler than a standard web certificate. It holds a public key, an identity name, allowed usernames, and specific rule options.

To set this up, your team creates a central authority key pair. You tell your servers to trust this single authority public key. When an engineer needs to log in, the key tool signs their public key. During signing, you set an identity string and list the allowed server usernames.

You must also set a short validity window on every signed certificate. By default, signed OpenSSH certificates stay valid forever if you skip this step. Setting a validity window of a few hours forces automatic key rotation. Once the timer runs out, the certificate stops working without touching the server. You never need to scrub old public keys from host files again.

Inventory and host key management

User keys are only half of the rotation problem. Servers also have host keys that prove their identity to clients. If you do not rotate host keys, attackers can spoof a server.

You can sign host public keys with your authority key as well. You mark the signature as a host certificate and list the server names. Clients then trust the central authority instead of saving individual host keys. This makes rotating server keys safe and simple for the whole team.

SSH Key Rotation + AWS IAM Best Practices: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Good rotation also means finding and removing old static keys on every machine. Teams should check all user folders for leftover key files on a set schedule. Removing static files ensures that every login uses a fresh, timed certificate.

What native key rotation solves and what it misses

Native key rotation and OpenSSH certificates solve the problem of lingering credentials. Stolen keys expire fast, and you do not have to manage large key lists. Your team controls who can log in to a machine and when.

Still, key rotation only checks identity at the moment a user logs in. Once the session starts, the server trusts the user until they log out. A valid key does not inspect or control individual commands in flight. If an engineer logs into a shared admin account, they can run destructive commands. They can also read raw database files that hold user emails or card numbers. Key rotation alone cannot approve specific actions or mask sensitive data in outputs.

Check SSH commands and file transfers on the host

OpenSSH keys and certificates decide who can log into a Linux host. They do not check what a one-shot command or file transfer does once the session starts. With hoop.dev, you run an open-source SSH server with guardrails on the host. It checks callers with OpenSSH certificates and works with ssh, sftp, scp, and rsync clients.

When a caller runs a one-shot command or moves a file, hoop.dev guardrails check the command or path before it runs. Then hoop.dev data masking masks matched sensitive patterns in downloads and output. Interactive shells carry no guardrails because keystrokes have no statement boundary. Teams can turn interactive shells off and allow only commands and file tasks. The audit trail logs who connected and what was allowed or refused, without storing keystrokes or file bytes. See the source on GitHub.

Frequently asked questions

How often should teams rotate static SSH keys?

If you use static keys, rotate them at least every ninety days. You should also rotate keys right away when a team member leaves. Switching to short-lived certificates lets you rotate keys every few hours instead.

Do OpenSSH certificates expire on their own?

No, OpenSSH certificates stay valid forever by default when you sign them. You must set an explicit validity time window during the signing step. Always set a short expiration window so certificates expire on their own.

What happens to an active session when a key rotates?

Rotating a key or expiring a certificate only blocks new logins. Any SSH session that is already open stays active until it closes. Teams must close active sessions manually if they need to cut off a user.

The query runs. The emails stay hidden.