Skip to content

Instantly share code, notes, and snippets.

@Gabelbombe
Last active August 10, 2017 19:31
Show Gist options
  • Save Gabelbombe/3657cc82ac40d47980d77165d9022269 to your computer and use it in GitHub Desktop.
Save Gabelbombe/3657cc82ac40d47980d77165d9022269 to your computer and use it in GitHub Desktop.
Multiple Github ssh keys for multiple users

Background

So let's say that you started a new company. New companies mean new gear, new tools... and GASP a new GIT account. Okay, so that's super spiffy and stuff but begs the question. What if you aren't bringing your OWN Github account (GH was just my scenario and is most likely not yours, I'm staring at YOU Bitbucket...).

So the issue that you'll immediately find out (if you're me that is), is that you can't really use two accounts with two individual keys and expect to actually accomplish... well... shit... Hence this mini-paper thing.

The Issue

The issue is is that GH (as Github will be referred to from now on) runs over ssh during snazzy stuff such as the clone, commit as well as the pull processes. So there are a few things we can do here. One of them, is hijack the GIT_SSH or GIT_SSH_COMMAND global variables. This comes with the downfall that you'll be required to use some silly wrapper script to be able to continually issues authority or users in orde to get anything accomplished.

The Answer

So what's a boy to do? You have a deadline already on filing out papers, you most likely need to clone a mess of repos to read over so you need a solution.

Luckily, it's extremely easy. First let's generate two keys (you can always recycle your existing GH pubkey)

cd ~/.ssh ; for KEY in work home ; do
  keyname="4096_${KEY}_rsa"

  ## Generate the keys
  ssh-keygen -t rsa -b 4096 -f "${KEYNAME}"

  ## Add the keys to ssh
  ssh-add "${KEYNAME}"

  ## Add to config
  echo -e "Host github.com-${KEY}\n  HostName github.com\n  User git\n  IdentityFile ~/.ssh/${KEYNAME}\n\n" >> ~/.ssh/config

  ## Test
  echo -e 'Testing...'
  ssh -T "${KEY}"
done

Then clone all repositories using the following script

#!/usr/bin/env bash
USERNAME='username'
PASSWORD='password'
ORGSNAME='organisation'
SSHBYPAS='-work'

ssh-add -l ; read -p "If your key is not present please add, otherwise type [n]: " load

[[ $load != n ]] && ssh-add $(eval echo $load)

mkdir -p ~/Repositories/${ORGSNAME} && cd $_
for REPO in $(curl -sSL -u "${USERNAME}:${PASSWORD}" \
  https://api.github.com/orgs/${ORGSNAME}/repos      \
  |jq -r '.[] .ssh_url') ; do
    [[ $SSHBYPAS = *[![:space:]]* ]] && {
      REPO=$(echo $REPO |sed "s/github\.com/github\.com${SSHBYPAS}/")
    }
  git clone $REPO
done

Notes

So at this point all urls cloned will end up having one of two points that will be hijacked either github-work.com or github-home. In order to default for github I recommend using the following in your ~/.ssh/config file

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

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment