In ssh communication between server and client, client gets authenticated by its private key. I want to know that how does the server knows that which public key belong to client, if the server has more than 1 public key in its authorized_keys file.


Client sends its public key ID and server checks if this key is in list of authorized_keys.

  • The client begins by sending an ID for the key pair it would like to authenticate with to the server.

  • The server check's the authorized_keys file of the account that the client is attempting to log into for the key ID.

  • If a public key with matching ID is found in the file, the server generates a random number and uses the public key to encrypt the number.

  • The server sends the client this encrypted message. If the client actually has the associated private key, it will be able to decrypt the message using that key, revealing the original number.


  • Thanks for the explanation. Can you please confirm if the key ID you are talking about here is the fingerprint we calculate using ssh-keygen -lf public_key or is it something else. how can i see what is the keyID for my public/private key pair. – Rajnish Kumar Soni Jul 3 '17 at 6:01
  • The first 2.5 bullets are almost right; the rest is wrong even for RSA, and is ludicrously impossible for DSA, ECDSA, and ED25519. – dave_thompson_085 Jul 3 '17 at 10:03

This is very nicely explained in the RFC4252 including the messages that are send back and forth by client and server:

  • The first message SSH_MSG_USERAUTH_REQUEST contains the public key blob which is used to compare with the key stored in authorized_keys
  • Server answers with SSH_MSG_USERAUTH_PK_OK or failure
  • Second SSH_MSG_USERAUTH_REQUEST contains again the public key (assuming it is the blob again) together with the signature over known data (including the public key).

The first two points are not mandatory and you can do only the last one (but probing which key to use saves a computation power).

  • 1
    Probing may also avoid forcing the user to enter the passphrase for a privatekey that won't be accepted (although if you keep all the keys in ssh-agent or keyring or such it doesn't matter). – dave_thompson_085 Jul 3 '17 at 10:01

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.