public
Last active

Multiple SSH keys for different github accounts

  • Download Gist
multiple_ssh_setting.md
Markdown

Multiple SSH Keys settings for different github account

create different public key

create different ssh key according the article Mac Set-Up Git

$ ssh-keygen -t rsa -C "your_email@youremail.com"

Please refer to github ssh issues for common problems.

for example, 2 keys created at:

~/.ssh/id_rsa_activehacker
~/.ssh/id_rsa_jexchan

then, add these two keys as following

$ ssh-add ~/.ssh/id_rsa_activehacker
$ ssh-add ~/.ssh/id_rsa_jexchan

you can delete all cached keys before

$ ssh-add -D

finally, you can check your saved keys

$ ssh-add -l

Modify the ssh config

$ cd ~/.ssh/
$ touch config
$ subl -a config

Then added

#activehacker account
Host github.com-activehacker
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_activehacker

#jexchan account
Host github.com-jexchan
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_jexchan

Clone you repo and modify your Git config

clone your repo git clone git@github.com:activehacker/gfs.git gfs_jexchan

cd gfs_jexchan and modify git config

$ git config user.name "jexchan"
$ git config user.email "jexchan@gmail.com" 

$ git config user.name "activehacker"
$ git config user.email "jexlab@gmail.com" 

or you can have global git config $ git config --global user.name "jexchan" $ git config --global user.email "jexchan@gmail.com"

then use normal flow to push your code

$ git add .
$ git commit -m "your comments"
$ git push

Another related article in Chinese

  1. http://4simple.github.com/docs/multipleSSHkeys/

I had to change the [remote "origin"] / url field in my local .git/config to use the Host defined in .ssh/config in order for this to work, i.e.,

[remote "origin"]
        url = git@github.com-activehacker:activehacker/gfs.git

Without that modification, git would just try to use my default ssh key.

Had to do the same to get it working.

Instead of having to change the remote origin in the config after cloning, you can just use that custom Host variable on the git clone command instead.

e.g.

git clone git@github.com-activehacker:activehacker/gfs.git gfs_jexchan
________________________^

as oppose to:

git clone git@github.com:activehacker/gfs.git gfs_jexchan

that's the ticket!

Thanks!

To make it work with Gists, I needed to add another entry in ~/.ssh/config:

Host gist.github.com
  ...

If your ssh keys don't all show up with
ssh-add -l
you have to run
ssh-add ~/.ssh/yourkey.rsa

thanks, worked like a charm

thanks, would be better if it could be changed as @jmpnot mentioned.

when trying to pull through a php script via exec it would not use the key automatically though it was the correct user configured (my user). Going through the ssh config file just saved the day! :D

ssh config +
updated clone url

Working great... :-) Thanks alot...

I was having an issue with SSH using the wrong key, depending on which order I accessed repositories. Turns out it will try all the keys loaded into ssh-agent in order, and IdentityFile only adds to the end of that list.

You can add IdentitiesOnly yes the SSH config block to force it to only use the IdentityFile you specified.

I was also having issues with SSH using the wrong key even after following the advice of most who have encountered the same issue. In my case, I have a globally enabled ControlMaster in my ssh config. Removing the global ControlMaster config and deleting the github ssh socket (which in my case, is stored in /tmp as ssh_hostname.com_user as specified by my ControlPath config) fixed the issue for me. I can still use ControlMaster in other hosts by adding a ControlMaster auto line in that host's block.

@oleweidner: Thank you !

How do you do this with https urls though?

Thanks Jexchan. It helped and working as expected.

Thank you. It worked fine.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.