Setting up 2 different git ssh keys.

I have come across this situation few times where there is a need to connect to 2 or more different repositories at the same time, and configure ssh key for them all. For example : github.com, gitlab.com & bitbucket simulations.

The simpler solution would be to add the same ssh key to all the 3 site, this is ok but can cause issues later, if it does the below suggestion is for you.

Step 1: Create a new SSH key, for the account.

First run below command, place in your email id.

$ ssh-keygen -t rsa -b 4096 -C “email.id@example.com”

Next important thing, add the full path to the file where the key must be generated, as indicated in the below image. Then enter your secret phrase.

Create new ssh key.

Step 2: Update the ssh config file.

Navigate to the ~/.ssh folder on your machine, and open the config file with your favorite text editor. If this file doesn’t exist you can create one.

$ vi ~/.ssh/config

Using the three keywords shown below add in the key details.

  • Host , this should be the domain of the repository being cloned.
  • HostName, can be set similar to Host, in most cases.
  • IdentityFile, should have the same path mentioned when creating the key.

# Gitlab.org

Host gitlab.com

HostName gitlab.com

  IdentityFile ~/.ssh/gitlab

  UseKeychain yes

Example of the finished file.

Once all the keys are created and added on the ssh config file, restart the terminal or command window. Try few pull requests, thats it.. it should work.

Leave a comment

Design a site like this with WordPress.com
Get started