68

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??

2

4 Answers 4

33

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!

3
92

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.

6
  • 6
    This is the most useful answer to this question regardless of the destination whether GitHub or other sites.
    – Pierre
    Aug 20, 2017 at 4:47
  • This doesnt work if you have multiple bitbucket accounts and need to have different keys for each unfortunately. Mar 19, 2019 at 15:05
  • 2
    chmod 400 ~/.ssh/id_rsa or chmod 400 ~/.ssh/foo_id.rsa might be necessary.
    – T.Woody
    May 24, 2019 at 20:04
  • @JohnLittle Have a look at my answer which solves multi-user account for the same domain. Aug 20, 2019 at 18:48
  • 1
    Host *.gitlab.* didn't work for me, I had to use: Host *gitlab.com
    – jhnclvr
    Mar 31, 2022 at 18:40
21

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 (located at ~/.ssh/config )

#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 [email protected]<personal-account-name>:<personal-account-name>/<repo-name>.git

For Organisation account

git remote add origin [email protected]<organization-name>:<organization-name>/<repo-name>.git

Hope it helps.

2
  • Brilliant! Such approach allows to have multiple ssh-keys for one domain, awesome 👍 Mar 10, 2022 at 10:49
  • Thank you! It helps!
    – Tom Chen
    May 9 at 5:39
3

This worked for me

ssh-keygen -f ~/.ssh/<username> 
ssh-add ~/.ssh/<username>
git clone <username>@bitbucket.org:myfancyteam/myfancyproject.git

From : https://support.atlassian.com/bitbucket-cloud/docs/set-up-additional-ssh-keys/

You must log in to answer this question.

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