DEV Community

Cover image for Manage ssh keys for Multiple Github accounts
Prasanna
Prasanna

Posted on

Manage ssh keys for Multiple Github accounts

Currently, I am working for a company where they create a Github account using the company’s email id and all the repository access would be given to that Github account.
But I have a personal Github account as well and I am quite active in that too.

Problem

Now the problem is, How to manage the SSH keys for both the GitHub accounts. Git by default takes the ~/.ssh/id_rsa(.pub) keys and we can’t add the same ssh key in both the GitHub account.

Solution

We can manage multiple Github accounts using the ssh config file.

Create a new ssh key pair

Generate a new ssh key pair using the company’s email id.

ssh-keygen -t rsa -b 4096 -C "prasanna@his-company.com"

In the prompt, specify a different file name.
Eg: /User/prasanna/.ssh/id_rsa_my_company

Add the ssh key to the ssh-agent

Now add the generated key using the ssh-add command.

ssh-add ~/.ssh/id_rsa_my_company
Output:
Identity added: /Users/prasanna/.ssh/id_rsa_my_company (prasanna@his-company.com)

Add the id_rsa_my_company.pub key to your company Github account.

cat ~/.ssh/id_rsa_my_company.pub|pbcopy

Update the hosts in the ssh config file

Update the config file (~/.ssh/config) with these values and it should have entry to for you personal Github account,

My Company Github Account

Host github-mycompany.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_my_company

Personal Github Account

Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

Clone your personal Github and company Github projects

You can clone your personal private projects using the command,

git clone git@github.com:your-github-account/private-repo.git

You can clone your company ‘s private projects using the command,

git clone git@github-mycompany.com:company-acc/private.git

That’s it. Hope it will be useful. Let me know If you have any questions in the comment section.

Top comments (1)

Collapse
 
horiyomi profile image
horiyomi •

Thank you so much, this is so helpful.