December 15, 2011

How to setup several GitHub accounts on multiple machines with separate SSH keys

Problem

So you've got a personal GitHub account, one for one client, one for another and you want to keep them all separate. How do you setup your box to clone private repositories based on which account you're using? Here's the simple guide on how to do this

Assumptions

I have set this up on Linux (Ubuntu 11.10) and Windows, but only via the Git Bash and/or Cygwin. This post will not cover how to setup and where to store SSH keys for Windows or Mac, though Mac should be fairly similar to Linux.

Setup first SSH key

I'm not going to go into depth on how to setup SSH keys, you can read more about that here. However, I will show the steps I used to get there. For first-account: (please replace "first-account" with your GitHub account)
  1. Change to your home .ssh folder
    cd ~/.ssh
    
  2. Setup your RSA key
    ssh-keygen -t rsa -f ~/.ssh/first-account-rsa -C "first-account@example.com"
    
  3. After following the directions, you should get something that looks like:
    The key fingerprint is:
    00:ff:00:cc:13:00:bb:ll:aa:hh
    
    The key's randomart image is:
    +--[ RSA 2048]----+
    |     .+   +      |
    |       = o P .   |
    |        = * *    |
    |       o = +     |
    |      o S .      |
    |     o o =       |
    |      o . E      |
    |                 |
    |                 |
    +-----------------+
    
    
  4. Open your GitHub.com account setup page, click on the "SSH Public Keys" tab.
  5. Create a new SSH key and copy the contents of ~/.ssh/first-account-rsa.pub and place into the new text field. Name it something unique so you can remember which machine you're using for your account.

    If you plan on each of your accounts needing access, open each of those accounts and add your SSH key to each account. This isn't very common, so only do this if you need to.
  6. To test, type in:
    ssh -t git@github.com
    

    As long as you do not see "access denied", you're good. You might see "failed on channel 0", this is ok.

Adding a second account

Now you should have your first account working, let's setup and add the second account.

  1. Repeat process 1-5 and stop (using the next account)
  2. Create a file named "config" in your ~/.ssh folder
    echo '' > ~/.ssh/config
    
    –or–
    touch ~/.ssh/config
    
  3. Use your favorite editor to add the configuration information for the SSH client to determine which RSA key to use:
    # first-account GitHub account
    Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/first-account-rsa # Note, this is the private key matching the .pub
    
    # second-account
    Host github-second-account  # NOTE: The host is NOT github.com, more on this further down
    HostName github.com
    User git
    IdentityFile ~/.ssh/second-account-rsa # Private key for the second account
    
    # repeat for the third and so on
    
  4. Save the file and your configuration is done

Let's look at the config file in a bit more detail. The first account is the base github.com host name, and you typically see a git URL as git@github.com:user/project.git. With your first account, you will leave this the domain the same**.

NOTE: This doesn't stop you from modifying your ~/.ssh/config file and instead of using "github.com" as your default host, setting the value to "github-first-account".

Now, for your second account, you will need to modify the URL you are using to clone the repository. This is done by changing the domain from "github.com" to "github-second-account". For example: git@github-second-account:user/project.git

Summary

You should now have the setup to clone and modify code for multiple accounts on the same machine. Repeat this process for each machine you would like to make available to GitHub.

4 comments:

Anitha said...

This article is really helpful for me. I am regular visitor to this blog. Share such kind of article more in future. Personally i like this article a lot and you can have a look at my services also: I was seriously search for a Salesforce training institutes in ameerpet which offer job assistance and Salesforce training institutes in Hyderabad who are providing certification material. It's worth to join Salesforce training institutes in India because of their real time projects material and 24x7 support from customer desk. You can easily find the best Salesforce training institutes in kukatpally kphb which are also a part of Pega training institutes in hyderabad. This is amazing to join Data science training institutes in ameerpet who are quire popular with Selenium training institutes in ameerpet and trending coureses like Java training institutes in ameerpet and data science related programming coures python training institutes in ameerpet If you want HCM course then this workday training institutes in ameerpet is best for you to get job on workday.

Technogeekscs said...

This information is really awesome thanks for sharing most valuable information.
Data Science
Selenium
ETL Testing
AWS
Python Online Classes

Nichole Williams said...


I'm truly grateful to you for offering Such sort of instructive articles to us.
Really helpful.
custom logo design company

Haris said...

I feel satisfied with your share.
It's very helpful for me and I am so happy to see your blog.
Thank you for sharing it with us. law dissertation help