Sooner or later you need to rotate ssh keys for everyone who logs into your servers. Maybe someone left the team, maybe a laptop was stolen, or maybe the keys are simply years old and nobody remembers who has a copy. Whatever the reason, the fear is always the same: one wrong edit to authorized_keys and half your team is locked out of production. The fix is deliberate: add the new keys before you remove the old ones, test every login, and only then clean up. This guide walks through exactly that, in four phases you can run over a week without a minute of downtime.
Why rotating keys feels risky, and why it is not optional
An SSH key is a credential that never expires on its own. Passwords get changed when people leave or when a policy forces it, but a private key copied to a laptop in 2022 still opens your server in 2026. That is fine while the laptop stays in trusted hands. It stops being fine the day a contractor finishes a project, a teammate moves on, or a machine gets lost.
Rotation is also when most teams discover they never had an inventory. If you cannot list every key that can log into each server, you cannot call the server secure. A rotation forces you to build that list, and the list is worth keeping afterwards. If your team still logs in with passwords at all, fix that first and switch to key-only login before you try to rotate anything. Rotating passwords is a different problem; this guide is about keys.
How to rotate ssh keys in four phases
The whole method rests on one rule: never remove an old key until the new key has been proven to work. That means every server runs with both keys active for a few days. The overlap is the safety net. The four phases are inventory, collection, dual-key testing, and cleanup. Plan on a week from start to finish, most of it waiting while people test at their own pace.
Phase 1: Map every key before you touch anything
Start on the server side. On each machine, list every user account and read the keys each one holds:
ls /home/*/.ssh/authorized_keys /root/.ssh/authorized_keys
cat /home/*/.ssh/authorized_keys
Note which key belongs to whom. The comment at the end of each public key line usually names the owner, like ana@laptop. If a key has no comment or a generic one, ask the owner to identify it. Also check for keys in less obvious places: deployment scripts, CI runners, backup jobs, and monitoring tools that log in over SSH. Those service keys need rotation too, and teams forget them most. Write the full inventory down in one place: server, user, key comment, owner, purpose.
Phase 2: Collect the new keys
Ask each team member to generate a fresh key pair on their own machine. Ed25519 is the sensible default today:
ssh-keygen -t ed25519 -C "ana@company-laptop"
The comment should name the person and the machine; it is what you will read in authorized_keys six months from now. Ask people to protect the private key with a passphrase. Then collect only the .pub files. Never ask anyone to send a private key, and never accept one. A shared document, a ticket, or a repo with restricted access all work; a group chat does not, because keys pasted into chat get copied and screenshotted.
If you are setting up keys for several people for the first time, the same step applies. Our guide to setting up SSH key authentication for multiple team members covers the per-user setup in more detail.
Phase 3: Add the new keys, test, then remove the old ones
This is the core of the ssh key rotation process, and the overlap between old and new keys is what lets you replace ssh keys without downtime. On each server, append each new public key to the right authorized_keys file, keeping the old keys untouched:
cat ana-new.pub >> /home/ana/.ssh/authorized_keys
chmod 700 /home/ana/.ssh
chmod 600 /home/ana/.ssh/authorized_keys
Permissions matter. SSH ignores an authorized_keys file with loose permissions, so check them after every edit. If you manage more than a couple of servers, do this with a loop or a configuration tool rather than by hand. Hand-editing the same file on ten machines invites typos, and a typo in this file is exactly the lockout you are trying to avoid.
Now give the team a testing window, 48 hours is enough for most teams. Each person logs in with the new key and confirms it works:
ssh -i ~/.ssh/id_ed25519_new ana@server "echo login OK"
Track who has confirmed and chase the rest. Do not move on while anyone is untested, because the next step removes their only working key. On the server you can confirm logins in the auth log:
grep "Accepted publickey" /var/log/auth.log | tail -20
Once everyone has logged in with the new key, remove the old key lines from every authorized_keys file. Before you delete anything, back the file up with a dated copy:
cp ~/.ssh/authorized_keys ~/.ssh/authorized_keys.bak.2026-11-28
Then delete only the old lines, save, and have one person test again. If the new key still works after the old one is gone, the rotation is done on that server. Repeat the authorized keys rotation on each server until the inventory from phase 1 is fully covered.
Phase 4: Audit, revoke, and write it down
After the old keys are gone, verify rather than assume. Read the current authorized_keys on each server and compare it against your inventory. Every line should map to a known person or service. Anything unaccounted for gets investigated, not ignored. Check the login history as well. Our guide to auditing SSH logins with lastlog and journalctl shows how to confirm who actually logged in and when, which is the fastest way to spot a key you missed.
Then revoke properly. Deleting a line from authorized_keys revokes a plain key, but if the departing person also had access to backups, snapshots, or a shared jump host, clean those up too. A backup of authorized_keys that still contains the old key is a back door if someone restores it. Finally, write down the rotation date, who was included, and the next scheduled rotation. A note in your team runbook beats relying on memory. For larger teams, it is also worth reading about SSH certificates versus SSH keys, since certificates expire automatically and make future rotations far less manual.
What to do when someone gets locked out anyway
It happens even with a careful plan. Someone tests late, their old key is already gone, and they cannot get in. The recovery path is your hosting provider’s console access, which logs in without SSH. Almost every VPS provider offers a web console or rescue mode. Use it to log in as root, check the authorized_keys file, and re-add the correct key. That is why the backup from phase 3 matters: you can compare the current file against the backup and see exactly which line went missing.
Two habits prevent most lockouts. First, keep a second admin session open while you edit SSH configuration, and test the new login in a new terminal window. If it fails, the old session is still there to fix it. Second, take a snapshot before you start the rotation. Restoring a snapshot is a blunt tool, but it beats being locked out of a production server with no console access. If you have never tuned your SSH setup, our guide to hardening SSH on Ubuntu 24.04 covers the settings that make this kind of mistake harder to make in the first place.
How often should a team rotate SSH keys?
There is no single correct interval, but three triggers cover nearly every case. Rotate immediately when someone leaves the team or a device is lost or stolen. That is an incident response, not a scheduled event, and it should happen the same day. Rotate on a schedule for everyone else. Once a year is a reasonable baseline for a small team, and it gives you a regular chance to refresh the inventory. Rotate after any security incident on a server, even if you are not sure keys were involved. Rebuilding trust in access after an incident means assuming the old keys are compromised until proven otherwise.
The real goal of team ssh access management is not the rotation itself. It is knowing, at any moment, exactly who can log into your servers and with which key. A rotation done well leaves you with a current inventory, keys with names attached, and a documented date for the next one. The next rotation will be faster: the inventory already exists, the team knows the drill, and nobody has to guess which key belongs to whom.