Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change a SSH private Key to an existing one

I had to make a factory reset to my Mac and now I'm configuring again all the development stuff I need like Ruby/Rails etc...

Next step is to configure git and my question is about my ssh key:

From my previous configuration I saved my private ssh key in a text file (I don't know if it was a good idea in terms of security but that's not the point), just copied it from my terminal and pasted it on a text file.

I would like to use that private ssh key again instead of creating a new public and private one. How can I do that? Do I have to create a new one and then replace it somehow for the one I saved time ago? if so, how?

If you can be very specific in your answer it would be very appreciated since this topic is not familiar to me.

Thanks a lot in advance!

like image 559
Juanito Avatar asked Jan 20 '26 22:01

Juanito


2 Answers

Warning!!!

Reusing private key is almost always a bad idea. In your specific case it may be reasonable, but for other users, who may see this answer, regenerating a new pair of keys will usually be a better option

Keys location

By default, ssh keys are stored under ~/.ssh (hidden by default). Create this directory, if needed. Then you can place your saved private key there, naming it something like id_rsa. Since you mention, that you're not very common with this subject, I suppose you ran simple ssh-keygen to get this key without extra-options and got a key named like this by default.

After this step change copied file owner to be your user (can be achieved with chown <your username> <private key file>) and change permissions to 600 (with chmod 600 <private key file>)

As @DeepakMahakale mentioned, after this operation, you may need to execute ssh-add -K ~/.ssh/id_rsa to add your key to the list of keys known by your system.

Regenerating your public key

It's possible, that you'll need to get public key once again from your private key. To do so, just run ssh-keygen, providing existing private key, like this:

ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub

Where ~/.ssh/id_rsa is a path to private key and ~/.ssh/id_rsa.pub is a path to public key, that will be generated

like image 101
Евгений Крамаров Avatar answered Jan 23 '26 12:01

Евгений Крамаров


You can just copy the existing keys in your .ssh folder.

If you do not have the folder create one with

mkdir ~/.ssh

Copy the files (these maybe id_rsa so check the names before this)

mv path_to_your_existing/id_ed25519 ~/.ssh/id_ed25519
mv path_to_your_existing/id_ed25519.pub ~/.ssh/id_ed25519.pub

Note:

you may need to do this

ssh-add -K ~/.ssh/<private_key_file>
like image 33
Deepak Mahakale Avatar answered Jan 23 '26 12:01

Deepak Mahakale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!