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:

  1. 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_ed25519 stands 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 .pub file exists, its fingerprint from ssh-keygen -lf ~/.ssh/id_ed25519.pub must 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

  2. 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.com stands for your SSH destination. If SSH uses a nondefault port, include the same -p option each time. -F /dev/null skips settings such as aliases and connection sharing, and IdentityAgent=none disables the agent. StrictHostKeyChecking=yes requires a matching trusted host key.
    Related: How to verify SSH host key fingerprints before connecting

  3. 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.

  4. Back up the server's authorized keys file from the fallback session.
    $ cp -ip ~/.ssh/authorized_keys \
      ~/.ssh/authorized_keys.before-rotation

    The -p option preserves permissions. If -i prompts to overwrite the backup, answer n and use an unused path. A restore uses that same path.

  5. 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_next or its .pub companion exists, choose an unused name. The chosen path goes in the later replacement-key -i options. Its .pub companion goes in the next command. OpenSSH prompts for a passphrase and prints the new SHA256 fingerprint.

  6. Display the replacement public key on the client.
    $ cat ~/.ssh/id_ed25519_next.pub

    The .pub file contains the one line to copy to the server. The matching private key stays on the client.

  7. Open ~/.ssh/authorized_keys in your text editor from the fallback session.
  8. 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 .pub line replaces everything from ssh-ed25519 through laptop-next. The matching old entry's options go before the key type, with the replacement's expiry if policy requires expiry-time.

  9. Save ~/.ssh/authorized_keys with both keys present.

    OpenSSH reads the file on each authentication. The fallback session stays open for recovery.

  10. 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=yes excludes other keys offered by the agent, and PreferredAuthentications=publickey prevents password fallback. The command may ask for the new key's passphrase before id -un prints 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, with IdentitiesOnly yes and ControlPath none. The old entry stays in place until the server's sshd log records Accepted publickey with the new SHA256 fingerprint for that run.

  11. Delete the single line in ~/.ssh/authorized_keys that 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.

  12. Save ~/.ssh/authorized_keys with 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_keys in 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.

  13. 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.

  14. 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 denied is 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.

  15. 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.

Share this guide