Search code examples
linuxsshkey

Add public key to known_hosts file


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!


Solution

  • 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