Managing Multiple GitHub Accounts with SSH Keys

Umair Mohammad
2 min readJan 7, 2023

We often end up in a situation where we want to authenticate multiple github accounts using ssh keys. Like, personal and work github accounts. Issue is, how to instruct ssh to use different keys for respective github accounts.

So in this blog post, we will go over the steps to generate SSH keys for two different GitHub accounts, add them to our GitHub account, and create or update the ssh config file to link the ssh keys to respective accounts. This allows us to easily work with repositories from both github accounts.

First, navigate to the .ssh directory and generate an SSH key for each GitHub account using something like this

ssh-keygen -t rsa -C “your_name@email.com”

Make sure to give each key a unique file name, such as id_rsa_personal and id_rsa_work.

Next, add the SSH keys to your GitHub account by copying the public key and following the steps in the GitHub Settings under SSH and GPG keys.

Then, we need to update the SSH config file (or, create one if none exists) by adding rules linking ssh keys to respective github accounts.

For this we’ll be using proxy/fake hostnames, something like github.com-personal and github.com-work. While cloning the repository we’ll use this proxy hostname and under the hood ssh will able to differentiate and use respective ssh keys.

# Sample ~/.ssh/config file

# Personal account, - the default config
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal

# Work account
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work

Finally, lets clone the repositories for each account using our respective proxy hostname.

# For example
git clone git@github.com-personal:username/personal-repo.git
git clone git@github.com-work:workname/work-repo.git

Reference : https://www.section.io/engineering-education/using-multiple-ssh-keys-for-multiple-github-accounts/

--

--

Umair Mohammad

Software Engineer. Enthusiastic about tech, data, iot, software development methodologies and growing together.