Setting up SSH keys for multiple Bitbucket/Github accounts
Every time I switch computers I go through this dance of setting up new SSH keys for my different code repository accounts.
Coincidentally, every time I switch computers I've completely forgotten how to set up different SSH keys for my different accounts, leading to me Googling and looking through my old .ssh
folder a lot trying to figure it out.
So this post is for future me, and probably present you, since you are here. 🤔
Generating keys
If you are setting up a new machine, first generate a key to the default ~/.ssh/id_rsa
by running:
ssh-keygen -t rsa
ssh-add ~/.ssh/id_rsa
The created key is a private key you should never share with anyone. A public key
~/.ssh/id_rsa.pub
will also be created.
You will be prompted for the location to save the keys in, hit enter to accept the default. You will also be asked for a passphrase to protect the key, enter anything you want here.
The idea is that you'll use this key for your personal accounts.
Next, for each company account you have, run this command (substituting companyName to something more meaningful), which tags the key with companyName and saves it to ~/.ssh/companyName
.
ssh-keygen -t rsa -C "companyName" -f "companyName"
ssh-add ~/.ssh/companyName
That takes care of generating the keys, next up you have some SSH config to write.
Config
Open up the SSH config file:
nano ~/.ssh/config
Don't worry if this file doesn't already exists, it will be created later when you save your changes.
What you will do is add rules for different hosts.
Host bitbucket.org
HostName bitbucket.org
IdentityFile ~/.ssh/id_rsa
Host companyname.bitbucket.org
HostName bitbucket.org
IdentityFile ~/.ssh/companyName
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa
Host companyname.github.com
HostName github.com
IdentityFile ~/.ssh/companyName
First you make sure that requests to plain bitbucket.org
uses the default ~/.ssh/id_rsa
that you set up for your personal account.
Next, add a rule for companyname.bitbucket.org
and make it use the ~/.ssh/companyName
key instead.
companyname.bitbucket.org
can be anything that makes sense to youFluffyBunny
orBow-Ties-Are-Cool
. A subdomain makes sense to me, and it's a format that I remember when cloning repositories.
The Github config is just a repeat of the Bitbucket config.
When you are happy with how your config looks, save the changes by hitting CTRL+O
(CTRL+X
to exit the file).
Adding SSH keys to Bitbucket/Github
You're all set locally, now we just need to tell Bitbucket/Github to trust these keys you have created.
The key you share with these services is the public key, copy it to your clipboard with:
pbcopy < ~/.ssh/id_rsa.pub
Now log in to Bitbucket or Github and add your SSDH key.
In Bitbucket:
1. Click on your user icon in the top right and select Bitbucket settings
from the menu
2. Click on SSH keys
in the left hand menu
3. Click the Add key
button, enter whatever you want for label and paste your key into the box below.
4. Click 'Add key' and you are done
In Github:
1. Click on your user icon in the top right and select Settings
from the menu
2. Click SSH and GPG keys
in the left hand menu.
3. Click the New SSH key
button, enter whatever you want for title and paste your key in the box below
4. Click Add key
and you're done
Repeat process for the next account, except this time copy the companyName key pbcopy < ~/.ssh/companyName.pub
Cloning repositories
With all the setup done you should be ready to clone a repository.
Personal projects can now be cloned by following the normal directions given by Bitbucket/Github, like this:
git clone git@bitbucket.org:yourPersonalAccount/pet-project.git
Your default SSH key will verify your access and you will be able to pull and push as much as you want.
For your work projects however, one alteration needs to be made, in a command like this:
git clone git@bitbucket.org:companyName/company-project.git
bitbucket.org
should be replaced by companyname.bitbucket.org
(or whatever matches the SSH rules you set up earlier).
git clone git@companyname.bitbucket.org:companyName/company-project.git
This will ensure that the correct SSH key is used and verify your identity with Bitbucket/Github.
That's all folks! 👋