Can't find id_rsa.pub on unix server. Can I restore it? Id_sra (private key) exists

What I want to do is copy the key to another host.

ssh-copy-id -i ~/.ssh/id_rsa user@host

      

I get an error:

/ usr / bin / ssh-copy-id: ERROR: Could not open file ID '[homedir] .ssh / id_rsa.pub':

So there is no public key. So where is it? I tried using the command

sudo find / -name id_rsa.pub

      

but it only found the one I created experimentally in my test directory. I tried submitting the experimental file from the test directory, but then it retries infinitely and doesn't submit when I keep pasting.

So something is wrong.

I could regenerate with

ssh-keygen -t rsa

      

but then it tries to use the ~. / directory. ssh

and wants to overwrite the private key id_rsa. I am afraid that this may slow down something.

So how do I get the public key file?

+3


source to share


2 answers


RSA keys work in pairs. You can generate ssh private and public keys as many times as you want. It won't break anything. It simply replaces the old key. This only requires copying the newly generated public key id_rsa.pub to the remote machine's ~ / .ssh / authorized_keys file so that you can access the secure shell using rsa keys.

So, create new rsa keys in your home .ssh directory (your old keys are replaced with new ones) and copy to the remote host's .ssh directory

cd /home/<your_username>/.ssh
ssh-keygen -t rsa
scp ~/.ssh/id_rsa.pub remote_username@host:~/.ssh/authorized_keys

      

then



ssh remote_username@host

      

Keep the passphrase blank when generating new keys, unless you want to enter the passphrase every time you try to make an ssh connection.

NOTE. You need to add your public key to the authorized_keys file in the host's remote ~ / .ssh directory if it already exists with the public keys.

+2


source


Just in case someone comes here looking for an answer to the OP's question ... and directly answer that question (namely, how can you generate a .pub key in a situation where it is missing and you only have a private key) ...

Here's the answer:

Regenerating a .pub key from a private key



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

      

The parameter -y

is the teling command ssh-keygen

to output your public key.

This will re-generate the .pub part of the pair. As the OP pointed out, if you just create a new pair and replace the old private key, you will lose access to those servers where you have already provided your public key. Sure, you can go through the process of providing a new public key to these servers, but why would you solve this problem when it can be easily avoided?

+12


source







All Articles