Check presence of a hostname under custom port in known_hosts
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.
Related videos on Youtube
deimos
Updated on September 18, 2022Comments
-
deimos over 1 year
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?
-
deimos about 8 yearsDid not work for me, unfortunately. But it does not matter, I found an alternative solution and updated the post.
-
Jakuje about 8 yearsJust 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.
-
deimos about 8 yearsHm... It did not work for me even with IP and even with 22.
-
Jakuje about 8 yearsIt depends on how your
known_hosts
look like "before hashing" (it tells you the known lineWarning: Permanently added '[localhost]:2222' (ECDSA) to the list of known hosts.
). But it might slightly differ by openssh version. -
deimos about 8 yearsOMG, I got it. Needed to use syntax
ssh-keygen -H -F "[myserver.com]:2102"
but I did not use the [brackets]. Thank you! -
Jakuje about 8 yearsGotcha :) Welcome to U&L. Let's proceed to the Tour to find out what to do with answers :)