Multi-Account SSH Authentication for Git

One challenge I ran into recently after starting a new role at Ramp was managing SSH authentication multiple GitHub accounts. Being able to access personal repositories like my dotfiles while also accessing work repositories which are managed by a separate GitHub enterprise account was something I hadn’t done before.

With a very simple trick, I was able to configure my SSH to work seamlessly for work and personal repositories with very little hassle. To start, I updated my SSH config like so, splitting into the main github.com host for personal/open-source repositories, and then a work host for work repositories.

~/.ssh/config
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal.pub
  IdentitiesOnly yes

Host work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work.pub
  IdentitiesOnly yes

You might think, “doesn’t the host need to match github.com?”, but actually it doesn’t! The Host property simply defines how to resolve a given host you have configured as a Git remote. The HostName configuration is where we point to github.com as the actual hostname we are connecting to.

With this in place, we can now clone personal repositories normally:

git clone git@github.com:mskelton/dotfiles.git

For work repositories, it’s almost exactly the same as normal, but instead of specifying git@github.com: we can simply specify work: and then the repository path. Neat right!

git clone work:ramp/repo.git