Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

I'm always running ssh with the -i parameter and it's a hassle to always type in the correct key for whatever host I'm connecting to.

Is there a config file or something (on Mac) to define which private key to use when connecting to a particular host?

share|improve this question
up vote 8 down vote accepted

Yes, you want to create a ~/.ssh/config file. That lets you define a shortcut name for a host, the username you want to connect as, and which key to use. Here's part of mine, with hostnames obfuscated:

Host tabs
     User     me
     IdentityFile       ~/.ssh/new_rsa

     User       cap
     IdentityFile       ~/.ssh/git_rsa

Host project-staging
     User     me
     IdentityFile       ~/.ssh/new_rsa

With this I can say, ssh tabs and get connected to host as user me, with key new_rsa, as though I'd used ssh -i ~/.ssh/new_rsa.

share|improve this answer
Also a good way to go if you don't want to use ssh-agent for whatever reason. – voretaq7 Apr 22 '11 at 19:28
I didn't know that was possible. +1 – Safado Apr 22 '11 at 19:48

SSH clients will typically use ~/.ssh/identity (ssh v1) or one of~/.ssh/id_rsa or ~/.ssh/id_dsa (v2) as the default private key. You can change this in ~/.ssh/config (the IdentityFile parameter - the -i option to SSH actually overrides this. See man ssh_config for details).

If you have multiple private keys to deal with using ssh-agent is probably a better choice.
See man ssh-agent for more details.

share|improve this answer
+1 for describing the defaults and citing the man page. – pjmorse Apr 22 '11 at 19:42

If you use different keys for different servers I don't think there is.

If you use a single key for all servers, just save it as ~/.ssh/id_rsa, it will be used automatically.

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.