Recently I ran into an issue I’d never had before. I wanted to access more than one GitHub account from the same machine using SSH. I needed to figure out how to use multiple SSH keys on GitHub from one computer.
In this case it was due to my new job having our code hosted on GitHub and wanting to still access my personal account. This problem isn’t isolated to GitHub, of course. Pretty much any cloud-based system with SSH support could suffer this same issue.
Generating the SSH Pair
Presumably, you already know how to generate a key. Here’s GitHub’s documentation on generating ssh key pairs. In this case, however, since you probably already have one called
id_rsa, just make sure you give the new one a different filename. For my example let’s pretend I’m creating one for work and calling it
Once you’ve generated the new pair you can associate it to the GitHub account you don’t already have a key associated with. See GitHub’s documentation on doing that in case you’ve forgotten.
Edit (or create) ssh config
Navigate to your
~/.ssh folder. In Windows, this is found within
c:\Users\[username]\.ssh. From bash you can simply access it via
cd ~/.ssh. If you don’t have a config file, you can issue the
touch config command (or Windows command prompt, simply
copy NUL config).
config file you’ll need a couple
host mappings. One for each account. Something like the following:
# Default github account: personal Host github.com HostName github.com IdentityFile ~/.ssh/id_rsa IdentitiesOnly yes # Other github account: work Host github-work HostName github.com IdentityFile ~/.ssh/id_rsa_work IdentitiesOnly yes
Depending on the computer, you may want “work” to be your default instead of personal. Totally personal preference here.
Now I have multiple SSH keys I can use on GitHub!
Add ssh private keys to agent
In the examples I found, I actually was able to skip this step. I’m uncertain if it is because it automatically added it to my ssh-agent or what. All I know is that I could skip it. I’m including it for good measure anyway.
$ ssh-add ~/.ssh/id_rsa $ ssh-add ~/.ssh/id_rsa_work
After adding them (or, in my case, I didn’t have to), be sure to test them out. You can issue the following commands to test the connection:
$ ssh -T email@example.com $ ssh -T git@github-work
Cloning the repo
For your default account, clone as normal. For the other account, however, you’ll need to slightly modify the url.
$ git clone git@github-work:org2/project2.git
You may also want to update the user.name and user.email on the cloned repo:
$ cd /path/to/project2 $ git config user.email "firstname.lastname@example.org" $ git config user.name "My Name"
It is possible to have multiple SSH keys on your computer in order to access different GitHub accounts. Making use of the
.ssh/config file and creative usage of the
host property allows you to access those accounts.
My initial Googling for help turned up a few links. Between two of them in particular and the comments, I was able to get up and running.
- oanhnn has a gist for using multiple github accounts with ssh keys.
- jexchan also has a gist but I found oanhnn’s more up-to-date.
- See also Bitbucket on setting up additional SSH keys.