Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I know there are several questions about ssh keys and how to configure it. But I found none to answer my question.

So here it is: I have several ssh keys in my .ssh directory. For some remotes different keys.

Now I have neither configured in .ssh/config which key to use for which host, nor have I added them to an agent manually, what some questions here already mentioned. I just created the keys with ssh-keygen and it works.

Now I would like to know who is responsible for that and which keys are tried automatically? I have something in my mind that says about five keys are automatically checked for compatibility - but I cannot find where I found this information.

Does ssh-keygen automatically add those keys to an agent? Or does ssh search for any matching key by default?

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

The ssh client will check all your keys until it finds one that matches.

This is how it works (this is very simplified, before this a quite complex dance has been made to encrypt all of this):

  • The server creates an auth token.
  • The token is encrypted using your public key on the server.
  • The server send the encrypted token to the client.
  • The client tries to decrypt the token, using all known private keys.
  • If it is successful it will send the decrypted token back to the server.
  • If the token matchs the server will let the client in.

What files are keys depends on the client.

For the Openssh client (Ubuntu default client), according to its man page, the files that are supposed to be private keys are ./sshid_rsa, .ssh/id_dsa, .ssh/id_ecdsa, plus those given after the -i flag (it supports multiple files) and those declared in the config file.

You can give it the -v option to make it print a line when it tries to use any file as a key. This is an example from a non-key login:

$ ssh -v www.hostremoved.com
OpenSSH_5.9p1 Debian-5ubuntu1, OpenSSL 1.0.1 14 Mar 2012
debug1: Reading configuration data /etc/ssh/ssh_config
<...>
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/javier/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/javier/.ssh/id_dsa
debug1: Trying private key: /home/javier/.ssh/id_ecdsa
debug1: Next authentication method: password
<...>

As you can see, it prints all the keys it tries, it fails all. You can use this in your system to discover what files is ssh using in your own system.

Below you can see the output if some existing key is found and tried

debug1: Authentications that can continue: publickey,password
debug1: Offering RSA public key: user@xyz

user@xyz is the information appended to the public key.


If you're wondering how your ssh client finds your private keys, it's not magic. Under Gnome (xfce and KDE also) there is a special ssh-agent that automatically adds keys in .ssh directory that have a correspondending public key with the ending .pub.

If you not have such a comfortable ssh agent, you'll have to add your private keys with ssh-add key.

share|improve this answer
    
Okay, thanks for that more detailed progress. But same question as to jacakweirdy: Which files are considered to be keys? All files in the .ssh directory? –  Bertram Nudelbach Jun 19 '12 at 16:51
1  
Thanks for the edit, I just added a bit more of output .. I tried -vvv to get a lot of output but the lines Offering ... looked all the same - because all public keys had the same name and host the key belongs to. I'll just had to edit these and could read the output. –  Bertram Nudelbach Jun 19 '12 at 19:35
    
Just added some research result (also mentioned in the meantime by John S Gruber): The ssh agent ist responsible for knowing which private keys you have. –  Bertram Nudelbach Jun 20 '12 at 8:04
add comment

Assuming that you have copied the public key to the authorized_keys file on the remote machine, the ssh client will check whichever private key on the local machine matches the public key on the remote machine.

share|improve this answer
    
All files in .ssh directory? Or which files are considered to be keys? –  Bertram Nudelbach Jun 19 '12 at 16:43
2  
Assuming you are using gnome-kerring-daemo (most Ubuntu users are) all of the private keys in the ~/ssh/ directory are picked up by the agent, and suggested by the openssh client to the server until a match is found (from my rxperiments). ssh-add -l will list them. Use the -v option on ssh to watch this happen. –  John S Gruber Jun 19 '12 at 21:02
    
@JohnSGruber hit the nail on the head there, assuming the keys are added ssh will try all of them :) –  jackweirdy Jun 19 '12 at 22:20
add comment

Copied/pasted from ssh_config(5) in openssh-client 1:5.3p1-3ubuntu7:

IdentityFile

Specifies a file from which the user's RSA or DSA authentication identity is read. The default is ~/.ssh/identity for protocol version 1, and ~/.ssh/id_rsa and ~/.ssh/id_dsa for protocol version 2. Additionally, any identities represented by the authentication agent will be used for authentication.

The file name may use the tilde syntax to refer to a user's home directory or one of the following escape characters: ‘%d’ (local user's home directory), ‘%u’ (local user name), ‘%l’ (local host name), ‘%h’ (remote host name) or ‘%r’ (remote user name).

It is possible to have multiple identity files specified in configuration files; all these identities will be tried in sequence.

The bottom line is the it depends on the protocol version. For version 2 (most likely these days), id_rsa and id_dsa are tried in that order according to the above doc. Newer versions may include other type of keys by default like id_ecdsa. Check that man page in your version for the details.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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