Rotating SSH keys on three production VMs

I rotated the SSH keys on three Linux VMs in one evening, and nobody lost access they were supposed to keep. Two of the machines run our fleet tracking platform, one of them production, so getting locked out wasn't an option.

The whole job comes down to one rule: add the new key and prove it works before you remove the old one. Everything else in this post is about finding out what you're actually removing.

I've changed the IPs, hostnames, port numbers and other people's names. The commands are exactly what I ran.

Step 1: find every key that can log in

SSH keys live per user, in ~/.ssh/authorized_keys. So the first question is which users can log in at all, and whether sshd is reading keys from anywhere else:

grep -vE 'nologin|false' /etc/passwd | cut -d: -f1,6,7
sudo sshd -T | grep -Ei 'authorizedkeysfile|authorizedkeyscommand|passwordauthentication|permitrootlogin'

The second command matters more than it looks. If passwordauthentication is yes, removing keys doesn't lock anyone out, because they can still type a password. On all three of my machines it was already no, so keys were the only way in.

Then I listed every key on the box. My first version of this loop had a bug: it ran the file check as my normal user, which can't see inside /root, so it skipped root without saying anything. It also only looked in /home/*, which misses accounts like postgres whose home is /var/lib/pgsql. This is the version that works:

sudo bash -c 'for h in $(cut -d: -f6 /etc/passwd | sort -u); do f="$h/.ssh/authorized_keys"; [ -f "$f" ] && ssh-keygen -lf "$f" | sed "s|^|$f |"; done' | tee /tmp/keys.txt
awk '{print $3}' /tmp/keys.txt | sort -u | wc -l

The first line prints every key with the file it's in. The second counts unique keys by fingerprint, which is the real number, since the same key can sit in more than one account. On the dev box there were three entries but only two unique keys.

Step 2: work out who and what uses each key

The comment at the end of a key (umer@laptop, ssh-key-2025-11-29) is a hint, but anyone can edit it. The auth log is better. Every successful login records the user, the source IP and the key's fingerprint:

sudo grep -h "Accepted publickey" /var/log/secure* /var/log/auth.log* 2>/dev/null | awk '{print $9, $11, $NF}' | sort | uniq -c

On Oracle Linux and AlmaLinux the file is /var/log/secure; on Debian and Ubuntu it's /var/log/auth.log. Running both covers either.

On prod this one command told me most of what I needed:

KeyAccountLogins came fromMy admin keyadmin account9 different IPs, including my own, one with 193 loginsA teammate's keyadmin accountone IP abroad, 3 loginsgh-actions-deploydeploy5 Azure IPs, 7 logins each

The deploy key was the one to be careful with. Those Azure IPs are GitHub Actions runners, so revoking that key would have broken every deployment until someone updated the private key in the repo's secrets. Nobody would get an error at the time; the next deploy would just fail.

Two other things showed up that are worth knowing. Logins came in bursts of 7 to 12 connections a few seconds apart, which is what an SFTP sidebar or VS Code Remote does, not a person typing. And the server logs in UTC, so a login at 15:44 was me at 20:44 Pakistan time. If you don't recognise an IP, find out what it is before you revoke anything.

Step 3: the rotation itself

Before touching anything, open two SSH sessions and leave them open until the end. If one drops or you break something, the other is your way back in.

  1. From a fresh terminal, confirm the new key works and the old one gets Permission denied (publickey).

Only now remove the old key. Delete by something unique to that line, like its comment or line number, and check the result straight away.

sudo sed -i '/OLD-KEY-COMMENT/d' /home/USER/.ssh/authorized_keys
sudo ssh-keygen -lf /home/USER/.ssh/authorized_keys

Test from a third terminal. IdentitiesOnly=yes stops the client quietly falling back to the old key, which would make a broken setup look fine. Run sudo -v once you're in, to check you still have admin rights.

ssh -p PORT -i ~/.ssh/prod -o IdentitiesOnly=yes USER@HOST

