11

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?

Dariux
  • 3,953
  • 9
  • 43
  • 69

2 Answers2

43

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?

inspirednz
  • 4,807
  • 3
  • 22
  • 30
  • 2
    This must have been an accepted answer. Public key may have already been distributed to number of servers and repos. – aiodintsov Dec 20 '18 at 18:06
  • 1
    I suspect the OP abandoned ship on this one, because yes, AFAIK, this should be the accepted answer...based on the votes its had, and for the reason you pointed out. Have a great day. – inspirednz Dec 20 '18 at 22:34
2

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.

sa77
  • 3,563
  • 3
  • 24
  • 37
  • 1
    But then its kind of broken already. If I don't know remote machines or forget to copy newly generated key, then its broken - remote machine cannot connect to current server, right? Current machine might be administrated by several admins, so its easily possible such situation. Even if I use same passphrase, it will generate different pair of files? – Dariux Oct 17 '14 at 09:39
  • Or what if I make sub folder in .ssh for my new keys? Then they work both old and new and so are not breaking remote machines which want to connect with old key? – Dariux Oct 17 '14 at 09:42
  • you need a pair (private and public) of keys generated at the same time in order for it to work. you have a private key (id_rsa) but not it's corresponding public pair (id_rsa.pub). so it's better to generate a new pair replacing the old one for the machine you're using now..and try making a ssh username_at_server@server_host_ip connection to the server you're trying to make the connection. if the server configuration permits..it prompts you to add your key to it's known_hosts – sa77 Oct 17 '14 at 09:52
  • 1
    you can create as many combination of ssh-keys..but it's recommended to use one for a user. whenever you hit ssh-keygen command..it generates new pair even for same passphrase – sa77 Oct 17 '14 at 09:56
  • Whilst generating an entirely new keypair is one way to go about solving the issue of having the private key but no public key, I think it creates the unnecessary burden of having to send a new .pub key to all servers you interact with over SSH. So, in my opinion, the above answer does not directly address what the Darius.V is asking. He has specifically asked, "So how do I get my public key file?" and there is good reason to want to do just that (i.e. to re-generate the public key file). I have provided the answer to his question below. – inspirednz Aug 12 '16 at 18:52