Removal of an SSH Key pair from computer

Can anyone tell me the command to remove an SSH KEY pair from my computer where the pair was generated?

Everything I am googling is saying to edit the known host's file, this makes sense except, I do not have any keys in that file.

I am learning how to generate key pairs, and how can you delete an SSH key pair from your computer.

If it helps Im on a Mac and a desktop with Ubuntu 22.04.

Any insight please, and thank you!

2 Replies

As far as I can tell, the keys on your local machine are files that you can delete like any other file. Though you'll also want to make sure you remove the keys from your agent.

I found the post online: terminal - How can I delete all SSH keys from my Mac - Stack Overflow

As it suggests, you can run a rm -rf /path/to/keys, though this will remove all keys in that directory. You can also do it on the specific private and public keys to remove from your computer. That would mean you'd want to remove both id_rsa and id_rsa.pub.

But, as that post also points out, you'll want to remove your keys from your agent besides just removing them from your computer. You can this for all keys by running ssh-add -D, or you can run ssh-add -d $key. I got that from the man page for ssh-add:

Instead of adding identities, removes identities from the agent. If ssh-add has been run without arguments, the keys for the default identities and their corresponding certificates will be removed. Otherwise, the argument list will be interpreted as a list of paths to public key files to specify keys and certificates to be removed from the agent. If no public key is found at a given path, ssh-add will append .pub and retry. If the argument list consists of “-” then ssh-add will read public keys to be removed fromstandard input.

The keys on the Mac are in ~/.ssh

You may have lots of keys in there so I suggest you just rename them first, create new ones, and then delete when you are sure you don't need them. (I never delete them). These are on my Mac:

ls -la ~/.ssh
-rw-------@  1 al  staff    218 Feb 27 19:57 config
-rw-------@  1 al  staff   1766 Jul  8  2014 github_rsa
-rw-r--r--@  1 al  staff    402 Jul  8  2014 github_rsa.pub
-rw-------@  1 al  staff    444 Nov 16  2021 id_ed25519
-rw-r--r--@  1 al  staff    120 Nov 16  2021 id_ed25519.pub
-rw-------@  1 al  staff   2635 Nov 15  2021 id_rsa
-rw-r--r--@  1 al  staff    592 Nov 15  2021 id_rsa.pub
-rw-------@  1 al  staff  47475 Mar 13 12:33 known_hosts
-rw-------@  1 al  staff  46343 Feb  6 10:52 known_hosts.old
drwxr-xr-x   6 al  staff    192 Nov 15  2021 xxxxconfig-not-used
-rw-------@  1 al  staff   1438 Jul 19  2014 xxxxid_rsa.ppk
-rwx------   1 al  staff   1679 Jul 19  2014 xxxxxid_rsa-alan.txt

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct