Search This Blog

Showing posts with label ssh. Show all posts
Showing posts with label ssh. Show all posts

Sunday, 16 November 2014

Managing ssh keys for multiple GitHub accounts

  1. Create ssh keys using the following command:
    ssh-keygen -t rsa -C "xxx@youremail.com"
    ssh-keygen -t rsa -C "yyy@youremail.com"
    
    
    Each email corresponds to a different github account. 
    It is also possible to have repository specific deploy keys.
    If a repository does not have its own deploy key, access to it 
    is given by the repo's github account ssh keys. 
  2.  Two pairs of ssh keys are created in ~/.ssh dir:
    1. ~/.ssh/id_rsa_xxx
      ~/.ssh/id_rsa_yyy
      with names chosen in step 1.
  3. Add keys using:
    1.   ssh-add ~/.ssh/id_rsa_xxx
       ssh-add ~/.ssh/id_rsa_yyy
      This adds adds your RSA identities to the authentication agent,  
      ssh-agent, a program for storing private keys used for public key 
      authentication.
  4.  Use ssh-add -l to list all added keys
  5. Associate the keys with the Github accounts according to instructions on:
    1.  https://help.github.com/articles/generating-ssh-keys/
  6. On your local machine configure GitHub settings. Add configuration to ~/.ssh/config:
    1. #xxx account
      Host github.com-xxx
          HostName github.com 
          User git
          IdentityFile ~/.ssh/id_rsa_xxx
      
      #yyy account
      Host github.com-yyy
          HostName github.com
          User git 
          IdentityFile ~/.ssh/id_rsa_yyy
      Hosts github.com-xxx, github.com-yyy are arbitrary names,
      that help distinguish the two accounts 
  7.  Test authorization to connect to GitHub:
    1. ssh -T git@github.com-xxx
    2. ssh -T git@github.com-yyy
  8. Clone a repository, using the relevant Host alias:
    1. git clone git@github.com-xxx:xxx/repo_aaa.git [aaa_dir]
  9.  The general git configuration can be found in ~/.gitconfig. The entries can be either added manually or through git commands:
    1.  git config --global ... (user.email, user.name etc)
  10.  For repository specific configuration (repository's .git/config): 
    1. cd repo_aaa (or aaa_dir)
    2. git config user.name "X XXX
    3. git config user.email "xxx@gmail.com"
    4. git remote set-url origin  git@github.com-xxx:xxx/repo_aaa.git
      1. This will result in the following setup:
        [remote "origin"]
                url = git@github.com-xxx:xxx/repo_aaa.git
        where
        github.com-xxx corresponds to the Host value in step 6
        xxx            corresponds to the github username 

NOTE

  • Configuration, that is present only in the global git config file ~/.gitconfig (setup either through the git config --global comand or manually) and not overriden by cloned repository's .git/config setup, will be used.
  • Setting up the [remote "origin"] block in the global configuration, results in a bizarre situation, where one can be cloning an empty (well, any) github repo, and the local clone will have an unexpected content (corresponding to the remote origin url, ie a different repo).