How to Setup Multiple Ssh Keys for Multiple Github/Bitbucket accounts.
Tags: git, security, tutorial, ssh
Published: 2019-07-15 12:14:14

---
title: How to Setup Multiple Ssh Keys for Multiple Github/Bitbucket accounts.
published: true
description: This article will help you to set up and configure multiple identities for several Git Repositories Management services. It is very useful if you have a personal and professional identity on a service and you want to use SSH to connect to it.
tags: Git, Security, Tutorial, SSH
series: Git Security
cover_image: https://images.unsplash.com/photo-1529661197280-63dc545366c8?ixlib=rb-1.2.1&auto=format&fit=crop&w=3151&q=80
---
*Photos from [Jantine Doornbos](https://unsplash.com/@jantined) on [Unsplash](https://unsplash.com)*

Hi everyone, 

Everytime I get a new computer (which is not that often but often enough to write this), I am "struggling" with Git configuration for the different code repository accounts I have.

And everytime I have a new computer, I can't remember what I did a few years/months ago to set it up properly.
So that post is as much for future me than it is for present you, as you are reading it. 🤔

----

### tl;dr

To sum up what we are going to do, we are going to **create a bunch of SSH keys for our personal and professional identities** on Github, Gitlab and Bitbucket and add them to the SSH-Agent.
Then we are going to **configure** which **key** must be used based on the host.
Then we are going to **add the relevant keys to the corresponding service** (Github, Gitlab and Bitbucket).
So we can clone, push and pull to repositories with the proper identities.
Every command to perform these actions is described below. 👨‍💻 ⬇️
Enjoy. 😎

----

## Introduction
I have a personal accounts on [GitHub](https://github.com/), [Bitbucket](https://bitbucket.org/) and [GitLab](https://about.gitlab.com/) and I have some work accounts on Github, Gitlab and Bitbucket.
How could I define everything to work properly through SSH Keys so that my system relies on the proper SSH key based on the identity it has to use.

For this particular post, we are going to connect a personal and professional identity for each accounts.
But you can add as many as you need. 😉


----
## :key: Keys Generation

We are going to create some default identities.

We can use the same SSH key to do that or we can use a specific key per account.
Same key : **id_rsa**
Specific key per account : **id_rsa_github**; **id_rsa_bitbucket**; **id_rsa_gitlab**

Let's use the "**specific key per account**" method. It will be clearer for everyone to understand the concept then.
Moreover we need the e-mail address that you are using for these accounts
*But feel free to do whatever suits your need.*😉

### :man_technologist: :old_key: Personal Keys Generation

#### :writing_hand: Information Required

Let sum up what we need in a table

|-|Default Github|Default Gitlab|Default Bitbucket
|--|--|--|--|
|SSH Key Name|id_rsa_github|id_rsa_gitlab|id_rsa_bitbucket|
|e-mail|name.github@gmail.com|name.gitlab@gmail.com|name.bitbucket@gmail.com|

#### :hammer_and_wrench: Keys creation

Let's run these commands to create the SSH keys.
```bash
ssh-keygen -f "~/.ssh/id_rsa_github" -t rsa -b 4096 -C "name.github@gmail.com"
ssh-keygen -f "~/.ssh/id_rsa_gitlab" -t rsa -b 4096 -C "name.gitlab@gmail.com"
ssh-keygen -f "~/.ssh/id_rsa_bitbucket" -t rsa -b 4096 -C "name.bitbucket@gmail.com"
```
Now, we have 3 keys for our personal use.

### :office: :key: Organization Keys Generation

#### :writing_hand: Information Required

Let sum up what we need in a table

|-|Organization Github|Organization Gitlab|Organization Bitbucket
|--|--|--|--|
|SSH Key Name|id_rsa_github_companyName|id_rsa_gitlab_companyName|id_rsa_bitbucket_companyName|
|e-mail|name.github@company.com|name.gitlab@company.com|name.bitbucket@company.com

#### :hammer_and_wrench: Keys creation

Let's run these commands to create the SSH keys.
```bash
ssh-keygen -f "~/.ssh/id_rsa_github_companyName" -t rsa -b 4096 -C "name.github@company.com"
ssh-keygen -f "~/.ssh/id_rsa_gitlab_companyName" -t rsa -b 4096 -C "name.gitlab@company.com"
ssh-keygen -f "~/.ssh/id_rsa_bitbucket_companyName" -t rsa -b 4096 -C "name.bitbucket@company.com"
```
Now, we have 3 keys for our organisation use.

###  :package: Add the SSH Keys to the SSH-Agent

We have now 6 SSH keys. Let add them to the SSH-Agent.

```bash
# Add the personal keys
ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_gitlab
ssh-add ~/.ssh/id_rsa_bitbucket

# Add the organisation keys
ssh-add ~/.ssh/id_rsa_github_companyName
ssh-add ~/.ssh/id_rsa_gitlab_companyName
ssh-add ~/.ssh/id_rsa_bitbucket_companyName
```
So we have in the SSH-Agent the 3 keys for our personal use and the 3 keys for our organisation usage.

Now it is mandatory to set up the configuration in order to define which key has to be use depending on the context.

---
## :memo: Configuration

Open the `~/.ssh/config` file or create it if it doesn't exist yet.

```bash
nano ~/.ssh/config
```

We are going to define some rules based on the hosts.

```text
Host github.com
  HostName github.com
  IdentityFile ~/.ssh/id_rsa_github

Host gitlab.com
  HostName gitlab.com
  IdentityFile ~/.ssh/id_rsa_gitlab
  
Host bitbucket.org
  HostName bitbucket.org
  IdentityFile ~/.ssh/id_rsa_bitbucket


Host companyname.github.com
  HostName github.com
  IdentityFile ~/.ssh/id_rsa_github_companyName

Host companyname.gitlab.com
  HostName gitlab.com
  IdentityFile ~/.ssh/id_rsa_gitlab_companyName

Host companyname.bitbucket.org
  HostName bitbucket.org
  IdentityFile ~/.ssh/id_rsa_bitbucket_companyName
  
```
Save and close the file by hitting Ctrl+O (Ctrl+X to exit the file).

---
## :thought_balloon: Add the Keys to your Repositories Accounts

Everything is setup properly on locally. Now we have to add the SSH **public** keys to the  services you are using.

On MacOS, it is pretty easy to copy a SSH key to the clipboard.

```bash
pbcopy < ~/.ssh/id_rsa.pub
```

### :octopus: Github

Let's login to your Github account and go to the account's settings.

![Github : Go to Settings](https://thepracticaldev.s3.amazonaws.com/i/x1vjz5xpylfmcp4t34yv.png)

Select "SSH and GPG Keys".

![Github : List SSH Keys](https://thepracticaldev.s3.amazonaws.com/i/k6iacznldh4zpjlb0d2m.jpg)

Click on the "New SSH Key" button.

#### 1. Add the Personal SSH Key to Github :

1. Define a relevant title for that SSH key, for example "FirstName LastName - MacBook Pro".
1. Copy the content of the public key to your clipboard.
    * `pbcopy < ~/.ssh/id_rsa_github.pub`
1. Paste it to the content of the key on the Github interface.
1. Save it clicking on "Add SSH key".

![Github : Add SSH Key](https://thepracticaldev.s3.amazonaws.com/i/y4h89bcedbvzko6edzl3.png)


#### 2. Add the Organization SSH Key to Github:

1. Define a relevant title for that SSH key, for example "FirstName LastName - MacBook Pro - Organization".
1. Copy the content of the public key to your clipboard.
    * `pbcopy < ~/.ssh/id_rsa_github_companyName.pub`
1. Paste it to the content of the key on the Github interface.
1. Save it clicking on "Add SSH key".

![Github : Add SSH Key](https://thepracticaldev.s3.amazonaws.com/i/y4h89bcedbvzko6edzl3.png)

### :fox_face: Gitlab

> To be added

### :wastebasket: Bitbucket

Let's log in to your Bitbucket account and go to the account's settings.

![Bitbucket : Go to Settings ](https://thepracticaldev.s3.amazonaws.com/i/k4yuwtrp4xmnenxlrq13.png)

Select "Bitbucket Settings" and "SSH Keys".

![Bitbucket : List SSH Keys](https://thepracticaldev.s3.amazonaws.com/i/p2fg817rlwrac92gaoz5.png)

Click on the "Add key" button.

#### 1. Add the Personal SSH Key to Bitbucket :

1. Define a relevant title for that SSH key, for example "FirstName LastName - MacBook Pro".
1. Copy the content of the public key to your clipboard.
    * `pbcopy < ~/.ssh/id_rsa_bitbucket.pub`
1. Paste it to the content of the key on the Bitbucket interface.
1. Save it clicking on "Add key".

![Bitbucket : Add SSH Key](https://thepracticaldev.s3.amazonaws.com/i/mmmkf250togp06l6mfu5.png)


#### 2. Add the Organization SSH Key to Bitbucket :

1. Define a relevant title for that SSH key, for example "FirstName LastName - MacBook Pro - Organization".
1. Copy the content of the public key to your clipboard.
    * `pbcopy < ~/.ssh/id_rsa_bitbucket_companyName.pub`
1. Paste it to the content of the key on the Bitbucket interface.
1. Save it clicking on "Add key".

![Bitbucket : Add SSH Key](https://thepracticaldev.s3.amazonaws.com/i/mmmkf250togp06l6mfu5.png)

----
## :family_man_boy: Clone Repositories

Now that we have our Setup for all our environments, we can clone repositories from Github, Gitlab or Bitbucket with the proper identity.

### :man_technologist: Personal Repositories

So we can clone the projects using a command you should have used numerous times.

`git clone git@bitbucket.org:yourPersonalAccount/pet-project.git`

With that command, git is using the "default" SSH key. It is the one that was defined for the Host "Host github.com" in the file `~/.ssh/config`.

Then you can `pull`or `push` to the repository with that identity.

### :office: Professional Repositories

For your organization projects, you just have to clone the project replacing `bitbucket.org` to `companyname.bitbucket.org`  (as defined in the `~/.ssh/config` file).

`git clone git@companyname.bitbucket.org:companyName/company-project.git`

So it is the proper identity that is going to be used.
You can then `pull`or `push`as many times as you want with the identity of your organization.

----
I hope that helps you.

Cheers :beers:


*This post was inspired by the very good job from [Fredrik Andersson](https://medium.com/@fredrikanderzon) on [Medium](https://medium.com).*

---

Video produced by [Wild & Secure](https://wildnsecure.com), your consulting firm to all things security and real estate.
If you want to receive weekly quality content about security, subscribe to our newsletter [on our website](https://wildnsecure.com/contact).