8

I have multiple keys in my ~/.ssh/ directory, each with a separate project name, for projects that have multiple servers each. id_rsa_project1, id_rsa_project2

However, ssh won't search for them. If I run ssh -v user@projectserver I get output like the following:

...
debug1: Connection established.
...
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/me/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Trying private key: /home/me/.ssh/id_dsa
debug1: Trying private key: /home/me/.ssh/id_ecdsa
debug1: Trying private key: /home/me/.ssh/id_ed25519
debug1: Next authentication method: keyboard-interactive
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: password
user@projectserver password: 

This appears to be by design, as the ssh_config manpage says that, by default, the Identities searched for are ~/.ssh/id_dsa, ~/.ssh/id_ecdsa, ~/.ssh/id_ed25519 and ~/.ssh/id_rsa.

Of course, I can:

  • add the -i ~/.ssh/id_rsa_project1 switch to the command line each time, or
  • add IdentityFile ~/.ssh/id_rsa_project1 to a specification against the server in ~/.ssh/config, or
  • add IdentityFile ~/.ssh/id_rsa_project1 to /etc/ssh/ssh_config for each project.

...but all these seem too cumbersome for the regularity with which we change keys and key files.

I did try to add IdentityFile ~/.ssh/* to /etc/ssh/ssh_config but it appears to take it as a literal * rather than a wildcard.

How can I tell ssh to read and try all key files in ~/.ssh/ ?

1 Answer 1

10

The easiest way is to add them to ssh-agent:

Start agent:

eval `ssh-agent`

Add all keys in ~/.ssh:

ssh-add ~/.ssh/id_rsa_*

But note that it is not ideal way, since all the keys are tried on all the servers where you want are connecting. Proper configuration in ~/.ssh/config is advised solution.

4
  • Do I have to rerun this every time I change the keys in ~/.ssh/ ? Commented Mar 18, 2016 at 0:49
  • If you change the keys often, you are doing something wrong. How often do you change the keys from our car?
    – Jakuje
    Commented Mar 18, 2016 at 6:02
  • 2
    Every time I get a new hire car, actually. :-p Commented Mar 21, 2016 at 3:42
  • 1
    You really should use the .ssh/config method. Trying them all can lead to the ssh server dumping you for too many attempts, and you never know what hardware you may accidentally connect to. Commented Jul 5, 2016 at 23:44

You must log in to answer this question.

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