Sub topic 13
Managing Multiple SSH Keys, Rotation, and Revocation
This chapter assumes that you can use a Linux or macOS command line, connect to a server with ssh, read Unix file permissions, and edit a file with nano or vim. You should also have an account on a remote Linux server where you can test changes.
SSH keys are credentials. A private key proves your identity, while the matching public key can be placed in a server account's ~/.ssh/authorized_keys file. “Passwordless” means that SSH does not ask for the account password. Your private key should still have a passphrase.
Use separate keys for separate purposes
Using one key everywhere makes revocation difficult. If that key is copied or stolen, every server that trusts it is affected. Create separate keys for different devices, users, or roles.
Common choices include:
| Key | Purpose | What to do if lost |
|---|---|---|
| Laptop key | Your normal administration | Remove its public key from every server |
| Desktop key | A second personal device | Remove only the desktop key |
| Temporary key | Short maintenance task | Remove it when the task ends |
| Automation key | A script or deployment service | Restrict its account and permissions |
A key comment helps you identify a key, but it does not provide security. The fingerprint is a stronger identifier.
ssh-keygen -lf ~/.ssh/id_ed25519.pubThe command prints the key type, fingerprint, and comment. Record fingerprints in your administration notes without recording private keys.
Configure multiple keys on the client
The SSH client may try several keys from your agent or default files. Too many attempts can cause authentication failures. Give each key a clear filename and select it with ~/.ssh/config.
Host production
HostName server.example.com
User alice
IdentityFile ~/.ssh/id_ed25519_production
IdentitiesOnly yes
Host staging
HostName staging.example.com
User alice
IdentityFile ~/.ssh/id_ed25519_staging
IdentitiesOnly yesNow connect with ssh production or ssh staging. Set the configuration file permissions so only your user can read it:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/configThe IdentitiesOnly yes setting tells SSH to offer the named key instead of trying every key available through the agent.
Rotate a key without losing access
Rotation means replacing an old credential with a new one. Add the new public key before removing the old key. Keep an existing administrative session open while testing.
- Generate a new key with a new filename.
- Install its public key in
authorized_keys. - Test a new connection using the new key.
- Confirm that the new connection reaches the intended account.
- Back up
authorized_keys. - Remove the old public key.
- Test again from a separate connection.
ssh-keygen -t ed25519 -C "alice-production-2026" \
-f ~/.ssh/id_ed25519_production_2026
ssh-copy-id -i ~/.ssh/id_ed25519_production_2026.pub [email protected]
ssh -o IdentitiesOnly=yes \
-i ~/.ssh/id_ed25519_production_2026 \
[email protected]A rotation window where both keys work is intentional. It prevents a typo or wrong file permission from locking you out.
Revoke a key
Revocation means removing trust in a public key. On a server, each authorized key normally occupies one line in ~/.ssh/authorized_keys. Identify the line by its fingerprint or comment, then remove only that line.
Make a backup first:
cp ~/.ssh/authorized_keys ~/.ssh/authorized_keys.backup
chmod 600 ~/.ssh/authorized_keys.backupEdit the file and delete the compromised or retired key. Do not delete keys belonging to other devices or administrators. If the private key may have been copied, revoke its public key immediately and review server logs for unexpected access.
After revocation, test from the affected device. A failed login confirms that the server no longer accepts that key, but it does not prove that no other credential can access the account.
For temporary access, use an expiration option on the authorized-key line:
restrict,from="203.0.113.10",expiry-time="20261201000000" ssh-ed25519 AAAA... temporary-maintenanceCheck that your OpenSSH version supports expiry-time. Restrictions such as from= and restrict reduce the damage if a temporary key escapes.
Exercise
What order should you use when replacing an SSH key on a server, and why should the old key remain temporarily?
Show answer
Generate and install the new public key, test it in a separate connection, back up the authorization file, and then remove the old key. Keeping the old key temporarily prevents a configuration mistake from locking out all administrators.