There are multiple option to remove a known ssh host from the known_hosts
file.
1. Use the ssh-keygen command
This is the easiest and safest method to remove the known ssh host form known_hosts
file.
Run the following command by replacing the <hostname_or_ip>
with the actual hostname or IP address of the ssh host -
ssh-keygen -R <hostname_or_ip>
For example:
ssh-keygen -R 172.16.10.10
or
ssh-keygen -R developerinsider.co
This will remove the host's entry from the ~/.ssh/known_hosts
file and also generate ~/.ssh/known_hosts.old
file with all the previous changes in case you want to revert the changes.
2. Manually edit the known_hosts file
First, create a backup of your known_hosts
file in case you need to revert any changes. Use the following command to copy the current known_hosts
file to known_hosts.bak
-
sudo cp ~/.ssh/known_hosts ~/.ssh/known_hosts.bak
Now, open the known_hosts
file in your favourite editior
nano ~/.ssh/known_hosts
Search for the host by its hostname or IP address and delete the corresponding line. After that, save your changes.
3. Remove all known hosts
To remove all known hosts, you can delete the known_hosts
file. Run the following command to delete known_hosts
file.
sudo rm ~/.ssh/known_hosts
When you access a new host via SSH, this file will be recreated automatically.