Planned SSH key rotation: verifying new access and revoking the old key

Replacing an SSH key on E-01: add the new key, verify a separate login and password-required sudo, revoke the old key and confirm denial.

A new key in the lock and the previous key beside it illustrate SSH key rotation.

A planned replacement of one administrator SSH key is complete on a training VPS. The new Ed25519 key was first added alongside the old one. A separate login and password-required sudo were verified before the old key was removed. After removal, the new key opened another fresh connection, while the server rejected the exact old key offered to it.

The commands below are adaptable examples for an environment you are authorised to administer: placeholders stand in for the address, user and paths. Three terminal frames show successive stages of the exercise on the E-01 training server.

Planned rotation starts with controlled access and no active attacker. A lost device or suspected key compromise requires a different response plan: the normal maintenance sequence does not cover that incident.

Before the first write: keep a verified recovery path

At the start, the old key worked and the authorisation file contained one key. Password-required sudo was also verified. The existing SSH connection was kept open until every check was complete. Separately, the provider console was used to confirm authenticated root access and a match with the expected server and current boot. This checks the availability of a fallback access path; it is not a full server recovery test.

The new key was created locally, separately from the old one, and protected with a passphrase. The private and public parts were checked for correspondence; private files have mode 0600. A key passphrase protects the local private file. A sudo password authorises an administrative action on the server. These are separate checks.

On another server, first identify the effective sources of authorised keys, the intended account and the active authorisation file. This exercise also checked that no SSH-identity-bound journals or persistence mechanism needed updating. If such a mechanism exists elsewhere, the rotation plan must account for it.

The following is only an example of local preparation in Bash. Replace every value in angle brackets. The known-hosts file must already contain a verified server host key; do not establish that trust by blindly accepting a key during rotation. Choose an unused path for the new key and do not approve overwriting an existing file.

SSH_HOST='<SERVER_ADDRESS>'
SSH_PORT='<SSH_PORT>'
SSH_USER='<ADMIN_USER>'
NEW_KEY='<LOCAL_NEW_PRIVATE_KEY_PATH>'
OLD_KEY='<LOCAL_OLD_PRIVATE_KEY_PATH>'
KNOWN_HOSTS='<VERIFIED_KNOWN_HOSTS_FILE>'
OLD_PROBE_LOG='<PRIVATE_LOCAL_DEBUG_LOG_PATH>'

ssh-keygen -t ed25519 -f "$NEW_KEY" -C 'planned-rotation'

The key-generation command prompts for a passphrase. Do not leave it empty. Keep the private key and passwords off the server and out of public reports.

Introduce the new key while retaining the old one

The first write changed the exact original file into the overlap state: the old key plus one new key. It used an atomic replacement within the same .ssh directory, checking the original bytes, ownership and permissions. The file retained mode 0600 and the directory retained mode 0700. The old key line was preserved unchanged.

This takes more than redirecting text into authorized_keys. The prepared file must contain only the approved change and preserve required options and every other authorised entry. The original state is compared again before replacement, and the new file and directory are synchronised to disk. This exercise used two such replacements: adding the new key, then removing the old one. There is no ready-to-run authorized_keys overwrite command here: one training example cannot safely determine that operation for another server.

During overlap, the retained old session stays open. Removal of the old entry waits for independent verification of the new access path.

Verify a fresh independent connection

The new key opened an independent SSH connection without reusing the old connection. The screenshot shows UID 1000, two authorisation lines and permissions 700/600. A line count alone cannot establish which keys are present: the agreed file bytes were checked separately.

The new key already opens a separate SSH connection; both keys are authorised during overlap.

The adaptable example below explicitly selects the local private key, disables the agent and connection multiplexing, and permits only public-key authentication. It keeps server host-key checking enabled. The client may prompt for the local passphrase to decrypt the private key.

ssh -F /dev/null -p "$SSH_PORT" \
  -o IdentityAgent=none -o IdentitiesOnly=yes \
  -o ControlMaster=no -o ControlPath=none \
  -o PreferredAuthentications=publickey \
  -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no \
  -o StrictHostKeyChecking=yes -o UserKnownHostsFile="$KNOWN_HOSTS" \
  -i "$NEW_KEY" -tt "${SSH_USER}@${SSH_HOST}"