Append the new public key. Use >> or tee -a; a single > wipes the file.

echo "PASTE prod.pub HERE" | sudo tee -a /home/USER/.ssh/authorized_keys

Back up the file you're about to edit. cp -p keeps the owner and permissions.

sudo cp -p /home/USER/.ssh/authorized_keys /home/USER/.ssh/authorized_keys.bak-2026-11-03

Create a new key on your own machine. I used a separate key per server, so losing one only exposes one box.

ssh-keygen -t ed25519 -C "prod-2026-11" -f ~/.ssh/prod

You don't need to restart sshd for any of this. It reads authorized_keys on every login.

If something goes wrong at step 4 or 6, copy the backup back from one of the sessions you kept open.

What was different on each machine

VM 1: one key, two places

This was an Oracle Cloud instance with a single key, the one Oracle generates at launch (its comment is ssh-key- plus the launch date). It was in opc's file and also in root's, but root's copy started with this:

no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"opc\" rather than the user \"root\".';echo;sleep 10;exit 142"

That forced command means a root login with the key prints a message and disconnects. It can't open a shell. I removed it anyway, because if the server still accepts the old key at all, the rotation isn't finished. My test showed this: the old key still got the "please log in as opc" message as root, which proved the line was still there. Once I deleted it, the old key got Permission denied.

On Oracle Cloud you don't need to change anything in the console. The launch key is written to the VM once, at first boot, and never again. GCP is different: it re-adds keys from project and instance metadata, so you'd have to remove them there too.

VM 2: dev, with an account to shut down

The dev box had the launch key in opc and root, plus a personal key for a second account. The plan was to replace the launch key, give root nothing, and revoke the second account entirely. For that account I emptied its key file and locked it, rather than deleting the user:

sudo truncate -s 0 /home/OTHERUSER/.ssh/authorized_keys
sudo usermod -L -s /sbin/nologin OTHERUSER
sudo passwd -S OTHERUSER   # should show LK

Nothing gets deleted, so it's easy to undo.

I also turned off root SSH login completely, since opc has sudo and nothing needs to log in as root directly:

sudo sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config
sudo sshd -t && sudo systemctl reload sshd
sudo sshd -T | grep permitrootlogin

Run sshd -t before every reload. It checks the config and stops a typo from taking sshd down. Also check /etc/ssh/sshd_config.d/, because a file in there can override the main config and the last command is how you see the setting that actually applies.

VM 3: prod

Prod was AlmaLinux on a non-standard SSH port, with three keys across two accounts. I was only replacing one of them, my own, which was also the key I was logged in with. That's the riskiest case, because a mistake locks out the person doing the work.

Two extra steps on this one. Each user has their own file, so after editing the admin account I checked /home/deploy/.ssh/authorized_keys separately to confirm the CI key was untouched. And because AlmaLinux runs SELinux, I reset the file's security label after editing it with sed:

sudo restorecon -v /home/USER/.ssh/authorized_keys
ls -l /home/USER/.ssh/authorized_keys   # owner USER, -rw-------

If the label or permissions are wrong, sshd silently ignores the file and the key you just added stops working.

What I got wrong, and what I'd tell you

I made two mistakes, and neither caused damage, because of the backups and the open sessions.

On the first VM I forgot to remove the old key from root's file. The test caught it, which is the reason to test the old key as well as the new one.

On the dev box I copied the new key into root's file by accident, which would have allowed direct root login. The fix took four commands: empty the file, set PermitRootLogin no, reload, and check. I'd rather find that out right away than three months later in an audit.

If you're about to do this yourself:

  • Count unique keys by fingerprint, across every account, with sudo.
  • Read the auth log before you revoke anything. Treat unknown IPs and CI runners as things that will break.
  • Add, test, then remove. Never the other way round.
  • Keep a second session open until the old key is refused.
  • Test the old key after removing it. "The new one works" only proves half the job.
  • Keep the .bak files for a day or two. They only hold public keys, so they don't give anyone access, and they're your undo if something you missed starts failing.