Rotating your SSH keys

Changing your SSH keys from time to time is smart. Especially in the case you lose it or something happens to your laptop/mobile phone/etc, you want from time to time to have a few keys in rotation

For most intents and purposes:

  1. First you SSH in.
    ssh user@hostname
  2. Once you're in
    • Copy paste it via your favourite editor
      $FAV_EDITOR ~/.ssh/authorized_keys
    • You might want to append it from a variable
      $KEY >> ~/.ssh/authorized_keys
    • Many other ways to add your keys, but the file it ends up in is the same .ssh/authorized_keys.

      But who wants to do this manually and copy paste things around, when you can specify your identity with a very nice script that's seriously underused:

      ssh-copy-id -i ~/.ssh/newkey user@hostname

This will automatically SSH into the host and put(append) the public key identity in your authorized_keys file.


Don't forget to remove your older and no longer used or compromised SSH keys from the very same file.

That's it!
Happy SSHing.

Leave a Reply

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