Handling multiple Github accounts with multiple SSH keys

Ahhhhhh…the challenge of dual personalities. I have been trying to connect via SSH to all my github accounts. However, I have struggled to assign the correct hostname for the applicable SSH keys.

After lots of online searches, trials and errors, I finally figured out my mistake: I was modifying a local configuration file, instead of updating my global configuration file (.ssh/config file) which is the one SSH looks at when connecting to the requested host.

So, here is the correct process:

  1. Generate new SSH keys and assign them to your SSH agent per github tutorial.
    • In case you wonder (I did), the SSH key is a secure and efficient way to connect to a server as you use a passphrase that is stored in your local machine. Paired with an SSH-agent, you store all your keys in memory so that you will not have to retype your long passphrase every time.  Read more about this here.
    • In my case, I ended up with two keys:
      • mykey_perso
      • mykey_work
    • I have two github accounts/usernames:
      • pinkintello/pinkintello_user
      • pinkwork/pinkwork_user
  2. Once you’ve added your keys to your SSH agent, you need to assign each key to the apppropriate Github account per this cool git tutorial.
  3. Then, you may want to change your remote from HTTPS to SSH as shown here.
  4. Now, you have two (or more) keys, but how to use the right one for a specific git account? The secret lies within your .ssh/config file …and this is were I made a mistake.  You want to  modify the config file that SSH uses (right way), not create and modify a random config file at the local directory of your project (mistake). Therefore, you can:
    1. $ touch ~/.ssh/config

    2. $vim ~/.ssh/config (feel free to use whichever editor you prefer)

      This is how my config file looks like:

      # github_perso
      Host github.com

      HostName github.com

      User git

      IdentityFile ~/.ssh/mykey_perso

      # github_work
      Host github-work

      HostName github.com

      User git

      IdentityFile ~/.ssh/mykey_work

  5. You want to check your SSH connections:

Running ssh -T git@github.com  should generate a message that says:

Hi pinkintello_user! You've successfully authenticated, but GitHub does not provide shell access.

Running ssh -T git@github-work  should generate a message that says:

Hi pinkwork_user! You've successfully authenticated, but GitHub does not provide shell access.

Now, say you want to clone your work git repo, make sure to use the correct host name:

git clone git@github-work:workAccount/pinkwork.git

Et voila!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s