Automated SSH Key Rotation

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up automated ssh key rotation by running scheduled scripts or signing short-lived OpenSSH certificates. These tools replace old public keys on servers before those keys become a risk. Most teams start by copying static public keys to servers using setup tools. When roles stay too broad, a leaked key lets an attacker log in to every server. How automated ssh key rotation works in practice Teams build automated ssh key rotation in two main ways. The first way uses a central script or se

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.

You set up automated ssh key rotation by running scheduled scripts or signing short-lived OpenSSH certificates. These tools replace old public keys on servers before those keys become a risk. Most teams start by copying static public keys to servers using setup tools. When roles stay too broad, a leaked key lets an attacker log in to every server.

How automated ssh key rotation works in practice

Teams build automated ssh key rotation in two main ways. The first way uses a central script or setup tool on a schedule. The tool makes a new key pair for a user or service account. Next, it pushes the new public key to the target server list. Then, it removes the old public key from the server file. Last, it puts the new private key in a secure store for the user.

This file push method takes work to run across hundreds of machines. If a server is offline during the run, the update fails. That server keeps the old key until the next scheduled job runs. Teams also must track every local account on every host.

The second way uses OpenSSH certificates instead of static key files. A Certificate Authority key signs a user public key to make a certificate. Each certificate holds a public key, an identity string, and valid user names. It also holds a set of options and a time window for use. Servers trust the central authority key instead of tracking individual user keys. When the time window ends, the certificate stops working without touching the server. Users must ask the authority to sign a fresh certificate for their next login.

Why key rotation alone does not control active sessions

Rotating keys and using short certificates solve a major login problem. They reduce the risk of old, forgotten keys sitting on servers for years. When an engineer leaves the team, their old key or certificate expires soon. You do not have to hunt down stray public keys on every host.

Still, key rotation only checks who can open a session at the door. Once the server accepts the key, the user gets a full shell. Native SSH keys do not inspect the commands a user runs inside that shell. A valid user with a fresh key can still run a bad script. They can delete production files or read sensitive customer data from disk.

SSH Key Rotation + Automated Deprovisioning: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Static roles also make it hard to split read and write work on a host. If two engineers share the same server account, they share the same host permissions. Standard SSH logs show who logged in, not what they typed in the session. Teams still lack command-level rules and clear visibility into live terminal work.

Planning a safe key update schedule

Before you turn on a rotation script, map out every automated script and service. Service accounts often break first when you swap out their private keys. Update the client side with the new private key before deleting the old public key. Keep both the old and new public keys active during a short overlap window. Once you confirm the new key logs in fine, delete the old public key. Test this full flow in a staging setup before running it on production hosts.

Add command and file guardrails to SSH access

Host firewalls and certificate principals control who opens an SSH session. Yet an allowed user or agent can still run a bad one-shot command or pull sensitive files over sftp. In practice, hoop.dev sits inline on the host as an open-source SSH server with guardrails that uses OpenSSH certificates. For every one-shot command or sftp task an agent or human runs in production, hoop.dev decides to pass, mask, block, or escalate it for review.

Using hoop.dev SSH controls, guardrails check one-shot commands, env variable names, and sftp or scp paths before they run. Port forwards default to deny unless the target is listed. Because interactive shells have no statement boundary and carry no guardrails, teams can turn them off so every command is checked and logged. Check out the MIT-licensed project on GitHub.

Frequently asked questions

How often should teams rotate static SSH keys?

Many security standards ask teams to rotate static keys every thirty to ninety days. If you use OpenSSH certificates, you can set validity windows for just a few hours.

What happens to an open SSH session when a key rotates?

Nothing happens to an active session when you remove a public key or a certificate expires. SSH only checks the key during the initial login, so open shells stay active.

How are OpenSSH certificates different from TLS certificates?

OpenSSH certificates use a much simpler format than the X.509 certificates used in TLS. They hold a public key, identity info, user or host names, and basic options.

The query runs. The emails stay hidden.