How to delete SSH keys from your computer and servers

Delete an SSH key properly: remove the key files, unload it from ssh-agent, revoke it on servers and GitHub, and replace it without locking yourself out.

6–9 minutes
Hands typing on a laptop keyboard

To delete an SSHSSH A secure way to type commands on a server from your own computer, as if you were sitting at it. More about SSH → key completely, you remove it in up to four places: the key files on your computer, the copy loaded in ssh-agent, every server that trusts it, and any service such as GitHub where you added it. This guide is for anyone retiring an old key, cleaning up after a lost laptop, or resetting a key they no longer trust.

Deleting the files in ~/.ssh/ only stops you using the key. To stop the key working, remove its public half from ~/.ssh/authorized_keys on each server and from the SSH keys page of services like GitHub. Then run ssh-add -d to unload it from the agent. If you are replacing the key, add the new key everywhere first and only then remove the old one.

Before you delete anything

An SSH key is a pair: a private key (for example id_ed25519) that stays on your computer, and a public key (id_ed25519.pub) that you copy to servers. Each server keeps the public keys it trusts in a file called authorized_keys. Deleting the private key on your laptop does not touch those server files, so the public key stays trusted until you remove it there too.

That matters in two situations:

  • You are retiring a key you still have. Revoke it on servers first, while you can still log in with it, then delete the files.
  • A key may be compromised (a stolen laptop, a key pasted somewhere it should not be). Revoke it on every server and service straight away, from another computer or another key. Deleting your local copy does nothing to stop someone else using theirs.

If this key is your only way into a server, do not remove it from authorized_keys until you have a second working way in: another key, a password login, or your host’s web console. Otherwise you can lock yourself out.

Find out which key is which

List the keys on your computer and their fingerprints, so you delete the right one:

ls -l ~/.ssh
ssh-keygen -lf ~/.ssh/id_ed25519.pub

The second command prints the key’s size, a fingerprint starting SHA256:, and the comment at the end (often your email or a name like exampleco-laptop). Note the comment and fingerprint: you will use them to spot the same key on servers and on GitHub. If you are not sure where your files are, see where SSH keys are stored.

Step 1: Remove the key from servers

This is the step that actually revokes access. On each server, the trusted keys for an account are in that account’s ~/.ssh/authorized_keys, one key per line.

  1. Log in to the server, ideally with a different key than the one you are removing.
  2. Make a backup of the file:
cp ~/.ssh/authorized_keys ~/.ssh/authorized_keys.bak
  1. Open the file and find the line that ends with your key’s comment:
nano ~/.ssh/authorized_keys

Each line looks like this:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleOnlyNotARealKey exampleco-laptop
  1. Delete that whole line (in nano, put the cursor on it and press Ctrl+K), then save with Ctrl+O and exit with Ctrl+X.
  2. From another terminal, check you can still log in with the key you mean to keep before closing your current session.

If the comment is not unique, compare fingerprints instead. This prints a fingerprint for every line in the file, in order:

ssh-keygen -lf ~/.ssh/authorized_keys

Repeat for every account the key was added to. A key added for root lives in /root/.ssh/authorized_keys, and a key added for exampleco lives in /home/exampleco/.ssh/authorized_keys. If your host uses cPanelcPanel The most popular hosting control panel: a website where you manage your email, files, databases and domains with point-and-click tools instead of commands. More about cPanel →, keys added through its SSH Access page are managed there as well, so check that page too.

Step 2: Remove the key from GitHub and similar services

Services that use SSH keys keep their own list. On GitHub, open your profile picture, then Settings, then SSH and GPG keys. Each key is shown with its title and fingerprint. Match the fingerprint from ssh-keygen -lf, click Delete and confirm. GitLab and Bitbucket have a similar SSH keys page in your account settings.

Also check any deploy keys you added to individual repositories, which are listed in that repository’s own settings rather than your account settings.

Step 3: Unload the key from ssh-agent

ssh-agent is a background program that holds unlocked keys in memory, so you do not retype your passphrase. A key you delete from disk can stay usable in the agent until it is cleared. On Linux, macOS or Windows:

ssh-add -l
ssh-add -d ~/.ssh/id_ed25519

The first command lists loaded keys by fingerprint. The second removes one key. To remove every key from the agent at once:

ssh-add -D

On Windows, the OpenSSH agent service keeps added keys between restarts, so running ssh-add -d or ssh-add -D matters even after you delete the files. On macOS, if you saved the passphrase in your keychain, you can also remove the entry for that key in the Keychain Access app.

Step 4: Delete the key files on your computer

Now remove both halves of the pair. On Linux and macOS:

rm ~/.ssh/id_ed25519 ~/.ssh/id_ed25519.pub

On Windows, in PowerShell:

Remove-Item "$env:USERPROFILE\.ssh\id_ed25519", "$env:USERPROFILE\.ssh\id_ed25519.pub"

Replace id_ed25519 with your key’s real name, such as id_rsa or work_key. Then open ~/.ssh/config, if you have one, and remove or update any IdentityFile line that points at the deleted key. Otherwise SSH will warn that it cannot find the file each time you connect to that host.

Removing a server’s key from known_hosts

People often mean a different file when they ask how to delete SSH keys: known_hosts. This is where your computer remembers each server’s own host key. If a server is rebuilt, or its IP addressIP address The number address of a device on the internet, like 203.0.113.10. Computers use it to find each other, the way post uses street addresses. More about IP address → is reused by a new server, you will see a warning that the remote host identification has changed and SSH will refuse to connect.

If you are sure the change is expected, remove the old entry by host name or IP address:

ssh-keygen -R example.com
ssh-keygen -R 203.0.113.10

ssh-keygen saves the previous file as known_hosts.old. The next time you connect, SSH shows the new fingerprint and asks you to accept it. If you did not expect the server to change, stop and check with whoever runs it first: an unexpected change can mean you are not talking to the server you think you are.

How to reset an SSH key

Resetting a key means replacing it with a new pair. Do it in this order so you are never without a working login:

  1. Create the new key with a new name so it does not overwrite the old one:
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_new -C "exampleco-laptop-new"
  1. Add the new public key to each server and service. The SSH key setup guide shows how with ssh-copy-id, and the GitHub guide covers adding it to GitHub.
  2. Test the new key on every server:
ssh -i ~/.ssh/id_ed25519_new exampleco@203.0.113.10
  1. Only after every test works, follow Steps 1 to 4 above to remove the old key.
  2. Optionally rename the new files back to the default name, or point IdentityFile at them in ~/.ssh/config.

If you only want to change the passphrase, you do not need a new key at all. This keeps the same key and updates the passphrase that protects it:

ssh-keygen -p -f ~/.ssh/id_ed25519

Common problems

SymptomCauseFix
Old key still logs in after deleting itIt is still loaded in ssh-agent, or still in authorized_keysRun ssh-add -D and remove the line from authorized_keys on the server
Permission denied (publickey) after cleanupYou removed the key you were actually usingLog in through your host’s console or another key and restore authorized_keys.bak
Warning: Identity file not accessible~/.ssh/config still points at the deleted fileRemove or update the IdentityFile line
Remote host identification has changedThe server’s host key is different from the one in known_hostsConfirm the change is expected, then run ssh-keygen -R with the host name
GitHub still accepts pushesThe key is also a deploy key on a repositoryDelete it from the repository’s Deploy keys settings

Official documentation

Get the next guide

One email when there is something worth reading: new guides, errors going around, and hosting changes that affect your site.

No tracking pixels, and one click to leave.

Something out of date? Software changes. If a step no longer works, tell us and we will check it and update the page.