If you want to change computers or format your current one and are already using your public SSH key to authenticate to servers and some accounts, it would be a lot of work to generate a new key in your new environment and change it in all the places you are using it, right?! I will help you solve this problem today with less work! =)

Before you format your computer or change it, you should copy the folder called .ssh somewhere, it can be a pen-drive. Usually, this folder is located in your home directory and is hidden. Then you can press Ctrl + H and it will appear.

Inside the .ssh folder, you will have some files named: id_rsa (your private key), id_rsa.pub (your public key), and one or a few configuration files. If they are under different names, at the time you generated them you may have renamed them, but that will be fine. By default, I will use the names id_rsa and id_rsa.pub.

Did you copy it to the desired destination? Yes? Then let’s proceed!

Now in your new environment without the keys, you will follow these steps:

Copy your .ssh folder from where you saved it and paste it in the home of your new environment:

Want to do it from the terminal? Below is the command to copy and paste the folder.

cp file_path destination_path

Now with your .ssh folder in your new home, make sure all files are in the right folder to proceed. Now let’s change the permissions of the keys:

sudo chmod 600 ~/.ssh/id_rsa 
sudo chmod 600 ~/.ssh/id_rsa.pub

Ready? Now let’s start the ssh-agent in the background:

eval $(ssh-agent -s)

And finally, let’s make ssh-agent actually use your copied SSH Key:

ssh-add ~/.ssh/id_rsa

And that’s it! Now you can access your accounts easily!

Did you like the help? Like it and share it with people! ❤

Master's Student in Computer Science at UFRGS | Bachelor in Computer Science from UNIJUÍ | Developer (Open Source).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store