5

I need to add a check if the hostname is already present in the known_hosts.

Normally I would do something like that:

ssh-keygen -H -F hostname

However, that does not seem to work for me in this particular case. I connect to the host using port 2202, like that:

ssh user@myhost -p 2102

I asked to add the hostname to known_hosts, I say yes. After that I run "ssh-keygen -H -F myhost" but receive empty result. To make the matter worse, the known_hosts is hashed.

That works perfectly with port 22, so if I login to "ssh user@myotherhost", save the known host and run "ssh-keygen -H -F myotherhost" I receive the exact line from the file.

So, how can I adjust the command to work with port 2102?

5

You can use this format: [hostname]:2121, as it is stored in the known_hosts file (note, you need to use the square brackets!):

ssh-keygen -H -F "[hostname]:2121"

Proof of concept (transcript of my minimal test case):

$ echo "[hostname]:2121 ssh-rsa AAA...==" > known_hosts
$ ssh-keygen -Hf known_hosts 
known_hosts updated.
Original contents retained as known_hosts.old
WARNING: known_hosts.old contains unhashed entries
Delete this file to ensure privacy of hostnames
$ ssh-keygen -H -F "[hostname]:2121" -f known_hosts
|1|R21497dX9jN052A92GSoVFbuTPM=|lRtIr6O564EaFG0SsIulNAWpcrM= ssh-rsa AAA...==

You might need to use IP address instead of hostname, but it should generally work.

  • Did not work for me, unfortunately. But it does not matter, I found an alternative solution and updated the post. – deimos Apr 5 '16 at 17:38
  • Just verified even with hashed known hosts and it still works. You might need to try with IP address instead of hostname, but it should work as a pie. – Jakuje Apr 5 '16 at 17:44
  • Hm... It did not work for me even with IP and even with 22. – deimos Apr 5 '16 at 17:51
  • It depends on how your known_hosts look like "before hashing" (it tells you the known line Warning: Permanently added '[localhost]:2222' (ECDSA) to the list of known hosts.). But it might slightly differ by openssh version. – Jakuje Apr 5 '16 at 17:54
  • OMG, I got it. Needed to use syntax ssh-keygen -H -F "[myserver.com]:2102" but I did not use the [brackets]. Thank you! – deimos Apr 5 '16 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.