Introduction

I have personal and work accounts on GitHub, GitLab and Bitbucket.
How could I set all of this to work properly through SSH keys so that my system relies on the correct SSH key based on the identity it needs to use.

For this particular post, we’re going to connect a personal and business identity for each account.
But you can add as many as you like 😉

🔑 Key generation.

We’re going to create default identities.

We can use the same SSH key for this or use a specific key per account.

  • Same key: id_rsa
  • Account specific key: id_rsa_github; id_rsa_bitbucket; id_rsa_gitlab

Let’s use the “account specific key” method. It will then be clearer for everyone to understand the concept.
Also, we need the email address you use for these accounts.
But feel free to do whatever you like.😉

👨‍💻 🗝️ Generation of personal keys

✍️ Required information

Let’s summarize what we need in a table.

AccountSSH key nameE-mail
Githubid_rsa_githubname.github@gmail.com
Gitlabid_rsa_gitlabname.gitlab@gmail.com
Bitbucketid_rsa_bitbucketname.bitbucket@gmail.com

🛠️ SSH key creation

Let’s run these commands to create SSH keys.

SSH key creation
1ssh-keygen -f "~/.ssh/id_rsa_github" -t rsa -b 4096 -C "name.github@gmail.com"
2ssh-keygen -f "~/.ssh/id_rsa_gitlab" -t rsa -b 4096 -C "name.gitlab@gmail.com"
3ssh-keygen -f "~/.ssh/id_rsa_bitbucket" -t rsa -b 4096 -C "name.bitbucket@gmail.com"

Now we have 3 keys for our personal use.

🏢 🔑 Organization key generation

✍️ Required information

Let’s summarize what we need in a table.

AccountSSH key nameE-mail
Organization Githubid_rsa_github_companyNamename.github@company.com
Organization Gitlabid_rsa_gitlab_companyNamename.gitlab@company.com
Organization Bitbucketid_rsa_bitbucket_companyNamename.bitbucket@company.com

🛠️ Creating SSH keys

Let’s run these commands to create SSH keys.

SSH key creation
1ssh-keygen -f "~/.ssh/id_rsa_github_companyName" -t rsa -b 4096 -C "name.github@company.com"
2ssh-keygen -f "~/.ssh/id_rsa_gitlab_companyName" -t rsa -b 4096 -C "name.gitlab@company.com"
3ssh-keygen -f "~/.ssh/id_rsa_bitbucket_companyName" -t rsa -b 4096 -C "name.bitbucket@company.com"

Now we have 3 keys for our business use.

📦 Add SSH keys to the SSH agent

We now have 6 SSH keys. Let’s add them to the SSH agent.

Adding keys in the SSH agent
1# Add personal keys
2ssh-add ~/.ssh/id_rsa_github
3ssh-add ~/.ssh/id_rsa_gitlab
4ssh-add ~/.ssh/id_rsa_bitbucket
5
6# Add organization keys
7ssh-add ~/.ssh/id_rsa_github_companyName
8ssh-add ~/.ssh/id_rsa_gitlab_companyName
9ssh-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 the professional use.

Now it is mandatory to set up the configuration in order to define which key should be used according to the context.

📝 Configuration

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

Open the file ~/.ssh/config
1vim ~/.ssh/config

Nous allons définir quelques règles basées sur les hôtes.

vim ~/.ssh/config
 1Host github.com
 2  HostName github.com
 3  IdentityFile ~/.ssh/id_rsa_github
 4
 5Host gitlab.com
 6  HostName gitlab.com
 7  IdentityFile ~/.ssh/id_rsa_gitlab
 8
 9Host bitbucket.org
10  HostName bitbucket.org
11  IdentityFile ~/.ssh/id_rsa_bitbucket
12
13
14Host organisation.github.com
15  HostName github.com
16  IdentityFile ~/.ssh/id_rsa_github_companyName
17
18Host organisation.gitlab.com
19  HostName gitlab.com
20  IdentityFile ~/.ssh/id_rsa_gitlab_companyName
21
22Host organisation.bitbucket.org
23  HostName bitbucket.org
24  IdentityFile ~/.ssh/id_rsa_bitbucket_companyName

Save and close the file.

💭 Add the keys to your repository accounts

Everything is set up correctly locally. Now you need to add the public SSH keys to the services you are using.

Put in the clipboard
1# macOS
2tr -d '\n' < ~/.ssh/id_rsa.pub | pbcopy
3
4# Linux (requires the xclip package)
5xclip -sel clip < ~/.ssh/id_rsa.pub
6
7# Git Bash on Windows
8cat ~/.ssh/id_rsa.pub | clip

Let’s log into your accounts and go to the settings to add our SSH keys.

Follow the documentation of your service to know how to add the keys.

👨‍👦 Cloning repositories

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

👨‍💻 Personal repositories

So we can clone projects using a command that you must have used many times.

Clone a personal repository
1git clone git@bitbucket.org:yourPersonalAccount/pet-project.git

With this command, git uses the “default” SSH key. This is the one that has been set for the host “Host github.com” in the file ~/.ssh/config.

You can then pull or push on the repository with this identity.

🏢 Organization repositories

For your organization’s projects, simply clone the project replacing bitbucket.org with organization.bitbucket.org (as defined in the ~/.ssh/config file).

Clone a professional repository
1git clone git@companyname.bitbucket.org:companyName/company-project.git

So the right identity will be used.
You can then pullor push as many times as you want with your organization’s identity.

I hope this helps.