if your sshd runs on a custom port, add '-p portNumber' to the ssh-keyscan command)
ssh-keyscan writes on stderr, not stdout(!), hence the bash redirection '2>&1' (that can vary depending on your shell)
That is the line I added to my ~/.ssh/known_hosts file in order to authorize ssh requests from localhost for my tests (mainly for gitolite, which uses ssh).
ssh-keyscan provides the full public key(s) of the SSH server
the output of ssh-keygen is nearly identical to the format of the public key files.
Just remove the 1st column (IP address or hostname) and save that or pipe it to ssh-keygen -l which presents the fingerprint.
Daniel adds:
Show fingerprints of all server public keys stored in ~/.ssh/know_hosts:
cut -d' ' -f2- ~/.ssh/known_hosts | while read line; do echo "$line" | ssh-keygen -lf-; done
What to do to check the authenticity of a host/server
First, 1. is to be done locally on the server (the one you want to connect TO via ssh ): it will give you a first ascii-art. Print it or take a picture.
Second, 2. is to be done at the first SSH connexion; it will display a second ascii-art. If the ascii-art is the same, then you can answer yes to the "do I trust?" question (i.e. Are you sure you want to continue connecting (yes/no)).
Example
Server side
$ ssh-keygen -l -v -f /etc/ssh/ssh_host_ecdsa_key.pub
256 2e:a6:b3:27:14:12:0b:79:df:9a:7f:bd:4d:b1:e0:b6 (ECDSA)
+--[ECDSA 256]---+
| . |
|o o |
| o + . |
| o o . |
| . + S . . |
| + . . . o |
| . .o ..o o |
| ooo....+ |
| o= . E.. |
+-----------------+
Client side
$ ssh -o visualhostkey=yes -o FingerprintHash=md5 192.168.12.211
The authenticity of host '192.168.12.211 (192.168.12.211)' can't be established.
ECDSA key fingerprint is MD5:2e:a6:b3:27:14:12:0b:79:df:9a:7f:bd:4d:b1:e0:b6.
+---[ECDSA 256]---+
| . |
|o o |
| o + . |
| o o . |
| . + S . . |
| + . . . o |
| . .o ..o o |
| ooo....+ |
| o= . E.. |
+------[MD5]------+
Are you sure you want to continue connecting (yes/no)?
Some more explanation
The first command will display the ascii-art corresponding to the fingerprint of the file you give as input. The file you give as input is the public host key of the server. When a client connect (not only for the first time), the server will sent its public host key. This public host key will be searched in ~/.ssh/known_hosts. If the public key is in the file, then it's ok: the host (server) is known, so we move on to the next step to authentificate the user (user auth is not described in this post). If the public key is not in the file, then the client will compute the fingerprint of this public host key with a hash algorithm (a different hash algo will give a different fingerprint). This fingerprint previously calculated is displayed (along with the ascii-art if corresponding option provided) and you will have to answer yes or no depending on you recognising this fingerprint or no (this fingerprint is the image/hash of the public host key of the server). If you say yes, then the bublic key of the server (not its fingerprint) will be added to the file ~/.ssh/known_hosts.
We can notice that ~/.ssh/known_hosts is under you home (~) directory, because you trust this host (server), but a different user may not trust the same as you. Also, the host public key of the server is not user-dependent, so it is stored in /etc/ssh/.
The second command will display the fingerprint and the ascii-art of the public key received from the host_server_to_connect (according to the hash algo given in options). It is the same as doing only ssh, but with more visual options, so the connection will continue the same way as a normal ssh connexion.