30

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?

Leopd
  • 1,757
  • 4
  • 24
  • 30

3 Answers3

36

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
     HostName tabs.com
     User     me
     IdentityFile       ~/.ssh/new_rsa

Host scm.company.com
     User       cap
     IdentityFile       ~/.ssh/git_rsa

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

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

pjmorse
  • 1,550
  • 1
  • 17
  • 34
2

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.

voretaq7
  • 79,879
  • 17
  • 130
  • 214
0

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.

Hubert Kario
  • 6,361
  • 6
  • 36
  • 65