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.
Account | SSH key name | |
---|---|---|
Github | id_rsa_github | name.github@gmail.com |
Gitlab | id_rsa_gitlab | name.gitlab@gmail.com |
Bitbucket | id_rsa_bitbucket | name.bitbucket@gmail.com |
🛠️ SSH key creation
Let’s run these commands to create SSH keys.
Now we have 3 keys for our personal use.
🏢 🔑 Organization key generation
✍️ Required information
Let’s summarize what we need in a table.
Account | SSH key name | |
---|---|---|
Organization Github | id_rsa_github_companyName | name.github@company.com |
Organization Gitlab | id_rsa_gitlab_companyName | name.gitlab@company.com |
Organization Bitbucket | id_rsa_bitbucket_companyName | name.bitbucket@company.com |
🛠️ Creating SSH keys
Let’s run these commands to create SSH keys.
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.
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.
1vim ~/.ssh/config
Nous allons définir quelques règles basées sur les hôtes.
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.
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.
- Github: documentation
- Gitlab: documentation
- Bitbucket: documentation
👨👦 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.
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).
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.