I'm having some trouble getting two different SSH keys/GitHub accounts to play well together. I have the following setup:
Repos accessible from one account using git@github.com:accountname
Repos accessible from another account using git@github.com:anotheraccount
Each account has its own SSH key. Both SSH keys have been added and I have created a config file. I don't believe the config file is correct though. I'm not quite sure how to specify that repos accessed using git@github.com:accountname
should use id_rsa
and git@github.com:anotheraccount
should use id_rsa_anotheraccount
.
I have 2 accounts on github, and here is what I did (on
linux
) to make it work.Keys
ssh-keygen
, name them properly, so that make life easier.ssh-add path_to_private_key
Configuration
~/.ssh/config
Set remote url for repo:
For repo in Host
github-kc
:For repo in Host
github-abc
:Explaination
Options in
~/.ssh/config
:Host
github-<identify_specific_user>Host could be any value that could identify a host plus an account, it don't need to be a real host, e.g
github-kc
identify one of my account on github for my local laptop,When set remote url for a git repo, this is the value to put after
git@
, that's how a repo maps to a Host, e.ggit remote set-url origin git@github-kc:kuchaguangjie/pygtrans.git
Host
]Hostname
specify the actual hostname, just use
github.com
for github,User
gitthe user is always
git
for github,IdentityFile
specify key to use, just put the path the a public key,
LogLevel
specify log level to debug, if any issue,
DEBUG3
gives the most detailed info.A possibly simpler alternative to editing the ssh config file (as suggested in all other answers), is to configure an individual repository to use a different (e.g. non-default) ssh key.
Inside the repository for which you want to use a different key, run:
And make sure, your key is added to the ssh-agent, by running:
Keep in mind that the command above will only add key to the ssh-agent for your current session. If you want this to work forever, you have to "permanently" add it to your ssh-agent. E.g here's how to do it for ubuntu and here for OSX.
It should also be possible to scale this approach to multiple repositories using global git config and conditional includes (see example).
I recently had to do this and had to sift through all these answers and their comments to eventually piece the information together, so I'll put it all here, in one post, for your convenience:
Step 1: ssh keys
Create any keypairs you'll need. In this example I've named me default/original 'id_rsa' (which is the default) and my new one 'id_rsa-work':
Step 2: ssh config
Set up multiple ssh profiles by creating/modifying ~/.ssh/config. Note the slightly differing 'Host' values:
Step 3: ssh-add
You may or may not have to do this. To check, list identity fingerprints by running:
If your entries aren't there then run:
Step 4: test
To test you've done this all correctly, I suggest the following quick check:
Note that you'll have to change the hostname (github / work.github) depending on what key/identity you'd like to use. But now you should be good to go! :)
I posted the technique I use to deal with these here
I spent a lot of time to understand all the steps. So lets describe step by step:
ssh-keygen -t rsa
. Give it an alternative likeproj1.id_rsa
and hit with no doubt because you don't need a passphrase.Add new section in
.ssh/config
:Take into account the first section and note that
proj1.github.com
we will back to the section later.ssh-add ~/.ssh/proj1.id_rsa
proj1.github.com
(exactly the host from the config file).git clone git@proj1.github.com
.A good tutorial.
Don't mess up with hosts