Github, SSH and Multiple Repositories

When working with Github. Setting up SSH key based authentication is easy. You just create a key. Add the public key in the admin panel and voilà. If a new contributor joins your team. You add his / her key and they also have access.

But how does it work when you have need access to multiple repositories using multiple keys yourself? I had to set this up a couple of times. And it turns out to be just as easy. But if for some reason it’s not that obvious these steps might help.

Let’s pretend we need some key first.

$ ssh-keygen -t rsa -b 4096

Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): repo1_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Your identification has been saved in repo1_rsa.
Your public key has been saved in repo1_rsa.pub.
The key fingerprint is:
03:b5:da:08:3b:6f:9a:29:3f:f5:4c:ee:5d:14:cb:41 user@host
The key’s randomart image is:
+–[ RSA 4096]—-+
some output
+—————–+

This will generate the public and private key for repo1. Now repeat the same thing for repo2. Make sure the keys reside in ~/.ssh

Now let’s say we have access to two repositories under two different users / companies. We will use github.com/repo1 and github.com/repo2 as examples. The only thing we have to do is add some entries to ~/.ssh/config to make this work. They key is to use the Github sub-domain mappings

$ vi ~/.ssh/config

Host    repo1.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/repo1_rsa

Host    repo2.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/repo2_rsa

Save the file and we’re done. Now to checkout a project in one of the repositories. Do the following.

$ git clone git@repo1.github.com:repo1/project.git

Cloning into ‘project’…
remote: Counting objects: 86, done.
remote: Compressing objects: 100% (66/66), done.
remote: Total 86 (delta 27), reused 64 (delta 17)
Receiving objects: 100% (86/86), 34.83 KiB, done.
Resolving deltas: 100% (27/27), done.

comments powered by Disqus