After login, check the administrative action separately. Here is an adaptable example for an environment whose agreed sudo policy requires a password:

id -u
sudo -k
sudo -n true
sudo -v
sudo id -u
sudo -k

In the completed exercise, clearing the sudo timestamp was followed by the expected refusal of a password-free attempt. Password-required sudo then succeeded and returned UID 0. If the password-free check unexpectedly succeeds, review the effective privileges and test conditions before removing the old key. A successful SSH login alone is insufficient.

Verify more than the SSH login: the administrator can obtain root privileges through password-required sudo.

The label grant excerpt in the frame identifies only the displayed excerpt of genuine sudo -l output. It does not mean the image contains the full sudo policy.

Withdraw only the agreed old key

After both positive checks, the overlap file was compared again with the exact expected bytes and metadata. The second atomic replacement retained only the new key. The final bytes, ownership and permissions were checked separately.

An unknown additional key or an unrelated file change stops this step. Do not empty the file to obtain an expected line count. Likewise, after a disconnect or an unknown result, do not automatically repeat an addition or removal. Use the retained session or verified console to read the state first and determine whether it is the exact original, overlap or final version. Any other state requires separate reconciliation.

Rollback was not needed in this exercise. Arbitrarily restoring an old file copy is not a universal remedy: it could erase someone else's change or restore revoked access.

The new key works and the old key is denied

After the old key was removed, another fresh connection was opened with the new key. It worked, and password-required sudo again returned UID 0. One key remained in the file; its exact contents were verified separately.

The earlier example using NEW_KEY can be used for this repeat login. It must create a new connection after removal rather than continue an already open session.

The old key was then tested in a separate new connection. The client had access to that exact key and actually offered it to the server. The server responded with Permission denied (publickey)., and SSH exited with status 255. SSH debug was checked separately to establish that the offered identity matched the exact old key.

After revocation, the new key works and the server rejects the old key on the verified login path.

An example of a separate old-key check with a private debug log. Choose a new log path inside a private directory outside Git; the umask below restricts permissions on the newly created file.

umask 077
ssh -vv -E "$OLD_PROBE_LOG" -F /dev/null -p "$SSH_PORT" \
  -o IdentityAgent=none -o IdentitiesOnly=yes \
  -o ControlMaster=no -o ControlPath=none \
  -o PreferredAuthentications=publickey \
  -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no \
  -o StrictHostKeyChecking=yes -o UserKnownHostsFile="$KNOWN_HOSTS" \
  -i "$OLD_KEY" -T "${SSH_USER}@${SSH_HOST}" true
old_probe_status=$?
printf 'SSH exit status: %s\n' "$old_probe_status"

Keep the debug log private: it can contain addresses, paths and fingerprints. Verify that the offered identity matches the exact old key and that authentication ends in denial. Status 255 alone is insufficient: a network error, host-key mismatch or unavailable private key can also accompany that status. Such outcomes do not prove revocation.

Switch working references to the new identity

Once both outcomes were verified, the working operator references were switched to the new public key and a separate agent holding one new key. The other parameters and SSH profile bytes were preserved. The deploy-cli host ssh-policy inspect command checked the new references by reading them without performing writes.

Only then was the retained old SSH connection closed normally. The new agent remained available for work; old local keys were not automatically deleted. Deleting a local file and revoking server authorisation are separate actions.

In this exercise, rotation required no changes to SSH policy, passwords, sudo rules or the firewall, and no reload, restart or reboot. The sequence was what mattered: retain a recovery path, establish a new independent login, verify administrative access, revoke the old key, and check both keys separately afterwards.

  1. Operations & Security A separate administrator: from key login to the right privileges
  2. Operations & Security Your first SSH login: recognising your server
  3. Operations & Security When to restrict root and password-based SSH
  4. Operations & Security WireGuard on Debian: private SSH access and a post-reboot check
  5. Operations & Security Recovering VPS access when SSH stops working