Adding Multiple GitHub Accounts for SSH Communication

You may have multiple github accounts. In this post, we cover setting up ssh authentication for multiple github accounts from one computer, so pushing code to and from different github accounts is an easy process

Step 1. Generate the extra key in .ssh directory and copy it into clipboard

ssh-keygen -t rsa -C "your_email@youremail.com"
pbcopy < ~/.ssh/id_rsa_new_key.pub

In the line 1, it generates new ssh key into file(for example, in ‘id_rsa_new_key.pub’) and in line 2, it copies the new key into clipboard

Step 2. Paste new key into the GitHub account
In your account on the GitHub.com, go to the ‘Account Settings’-> ‘SSH Keys’ and past the new key

Step 3. Update ssh config (~/.ssh/config)
On you computer, in the ~/.ssh directory add the entry

Host github.com-kapasoft-views
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_new_key
Host github.com-kapasoft-scripts
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_new_key2

Here the ‘github.com-kapasofts-views’ is part of the git remote repo url(i.e. git@github.com-kapasoft-views:user/repo.git). The ‘IdentityFile’ is file created in Step 1. When you are going to push the content, ssh looks up the url of remote repo and compares it to the Host ‘github.com-kapasofts-views’ in .ssh/config file to find the action HostName(i.e. github.com) along the ssh key to use for authentication

Step-4. Add the key to ssh cache

ssh-add ~/.ssh/id_rsa_new_key

Step-5. Update the git remote repo accordingly in the git directory

git remote add origin git@github.com-kapasoft-views:github-account/remoteRepo.git

Here the ‘github.com-kapasoft-views’ part is the same specified in the ~.ssh/config file. You can also manually edit it in the repository .git/config file

Issues

1. ERROR: Permission to gitHub-account/remoteRepo.git denied to gitHub-account.
fatal: Could not read from remote repository.
The step 3 was missed or it was needed to be do again:

ssh-add ~/.ssh/id_rsa_new_key

This solved the problem. Important to note that this keep happening often time on Mac Lion where i have to re-add the key to the ssh-key cache often time

2. ssh: Could not resolve hostname nodename nor servname provided, or not known
I am not sure the cause, but after authenticating directly with github as following, the error went away:

ssh -T git@github.com

Useful links:

  • https://gist.github.com/jexchan/2351996
  • https://help.github.com/articles/generating-ssh-keys

Leave a Reply

Your email address will not be published. Required fields are marked *