Multiple SSH identities for multiple GIT remotes in OSX

If you’re a member of different development teams or you have a multiple GIT remotes setup, you might need to deal with multiple SSH identities.

In my own case, I use my own git server (with Gogs) and BitBucket for my private repos, as well as GitHub for my public repos and gists.

This is my own setup to deal with multiple SSH identities in OSX.

Obviously, you can use the same key pair for your different git remotes. But sometimes, you need to have different identities in the same git server, which don’t allow you to use the same SSH key. For example, you might have two different identities in GitHub at the case you belong a development team and in the same time, you have your own personal GitHub identity.

Create another (and different) key pair

The first thing you need is a new and different key pair for your additional remote. You can create a new key pair with:

$ ssh-keygen -t rsa -C "your_email@youremail.com"

You must provide a new name for your new key pair files:

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/you/.ssh/id_rsa): /Users/you/.ssh/id_rsa_newid

Then you can setup a passphrase for your new SSH key:

Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/you/.ssh/id_rsa_newid.
Your public key has been saved in /Users/you/.ssh/id_rsa_newid.pub.
The key fingerprint is:
SHA256:fwR5yBiNz+3kvu6X4Bb418Ekw9NN6tiWl5vATp/1vjU your_email@youremail.com
The keys randomart image is:
+---[RSA 2048]----+
|     . ..=.      |
|      = +=oo     |
|       =o+=..    |
|       =..++.    |
|      . S.ooo    |
|       +.=+*..   |
|        .=*=...E.|
|          B.o. ..|
|         ==o  o. |
+----[SHA256]-----+

Now you have two (or more) different SSH key pairs created:

$ ls ~/.ssh/
..
-rw-------   1 you          staff   1,7K 27 nov 13:56 id_rsa
-rw-r--r--   1 you          staff   404B 27 nov 13:56 id_rsa.pub
-rw-------   1 you          staff   1,7K 14 dic 11:56 id_rsa_newid
-rw-r--r--   1 you          staff   404B 14 dic 11:56 id_rsa_newid.pub
..

And finally, add your new ssh key:

$ ssh-add ~/.ssh/id_rsa_newid

You can check all your loaded keys with:

$ ssh-add -l

If you need it, also you can remove all loaded/cached keys:

$ ssh-add -D

Modify your SSH config file

Once you have created your new SSH key, it’s time to modify the SSH config file to setup your different SSH identities. Use your favorite text editor (Atom in my case) to edit (or create) that file:

$ atom ~/.ssh/config

Then add your identities:

#My personal account
Host github.com-myid
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa

#My team member account
Host github.com-measamember
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_newid

Now every time you get or post data to your repos, SSH knows which identity must be used.

One more thing…

It would be a good idea to setup your local git repo config with the corresponding name and email:

$ cd /path/to/your/local/repo
$ git config user.name "my name as a member"
$ git config user.email "your_email@youremail.com"

Cheers! :beers:

"Multiple SSH identities for multiple GIT remotes in OSX" was originally published on 16 Dec 2015