Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add public key to known_hosts file

Tags:

linux

ssh

key

I am trying to copy a public key from Server A over to the known_hosts file in Server B. They are both linux servers. Initially I thought about opening the public key file and copying its contents over to the known_hosts file but I suspect that is not the correct method. Does anyone know what the right way to do so is?

My public key is in the format ssh-rsa AADGD...

Can someone help?

Thanks!

like image 622
Trung Tran Avatar asked Jan 20 '16 17:01

Trung Tran


People also ask

How do I add a public key?

On your computer, in the PuTTYgen utility, copy the contents of the public key (displayed in the area under "Key") onto your Clipboard. Then, on the remote system, use your favorite text editor to paste it onto a new line in your ~/. ssh/authorized_keys file, and then save and close the file.

What key is stored in known_hosts?

The known_hosts file contains the host public key for all known hosts. The use of this file is optional, but, if used then it is prepared by the system administrator. It automatically maintains the per-user file in the system.

What is the difference between authorized_keys and known_hosts file for ssh?

authorized_keys is a file that allows you to add ssh public keys of users that should be allowed to log into your server (the server in which the authorized_keys file lives) using key based auth. known_hosts is a file that contains a list of keys from... known hosts that you have logged into.


1 Answers

I answered almost similar answer on SuperUser few days ago. The important parts:

  • The format differs
  • There are different host keys (types) on each server (make sure you paste the one that is actually used)
  • There is ssh-keyscan which can create the format for you

Otherwise just prefix your key with server IP address (you can add also hostname, after comma), remove the comment from end of the line and you are fine. Format then look like this:

11.22.33.44 ssh-rsa AADGD... 

And one more note, if you use HashKnownHosts yes (Debian and Ubuntu does), you need to re-hash your known_hosts such as:

ssh-keygen -Hf ~/.ssh/known_hosts 
like image 86
Jakuje Avatar answered Sep 21 '22 05:09

Jakuje