As I understand it, a host sends its public host key to the client to confirm it's the host the client intended to connect to. On initial login the host's public host key is added to the client's ~./ssh/known_hosts file or can pre-populated using ssh-keyscan.

This is fine for future logins, but how can I be sure that when I first login the host key is being sent from the host I intended to connect to and not one impersonating that host?

Many thanks.

  • @F.Hauri - looks like exactly the same to me and at least 6 others. – Rory Alsop Apr 10 '19 at 11:39

In order to ensure 1st connection to ssh server, you have to verify his public host key.

For this, each new connection to an unknown host will prompt fingerprint and visual ascii graphic corresponding to host's public keys.

To make this verification, there are many different ways

  • Ask personely server's sysadmin for the fingerprint of the public host key (by phone, fax, meet or any trusted way)
  • Ask a confident friend who already done previous job
  • Go away but don't trust your connection until you could see physical proof you are on correct host (making a reverse connection from host, instant publication, etc).

From server side, when you install the SSH server, installation procedure implie host keys generation. At this point, all server keys are show with fingerprint and small ascii graphic you could check and compare.

This could be shown by the use of

ssh-keygen -lvf keyfile


mkdir /tmp/testsshkey && cd $_
ssh-keygen -b 1024 -f /tmp/testsshkey/key01
mv key01.pub other.pub
diff <(ssh-keygen -lvf key01) <(ssh-keygen -lvf other.pub )
< 1024 SHA256:7WuWSSOigkJ0UOPABdsrdkO3zYizPd3EfVpxb4XgeDk no comment (RSA)
> 1024 SHA256:7WuWSSOigkJ0UOPABdsrdkO3zYizPd3EfVpxb4XgeDk user@sondbox (RSA)

The only difference are in comments.

  • Perhaps I should've mentioned these are VPS instances I create. I ended-up generating private/public host key pairs locally then including those as #cloud-config data over a secure connection when creating the VPS instance. This way I know the host's public host key in advance. – tech-otaku Apr 11 '19 at 11:19
  • If you know your keys, you just need to compare output of ssh-keygen -lvf wich could be same on both private and public key. – F. Hauri Apr 12 '19 at 9:55

Some more information would be good to understand your issue. Normally the server would us a certificate for authentication purposes. A third trusted party, called a Certificate Authority (CA) signs the certificate with it's own private key. Afterwards the certificate is ready to be verified by the client and authenticates the owner. When you trust the CA, you also trust the server.

  • It seems you are talking about ssl certificates while OP is asking about SSH key. – Sefa Apr 10 '19 at 9:55

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