Sub topic 13
Managing multiple keys, rotation, and revocation
One key pair is enough to get started, but it stops being enough once you have several laptops, several servers, or a teammate who leaves. This chapter covers how to keep more than one key without confusion, how to replace a key on a schedule, and how to cut off a key that should no longer work.
This chapter assumes you can already generate a key with ssh-keygen, install it with ssh-copy-id, and edit ~/.ssh/config. It also assumes you know that authorized_keys holds one public key per line.
Why you end up with more than one key
Several situations lead to more than one key pair:
- One key per device. Each laptop or workstation gets its own key, so losing one device means removing one line from each server. If you copy a single private key between machines, you cannot tell which copy was stolen, and you must retire the key everywhere.
- One key per purpose. A key for personal servers, one for work, and one for a code hosting service keeps a compromise in one area from reaching the others.
- Keys for automation. A backup script or deployment job should have its own key, with no passphrase if it must run unattended. You then restrict what that key can do (covered below).
A good rule is that a private key never leaves the machine where it was generated. If a second machine needs access, generate a second key there.
Naming and generating keys
Name key files by purpose and date so that you can tell them apart later. Put the same information in the comment (-C), because the comment is the label that appears on the server's authorized_keys line.
ssh-keygen -t ed25519 -C "alice@laptop-2026-11" -f ~/.ssh/id_ed25519_work_2026
ssh-keygen -t ed25519 -C "alice@laptop-personal-2026-11" -f ~/.ssh/id_ed25519_personal_2026The comment is not checked during login, and anyone with write access can change it. It is a label for humans, not a security feature.
Telling ssh which key to use
By default, ssh offers the keys in your agent and the default key files one after another. The server counts each rejected key as a failed attempt. With many keys loaded, you can hit the server's MaxAuthTries limit (6 by default) before the right key is tried. The error is Too many authentication failures.
The fix is to name the key per host and add IdentitiesOnly yes, so ssh offers only the key you named.
Host git.example.com
User git
IdentityFile ~/.ssh/id_ed25519_work_2026
IdentitiesOnly yes
Host home
HostName home.example.org
User alice
IdentityFile ~/.ssh/id_ed25519_personal_2026
IdentitiesOnly yesWith IdentitiesOnly yes, the key still comes from the agent if it is loaded there, but ssh offers no other key. You can test a single key without editing the config:
ssh -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519_work_2026 [email protected]To see what the agent currently holds, run ssh-add -l. It prints one fingerprint and comment per key. Run ssh-add -D to unload all of them, which is useful when you are debugging offer order.
Knowing what is installed on a server
On each server, ~/.ssh/authorized_keys is the full list of who can log in as that account. Print the fingerprint of every key in it:
ssh-keygen -lf ~/.ssh/authorized_keysEach output line shows the key size, the SHA256 fingerprint, the comment, and the key type. To match one of those lines to a key on your own machine, run ssh-keygen -lf ~/.ssh/id_ed25519_work_2026.pub and compare fingerprints. The comment can be wrong, but the fingerprint cannot.
If a key shows up in the list that nobody can account for, treat it as a possible intrusion. Do not simply delete it. First record the line and when the file was last modified (ls -l ~/.ssh/authorized_keys), then investigate.
For ongoing audits, set LogLevel VERBOSE in /etc/ssh/sshd_config. The server then logs the fingerprint of the key used for each successful login, so you can see which keys are in use and which have gone silent.
Limiting what a key can do
Lines in authorized_keys can start with options that restrict the key. This is how you give a backup job a key that cannot do anything else.
from="203.0.113.0/24",restrict,command="/usr/local/bin/backup.sh" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyDataOnly backup@nasfrom="..."accepts the key only from the listed addresses or hostnames.restrictturns off port forwarding, agent forwarding, X11 forwarding, and terminal allocation. It is available in OpenSSH 7.2 and later.command="..."runs that command no matter what the client asks for. The client's requested command is placed in theSSH_ORIGINAL_COMMANDenvironment variable.
These options are enforced on the server, so a thief who steals the private key still hits them. They narrow the damage but do not replace revocation.
Rotating a key
Rotation means replacing a key pair with a new one. Reasons include a schedule, a suspected exposure, or a move to a stronger key type (for example, from a 2048-bit RSA key to Ed25519). The order of steps matters: add the new key and test it before you remove the old one. If you reverse that order, you can lock yourself out.
The commands for one server look like this:
ssh-keygen -t ed25519 -C "alice@laptop-2027-05" -f ~/.ssh/id_ed25519_work_2027
ssh-copy-id -i ~/.ssh/id_ed25519_work_2027.pub [email protected]
ssh -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519_work_2027 [email protected] 'echo new key works'Only after the last command prints new key works, remove the old line on the server. Keep your existing session open while you do this. This command writes a new file without the old key, and the wc check protects against ending up with an empty file:
umask 077
cp ~/.ssh/authorized_keys ~/.ssh/authorized_keys.bak
grep -vF 'alice@laptop-2026-11' ~/.ssh/authorized_keys > ~/.ssh/authorized_keys.new
wc -l ~/.ssh/authorized_keys.new
mv ~/.ssh/authorized_keys.new ~/.ssh/authorized_keysIf wc reports 0, stop and restore from the .bak file. Because the match is on the comment text, check that the pattern is specific enough that it matches only the line you want to remove.
Then update IdentityFile in ~/.ssh/config, unload the old key with ssh-add -d ~/.ssh/id_ed25519_work_2026, and delete the old private and public files.
Changing a passphrase (ssh-keygen -p -f ~/.ssh/id_ed25519_work_2026) is not rotation. It re-encrypts the same private key. If someone already copied the file, they hold the same key, and they can try passwords against their copy offline.
Revoking a key
Revocation means making a key stop working, usually because a device was lost, stolen, or retired, or because a person left the team.
Remove it from authorized_keys
The main step is deleting the key's line from authorized_keys on every server and account where you installed it. That is why a list of where each key lives is worth keeping. Without one, you are searching every machine you can think of.
Removing the line blocks new logins with that key. It does not end sessions that are already open. List and end them if you suspect misuse:
who
sudo pkill -KILL -t pts/1Replace pts/1 with the terminal shown by who for the session you want to end.
Block a key for the whole host
If you cannot reach every account right away, you can block a key host-wide. Add it to a revocation file and point sshd at it.
sudo sh -c 'cat old_laptop.pub >> /etc/ssh/revoked_keys'
echo 'RevokedKeys /etc/ssh/revoked_keys' | sudo tee -a /etc/ssh/sshd_config
sudo sshd -t && sudo systemctl reload sshdOn Debian and Ubuntu, the service is named ssh. The listed key is then refused for every user on that host. For larger fleets, OpenSSH also supports a compact binary revocation list created with ssh-keygen -k.
Handling a lost laptop
A strong passphrase buys time, because the thief cannot use the key at once. It does not make the key safe. Assume the key can be attacked offline, and revoke it everywhere. Also review the logs on your servers for logins from that key's fingerprint during the period since it was lost.
Limits of this approach
This model has a scaling problem: every server holds its own list, so each key must be removed one server at a time, and expired access is easy to forget. Two approaches reduce that work. Configuration management tools can push a single approved authorized_keys to all hosts. OpenSSH certificates let you sign keys with a certificate authority so they expire automatically. Certificates are a larger topic than this tutorial covers, but if you manage many servers, they are worth learning.
Summary of a healthy key practice
| Practice | Reason |
|---|---|
| One key per device and purpose | A compromise affects one key and is easy to revoke |
| Descriptive file names and comments | You can identify a key's owner and age on the server |
| `IdentitiesOnly yes` with `IdentityFile` | Avoids `Too many authentication failures` and unintended key offers |
| Restrictions on automation keys | Limits what a stolen key can do |
| Add and test before removing | Prevents lockout during rotation |
| A record of where each key is installed | Makes revocation complete |
Exercise
You rotate your key. You generate a new key, install it on the server with ssh-copy-id, then immediately delete the old line from authorized_keys, and afterward you find you cannot log in. What went wrong, and what should the order of steps be?
Show answer
The new key was not tested before the old one was removed. If the new key had a problem, such as wrong permissions on ~/.ssh or authorized_keys, or the wrong IdentityFile in the config, the old key was the only working way in and it was gone. The correct order is: generate the new key, install it, open a new session using only the new key (ssh -o IdentitiesOnly=yes -i <newkey>) and confirm it works, keep an existing session open, and only then remove the old line, unload it from the agent, and delete the old private key.
Exercise
A colleague's laptop is stolen. Their key was installed on three servers, and it had a passphrase. Is it safe to do nothing because of the passphrase? List the steps you take.
Show answer
No. The passphrase slows an attacker down but allows offline guessing against the copied key file. Remove the key's line from authorized_keys for every account on all three servers (matching by fingerprint with ssh-keygen -lf), end any open sessions for that key with who and pkill, optionally add the public key to a host-wide RevokedKeys file, review logs for logins with that fingerprint since the loss (LogLevel VERBOSE records it), and then give the colleague a new key generated on a new device.