Score:0

How to remove ssh keys?

ke flag

Learning how to use public keys with ssh server

following this tutorial by DigitalOcean

ssh server : kali VM

ssh client : Ubuntu VM

Using an Ubuntu VM I generated a couple of ssh keys but I only need one to use since the others are somewhat duplicates but have a different password

running this command ssh-copy-id [email protected]

returns

Number of key(s) added: 4

Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

How do you remove the keys to be added and stick to the only key generated when running ssh-keygen

I tried to delete the keys by running this command sudo rm -rf /home/kali/.ssh

but every time I try to copy the newly generated key I get 4 keys on the kali VM

Score:0
us flag

It seems to me you are copying 4 keys to your server.

This is taken from the online documentation located at https://www.ssh.com/academy/ssh/copy-id

-i Specifies the identity file that is to be copied (default is ~/.ssh/id_rsa). If this option is not provided, this adds all keys listed by ssh-add -L. Note: it can be multiple keys and adding extra authorized keys can easily happen accidentally! If ssh-add -L returns no keys, then the most recently modified key matching ~/.ssh/id*.pub, excluding those matching ~/.ssh/*-cert.pub, will be used.

Update from the comments:

If you which to remove a certain key just use:

$ rm .ssh/id_target_key*

One of the backup options can be:

$ mv .ssh/id_target_key id_target_key.bak

Remember that there are always 2 keys. One private and one public. The latter has the extension .pub

Where to locate these keyfiles? On the server open /etc/ssh/sshd_config and look for

AuthorizedKeysFile .ssh/authorized_keys

This file can be edited in order to remove your key manually, there might be other users keys installed.

On the client open /etc/ssh/ssh_config and look for an alternative path from there. (Mine is all commented out so ~/.ssh is the default location) ~/ is a relative path which changes per user. Executing the ssh command set with sudo will result in a different path: /root.

Sec Team avatar
ke flag
I want to remove the 4 keys and create a new one and copy it without the `-i` options if possible
Whois_me avatar
us flag
Do you have multiple keys located in your Ubuntu VM? ( $ ls .ssh/ )
Sec Team avatar
ke flag
yes and i wish to remove them all
Sec Team avatar
ke flag
typing `cd .ssh/` shows only one key but when i create a new key i the number of ssh keys increases by one up to 6 now is there any other folder ?
Whois_me avatar
us flag
Do you type sudo perhaps? Then it gets added in the root home folder. Also delete the keys at the server side.
Sec Team avatar
ke flag
after rebooting the client i was left with one key for some reason it didn't refresh or didn't realize that i deleted the keys
hr flag
@SecTeam AFAIK none of the above removes the *public keys* from the remote host's `authorized_keys` file - which is what you really should care about from a security POV.
Whois_me avatar
us flag
Good point, although Sec Team removed the whole folder on the server side. Which is, off course, a bit like shooting with a cannon to a mosquito.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.