0

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!

Juanito
  • 9
  • 2

3 Answers3

3

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

  • This won't help as the public keys is associated with lots of other places. For example github accounts, ssh access with some servers. – Deepak Mahakale Oct 06 '21 at 14:48
  • @DeepakMahakale Correct me, if I'm wrong, but to connect to a remote server, you don't need a public key. Having only private will suffice. Hence, Copying saved key under `~/.ssh/` would be enough to connect to GitLab, etc. I added section about regenerating public key only in case author needs that in the future. E.g. to add this new key to a new ssh server. – Евгений Крамаров Oct 06 '21 at 14:53
  • Does this create the same public key? I remember in past I have to give my new public key to devops to grant me the ssh access to our private servers so they can add it in `authorized_keys` – Deepak Mahakale Oct 06 '21 at 14:58
  • It may not generate exactly same key, but it won't invalidate previous ones, author already used on remote servers. This way, if author has GitLab account with previous pubkey, he/she will still be able to use it. But if author decides to create, say, GitHub account he/she will simply regenerate pubkey and use it there – Евгений Крамаров Oct 06 '21 at 15:04
2

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>
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
0

Do you have a location of: C:\Users\User\ssh\authorized_keys To place your saved key into?

The only I have encountered personally, I had to open this file "Authorized keys" with WordPad++ then adding the key in.

If this doesn't work see here: Click here

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
Kieran
  • 1
  • 3