Can I have multiple ssh keys in my .ssh folder?

32

23

Can I create multiple ssh keys, and rename them to user friendly names so I know which key is for which website etc.

Is this safe?

e.g:

github_id_rsa
github_id_rsa.pub
..
...

How will it know which key to check against when connecting?

On my computer now, when I look at known_hosts, they all seem to have the same key after the name of the host??

user27449

Posted 2011-05-23T20:54:35.307

Reputation: 5 104

1

Cross-site duplicate: Best way to use multiple ssh private keys on one client

– slhck – 2011-05-23T21:07:05.823

Also, the known host keys are definitely not the same, just look at the ends of the lines for each host. – slhck – 2011-05-23T21:07:36.883

Answers

15

Yes you can have different ssh keys. There's very good documentation on the GitHub Help site at Help.GitHub - Multiple SSH Keys. Essentially you will be using ssh-add to add the extra keys so that the agent can utilize them. Then you set up the ssh hosts config so that any ssh connections to different domains will be looked up here and the appropriate key will be used. good luck!

ArtemT

Posted 2011-05-23T20:54:35.307

Reputation: 164

1Link no longer works – danwellman – 2015-02-19T17:45:36.477

2https://gist.github.com/jexchan/2351996 – biniam – 2016-04-25T20:03:50.287

47

You can modify the file ~/.ssh/config to use different identity file for different servers. Edit the ~/.ssh/config in your favorite editor and add an entry that is appropriate for your situation like so:

Host *
IdentityFile ~/.ssh/id_rsa

Host *.github.*
IdentityFile ~/.ssh/github_id.rsa

Host *.someother.com
IdentityFile ~/.ssh/someother_id.rsa

The first part above sets the defaults for all hosts and the other sections overrides what should be used for each of the hosts matching the patterns. If you have a different username for each of the hosts, then you can add a User key followed by the username on the remote to the section.

joehep

Posted 2011-05-23T20:54:35.307

Reputation: 571

3This is the most useful answer to this question regardless of the destination whether GitHub or other sites. – Pierre – 2017-08-20T04:47:39.430

This doesnt work if you have multiple bitbucket accounts and need to have different keys for each unfortunately. – John Little – 2019-03-19T15:05:11.303

chmod 400 ~/.ssh/id_rsa or chmod 400 ~/.ssh/foo_id.rsa might be necessary. – T.Woody – 2019-05-24T20:04:01.930

@JohnLittle Have a look at my answer which solves multi-user account for the same domain. – Sathishkumar Rakkiasamy – 2019-08-20T18:48:20.567

5

You can set up multiple ssh keys for any site having multiple user accounts

Below is the example I used to follow in my development for GitHub.com

Config file example

#Personal account
 Host github.com-<personal-account-name>
 HostName github.com
 User git
 IdentityFile ~/.ssh/id_rsa_personal
 IdentitiesOnly yes



#Organization account
 Host github.com-<organization-name>
 HostName github.com
 User git
 IdentityFile ~/.ssh/id_rsa_work
 IdentitiesOnly yes

At the time of adding a new origin

For Personal account

git remote add origin git@github.com-<personal-account-name>:<personal-account-name>/<repo-name>.git

For Organisation account

git remote add origin git@github.com-<organization-name>:<organization-name>/<repo-name>.git

Hope it helps.

Sathishkumar Rakkiasamy

Posted 2011-05-23T20:54:35.307

Reputation: 151