1

I am trying to understand the relationship between fingerprint of SSH server's public key and known_hosts file.

There are two SSH servers with same public key. I, the client has the private key. I login to the first SSH server,since it is asking me for the first time, I have say yes to "do you trust the fingerprint...".

The server sends the public key and the hash of the public key? or the hash is calculated by the client using the public key received by the server?

Is the fingerprint itself added to known_hosts file? I tried to grep the fingerprint that was provided in the known_hosts file but it produced no output. So what exactly is being added to known_hosts file when trust the fingerprint?

Secondly, when I swap out the first SSH server with a second one, the public key is the same but when I ssh into it, I get the fingerprint has changed? How is that possible? Which public key is being used to calculate the fingerprint? The public key of key pair for which I have the corresponding private key? If that's the case, they server's public key has not changed so why different fingerprint? or is it a public key from /etc/ssh directory? If that's the case, I don't have the private key for that public key. How would the session, continue then?

What things go into calculating the fingerprint?

1 Answer 1

2

In SSH, all servers have at least one key pair (that is, a matching public and private key). In addition, it's possible for the client to authenticate with a key pair, which is completely and totally independent.

When you start an SSH connection, algorithms are negotiated, a key exchange occurs, and the two sides produce what's called the exchange hash. The server signs this with its private key (using the negotiated algorithm), and sends the signature with its public key.

The value used in the known_hosts value is this public key, and OpenSSH saves it there once you have confirmed by verifying the fingerprint that it is the correct key (which you should do, since not doing so allows a MITM attack). If the server presents a different key, you get the error that you saw and the connection fails. Note that the fingerprint is not stored in the file, only the key, but you can save the key (that is, the portion of the line starting with ssh- or ecdsa-) into a separate file and then run ssh-keygen -l -f FILE to find the fingerprint.

All of this occurs before the client even authenticates. The SSH key that you use to log into any server is independent from the host key, and whether a key is authorized for a given user is usually configured in the ~/.authorized_keys file, which contains the public keys of authorized clients. The client will not attempt to authenticate if the server's host key is incorrect.

Note that the reason the server has a different host key is that it's a different server, and most OSes generate a fresh host key on installation of the SSH server, since this is the best approach for security. Those keys are typically stored in /etc/ssh/ssh_host_* (the .pub files are the public keys, and the others are private).

In an OpenSSH-format public key, the fingerprint is the SHA-256 base64 hash of the raw key bytes, which are stored base64-encoded in the second portion of the key. Thus, you can also compute the fingerprint of a public key like so:

cut -d' ' -f2 FILE | base64 --decode | openssl dgst -sha256 -binary | base64

Some older clients used MD5 for fingerprints, but MD5 is insecure and obsolete and should not be used for any purpose anymore.

4
  • So the fingerprint is a hash of the server's ssh public key in /etc/ssh/ssh_host_* and has no relationship with the client's public key stored in server's authorized_keys file? Who generates the fingerprint? The server or the client? Jun 9 at 5:15
  • The client calculates (not generates) the fingerprint from the server's public key. It wouldn't make sense to delegate the calculation to the server, because the server isn't trusted at this point and could present a wrong fingerprint that doesn't actually match the public key. So the client has to calculate the fingerprint itself (which is a very simple calculation and really just a shorthand to make the check easier for humans). The fingerprint of the server's public key has nothing to do with any client keys.
    – Ja1024
    Jun 9 at 5:51
  • Which key of the server does the client calculate the fingerprint ? For example in AWS EC2, a key pair is created and the client has the private key. The corresponding public is in authorized_hosts file. Is the public key from authorized_keys file is sent to the client and it calculates the fingerprint or the fingerprint is calculated from the Server’s public key in /etc/ssh/host*.pub. If yes, are both public keys sent to the client ? Jun 9 at 12:53
  • The fingerprint of a key is always over the public key. The fingerprint printed during the prompt you see when connecting from the first time is what the client computes from the public host key that the server has sent during the connection, which the server usually gets from /etc/ssh/host* (either directly from the public portion, or by deriving it from the private portion). The client's fingerprint is not usually printed by OpenSSH unless you ask for it directly with ssh-keygen.
    – bk2204
    Jun 9 at 20:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .