Dariux
Dariux

Reputation: 4253

Cannot find id_rsa.pub in the unix server. Can I regenerate it? Id_sra (private key) exists

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

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

I get error:

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

So there is no public key. So where is it? I tried to use command

sudo find / -name id_rsa.pub

but it only found one which I generated experimentally in my test directory. I tried sending the experimental from the test directory, but then it keeps infinitely asking paraphrase and does not send when I keep pasting.

So there is something wrong.

I could regenerate using

ssh-keygen -t rsa

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

and wants to overwrite private id_rsa key. I am afraid this might brake something.

So how do I get my public key file?

Upvotes: 11

Views: 54557

Answers (2)

inspirednz
inspirednz

Reputation: 5077

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

Here is the answer:

Regenerating a .pub key from the private key

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

The -y option is the command instructing ssh-keygen to output your public key.

This will re-generate the .pub part of the pair. Just as the OP pointed out, if you simply generate a new pair, and replace the old private key, you will lose access to whatever servers you have already provided your public key to. Sure, you can go through the process of providing a new public key to those servers, but why go through that hassle if it can be easily avoided?

Upvotes: 44

sa77
sa77

Reputation: 3603

RSA keys work on pairs. You can generate ssh private and public keys any number of times..it does not break anything. It simply replaces the old one with a newly generated keys. This only requires you to copy the newly generated public key id_rsa.pub to your remote machine's ~/.ssh/authorized_keys file in order for you to access secure shell using rsa keys.

So generate new rsa keys on your home's .ssh directory (your old keys are replaced by 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 passphrase empty while generating your new keys unless you want to enter passphrase every time you try to make a ssh connection.

NOTE: you need to append your public key to authorized_keys file in remote host's ~/.ssh directory if it already exists holding other client's public keys.

Upvotes: 2

Related Questions