Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to have multiple SSH key in a single client, and let ssh choose the right one automatically?

share|improve this question
up vote 13 down vote accepted

You can have different private keys in different files and specify all of them in ~/.ssh/config using separate IdentityFile values (or using -i option while running ssh). They would be tried in sequence (checkout man 5 ssh_config).

If you are using ssh-agent though, you might have to tell the agent about the multiple keys you have using ssh-add.

share|improve this answer
Note that if one account has multiple valid keys (e.g. because you use authorized_keys to run specific commands instead of a shell), you may have to use the IdentitiesOnly yes option to make sure ssh-agent doesn't use the wrong one. See also unix.stackexchange.com/q/52092/863 – Tobias Kienzler Oct 18 '12 at 6:13


-i identity_file

Selects a file from which the identity (private key) for public key authentication is read. The default is ~/.ssh/identity for protocol version 1, and ~/.ssh/id_dsa, ~/.ssh/id_ecdsa and ~/.ssh/id_rsa for protocol version 2. Identity files may also be specified on a per-host basis in the configuration file. It is possible to have multiple -i options (and multiple identities specified in configuration files). ssh will also try to load certificate information from the filename obtained by appending -cert.pub to identity filenames.

Just add -i for each identity, or use several IdentityFile lines in you .ssh/config.

share|improve this answer

Your Answer


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.