Guide to configure multiple Github accounts with SSH

Guide to configure multiple Github accounts with SSH

Every developer will face the necessity to handle several Github accounts at some point. You start a new job and need to create a new Github account for your company's repository and one you have for your personal account.

Github's new modifications no longer allow you to submit and pull code changes using your email and password. Sadly, there is now a new buzzword for setting up SSH keys for each Github account. I would keep it simple, just follow the below steps and you would be all good to go.

Steps to be followed for setting it up smoothly:

  1. Create SSH keys for both accounts
  2. Adding the SSH keys to your Github account
  3. Edit/Create SSH key config file
  4. Add ssh keys to your SSH agent
  5. Test your connection

1. Generating SSH keys

Let's start by making two SSH keys: one for your personal account and one for your work account. To generate it, open your terminal and input the following:

$ cd ~/.ssh
$ ssh-keygen -t rsa -b 4096 -C "your_personal_email@domain.com"
  # save as id_rsa_personal
$ ssh-keygen -t rsa -b 4096 -C "your_work_email@domain.com"
  # save as id_rsa_work

If it's created successfully you might see 4 different files in your ssh directory. The respective file name would be id_rsa_personal, id_rsa_personal.pub, id_rsa_work, id_rsa_work.pub

2. Adding the SSH keys to your Github account

Copy and paste are programmed by programmers for the programmers !!!

I think you got what we are going to do here :P. We are going to copy each public SSH keys and save it into our Github account. Let's do that, open your terminal

$ cd ~/.ssh
$ ls 
# would list down all your keys save
$ gedit id_rsa_personal.pub
# open the id_rsa_personal key in an editor, copy it and paste it into your Github account

Follow the same step to save your work account key to your work account.

Wooh! You have successfully added your ssh key to your Github account. Time for some configuration now.

3. Edit/Create config file

Let's get started with some configuration. This would alert the ssh agent to the existence of several accounts and allow them to be appropriately linked. Head off to your terminal again

$ cd ~/.ssh
$ touch config
# would create the config file required
$ gedit config
# would open the config file in a editor

Now write down the following in your config file.

# Default github account: deepak
Host github-personal #keep host name as per your wish
   HostName github.com
   IdentityFile ~/.ssh/id_rsa_personal #update this accordingly to your saved key name
   IdentitiesOnly yes

# Other github account: work
Host github-work
   HostName github.com
   IdentityFile ~/.ssh/id_rsa_work #update this accordingly to your saved key name
   IdentitiesOnly yes

Save the config file. Let's add SSH key to agent and test connection. Hold on for few minutes you are almost done !!!

4. Adding SSH key to ssh agent

You have created your config file now and you are all set to use your Github account now just after this last change. Open your terminal and do the following

$ ssh-add ~/.ssh/id_rsa_personal
$ ssh-add ~/.ssh/id_rsa_work

You have now added your keys to SSH agent. You wrote so much stuffs, won't you test it out?

5. Testing your connection

Open your terminal and write this command to test your connection.

$ ssh -T git@github-personal
$ ssh -T git@github-work

After each command you will see something like this:

The authenticity of host 'github-personal (192.30.252.1)' can't be established.
RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:
Are you sure you want to continue connecting (yes/no)?

If everything is ok, you might see something like this.

Hi deepak2431! You've successfully authenticated, but GitHub does not provide shell access.

Congrats! You have now setup your multiple Github accounts perfectly.

Clone your repo

You are all good now to clone your repo, make changes, commit it.

$ git clone git@github-personal:deepak2431/project2.git 
$ cd /path/to/project2
$ git config user.email "your_email@domain.com"
$ git config user.name  "Your Name"

Conclusion

In this article you saw how to setup multiple Github accounts in your machine. Hope it worked for you perfectly :)

This was my first Article on Hashnode. I think you enjoyed it. I am here to share my experiences and learning on web development, software engineering, and remote jobs.

Follow me on Twitter

Connect with me on Linkedin