Removing SSH keys from authorized_keys file

If you have a server that receives connections from various computers you may have setup key based authorisation. This is a good move but occasionally you may be in a position where you want to remove a key so as to revoke access from one of the computers.

To do this you simply need to remove the public key from the authorized_keys file on the server. You can do this in a text editor, but it can get messy if there are dozens of entries in the file and you just want to remove one.

This is where SED is your friend. With this simple line you can delete the entry for the hostname you want to revoke, and also keep a backup of the file in case you make a mistake:

sed ‘/hostname/ D’ -i.tmp ~/.ssh/authorized_keys

“hostname” is the hostname of the computer connecting to the server. If you don’t have the exact hostname you can always use part of the public key in here, it will work the same way.

The command keeps a backup of the authorized_keys file as authorized_keys.tmp and saves the original file without the public key of the hostname you specified.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *