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/ 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.


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.

  IdentityFile ~/.ssh/id_rsa

  IdentityFile ~/.ssh/companyName

  IdentityFile ~/.ssh/id_rsa

  IdentityFile ~/.ssh/companyName

First you make sure that requests to plain uses the default ~/.ssh/id_rsa that you set up for your personal account.

Next, add a rule for and make it use the ~/.ssh/companyName key instead. can be anything that makes sense to you FluffyBunny or Bow-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/

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/

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

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 should be replaced by (or whatever matches the SSH rules you set up earlier).

git clone

This will ensure that the correct SSH key is used and verify your identity with Bitbucket/Github.

That's all folks! 👋