How Do I Delete an SSH Key Quickly?

If you build a lot of systems and use SSH you'll undoubtedly run into this problem a lot:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:kP6qfBXh2iuvNL69JAg6eTCRwN4Dsf1zZRyheFAqdYQ.
Please contact your system administrator.
Add correct host key in $HOME/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in $HOME/.ssh/known_hosts:34
ECDSA host key for [192.168.11.26]:2222 has changed and you have requested strict checking.
Host key verification failed.

```

This happens when you've exchanged keys to avoid entering a password to access a trusted system via **SSH**. See my previous article about this where I define the **setuptrust** command.

Fixing this problem is a real pain. You can use the **ssh-keygen** command with a -R parameter. But I can't remember the command. It's simpler to just delete the line using **sed**. To this end, the **xsshkey** command is handy:

    function xsshkey()
    {
      sed -i -e "$1d" ~/.ssh/known_hosts
    }

Let's delete the offending key at location 34:

    $ xsshkey 34
How Do I Delete an SSH Key Quickly?
Share this