Adding SSH Keys for Multiple GitHub Accounts on the Same System | Complete Guide
logo
Manas Sinha
Developer | Designer
logo
Manas Sinha
Developer | Designer

By Manas | 30 June 2021

Adding SSH Keys for Multiple GitHub Accounts on the Same System | Complete Guide

If you have different personal and work GitHub accounts and are trying to use SSH-based authentication for both of them chances are you will face some errors. In this article I will show you how to do it.

1. Generate SSH keys

The first step will be to generate two separate ssh keys for the two accounts. To generate SSH key use the following command:
ssh-keygen -t rsa -b 4096 -C your_email@example.com
replace your_email@example.com with the email registered for the github account and provide suitable name for the keys when prompted.
Let’s say the two key generate are named as:
  • id_rsa_account1
  • id_rsa_account2

2. Add SSH keys

Use the following command to add the new ssh-keys:
ssh-add ~/.ssh/id_rsa_account1
ssh-add ~/.ssh/id_rsa_account1

3. Add SSH keys to your GitHub account

Steps to add ssh keys to GitHub account:
  • First You need to copy the ssh-key that we generated. You can run
    cat ~/.ssh/id_rsa_account1.pub
    and copy the output. You can follow the GitHub documentation for detailed steps.
  • Login to your GitHub account.
  • Go to
    settings -> SSH and GPG keys
  • Click on
    New SSH key
  • Paste the copied key.

4. Creating the config file

Now Here the most important step. Create a config file using the following command:
touch ~/.ssh/config
In this file we will specify which key is for which account.
Here is an example of what it should look like:
# The 'me' can be replaced with anything you like
Host me.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_account1

Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_account2

5. Verifying

You can verify that the GitHub accounts are added using the following command:
ssh -T git@me.github.com
It will prompt with a message :
Hi {username}! You've successfully authenticated
Same method for the second account:
ssh -T git@github.com
It will prompt with a message :
Hi {username}! You've successfully authenticated

6. While cloning a repository or adding remote origin

Just like what we did while verifying, we have to replace git@github.com with the customised Host.
Here is an example:
  • Cloning:
    git clone git@me.github.com:USERNAME/PROJECT.git
  • Adding remote origin:
    git remote add origin git@me.github.com:USERNAME/PROJECT.git

LEAVE A COMMENT

If you like the post leave a comment and share it.

Leave a Reply