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.
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 id_rsa_work
.
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.
Now before I go any farther on my post, I want to call out kudos to Oanh Nguyen and his gist. It’s what got me 99% of the way there.
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
).
Within this 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!
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.
#programming #git #ssh