An SSH private key can outlast the laptop or job it was issued for. On an OpenSSH server, you can rotate a user's SSH key without losing access by authorizing its replacement while the old key still works.
An account's authorized_keys file can grant access to several keys, each with its own restrictions. A replacement inherits those restrictions only if its entry includes the same options, such as a source address limit or a ban on agent forwarding.
The old key must open a shell and match exactly one line in the server account's ~/.ssh/authorized_keys file. Stop if you cannot identify the entry or its options, if the key appears more than once or is accepted through another key source, or if it runs a forced command in place of a shell. The client needs a trusted host key for the server.
Steps to rotate an SSH key without losing access:
- Identify the old private key from a terminal on the client.
$ ssh-keygen -y -f ~/.ssh/id_ed25519 | tee /dev/tty | ssh-keygen -lf -
~/.ssh/id_ed25519stands for your old private key. The command displays its public key and SHA256 fingerprint. The base64 field after the key type identifies the server entry. If a sibling.pubfile exists, its fingerprint fromssh-keygen -lf ~/.ssh/id_ed25519.pubmust match because SSH can load that file for login. Stop if either check fails.
Related: How to extract a public key from an SSH private key - Open an SSH session with the old key to keep as a fallback.
$ ssh -F /dev/null -o IdentitiesOnly=yes \ -o IdentityAgent=none \ -o PreferredAuthentications=publickey \ -o StrictHostKeyChecking=yes \ -i ~/.ssh/id_ed25519 \ alex@server.example.com
alex@server.example.comstands for your SSH destination. If SSH uses a nondefault port, include the same-poption each time.-F /dev/nullskips settings such as aliases and connection sharing, andIdentityAgent=nonedisables the agent.StrictHostKeyChecking=yesrequires a matching trusted host key.
Related: How to verify SSH host key fingerprints before connecting - List the authorized key fingerprints in the fallback session.
$ ssh-keygen -lf ~/.ssh/authorized_keys 256 SHA256:M6/SWwRJDmTIc4yloYLLaMcAhrMF7sJTcSJ6kig4I0M laptop-old (ED25519) 256 SHA256:c1lWDZ5dhj5mADvvLydisYFuM81Lp1YbenM4y9Pi8oM deploy (ED25519)
The fallback shell runs as the account whose key you are replacing. The old private key's fingerprint must match exactly one entry before any edit. The other fingerprints give you a baseline for the final file check.
- Back up the server's authorized keys file from the fallback session.
$ cp -ip ~/.ssh/authorized_keys \ ~/.ssh/authorized_keys.before-rotation
The
-poption preserves permissions. If-iprompts to overwrite the backup, answernand use an unused path. A restore uses that same path. - Generate a passphrase-protected replacement key pair under an unused filename on the client.
$ ssh-keygen -t ed25519 -C "laptop-next" \ -f ~/.ssh/id_ed25519_next
If either
~/.ssh/id_ed25519_nextor its.pubcompanion exists, choose an unused name. The chosen path goes in the later replacement-key-ioptions. Its.pubcompanion goes in the next command. OpenSSH prompts for a passphrase and prints the newSHA256fingerprint. - Display the replacement public key on the client.
$ cat ~/.ssh/id_ed25519_next.pub
The
.pubfile contains the one line to copy to the server. The matching private key stays on the client. - Open
~/.ssh/authorized_keysin your text editor from the fallback session. - Append the replacement public key as one line at the end of
~/.ssh/authorized_keys.no-agent-forwarding,no-X11-forwarding,expiry-time="20301231Z" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIL8mtklQZVC6v2z6aNXhlhFVCni3w/trriO1C2LN2+4O laptop-next
Your own
.publine replaces everything fromssh-ed25519throughlaptop-next. The matching old entry's options go before the key type, with the replacement's expiry if policy requiresexpiry-time. - Save
~/.ssh/authorized_keyswith both keys present.OpenSSH reads the file on each authentication. The fallback session stays open for recovery.
- Confirm a fresh login with the replacement key from the client.
$ ssh -F /dev/null -o IdentitiesOnly=yes \ -o PreferredAuthentications=publickey \ -o StrictHostKeyChecking=yes \ -i ~/.ssh/id_ed25519_next \ alex@server.example.com id -un alex
IdentitiesOnly=yesexcludes other keys offered by the agent, andPreferredAuthentications=publickeyprevents password fallback. The command may ask for the new key's passphrase beforeid -unprints the remote account name.Removing the old entry cuts off new connections that still rely on that key. If a job, service, or client uses it, switch its saved SSH configuration to the new key. A fresh run from that configuration must use only the new
IdentityFile, withIdentitiesOnly yesandControlPath none. The old entry stays in place until the server'ssshdlog recordsAccepted publickeywith the newSHA256fingerprint for that run. - Delete the single line in
~/.ssh/authorized_keysthat matches the old public key.Match the base64 field you derived from the old private key, even if the line starts with options. The replacement and unrelated entries stay in the file.
- Save
~/.ssh/authorized_keyswith the old key removed.If your editor stays open after saving, quit the editor to return to the fallback shell with the SSH session still connected.
If the replacement fails and the old key is still trusted, restoring the backup replaces the live file with the pre-rotation file, removing the new entry and restoring access for the old key. To restore it, run
cp -p ~/.ssh/authorized_keys.before-rotation ~/.ssh/authorized_keysin the fallback shell, using the backup path you chose. If the old key may be compromised, repair the new entry through a trusted session or the server console. - Confirm the saved key fingerprints from the fallback session.
$ ssh-keygen -lf ~/.ssh/authorized_keys 256 SHA256:c1lWDZ5dhj5mADvvLydisYFuM81Lp1YbenM4y9Pi8oM deploy (ED25519) 256 SHA256:fkn3j5HOSdrcBG+m65wp9dPZERetikTFPXgIC6askOw laptop-next (ED25519)
The replacement fingerprint should match the one printed at generation. The superseded fingerprint should be absent and the unrelated fingerprints should still match the baseline.
- Confirm that a fresh login with the old private key is refused.
$ ssh -F /dev/null -o IdentitiesOnly=yes \ -o IdentityAgent=none \ -o PreferredAuthentications=publickey \ -o StrictHostKeyChecking=yes \ -i ~/.ssh/id_ed25519 \ alex@server.example.com id -un alex@server.example.com: Permission denied (publickey).
Permission deniedis the authentication result to look for. A connection failure, host-key error, cancelled passphrase prompt, key-load failure, or signing error leaves the test inconclusive. In those cases, keep the fallback session open. If the old key still works, look for it in the account's other configured key sources. - Confirm a fresh login with the replacement private key after the removal.
$ ssh -F /dev/null -o IdentitiesOnly=yes \ -o PreferredAuthentications=publickey \ -o StrictHostKeyChecking=yes \ -i ~/.ssh/id_ed25519_next \ alex@server.example.com id -un alex
Once the old key is refused and the remote account name prints with the replacement, you can close the fallback session. A key used on other accounts or servers remains active wherever it is still authorized.