Tip of the Week: Using different SSH keys for multiple Bitbucket accounts

Create a key for each of your accounts

To generate a new key pair simply run this command in the ~/.ssh/ folder:

 ssh-keygen -t rsa -C "user1" -f "user1"

The -C option is a comment to help identify the key. The -f option specifies the file name. Repeat the above for each Bitbucket account you want to use.

Add the public key to the correct Bitbucket account

To add a public key to a Bitbucket account, you need to go to the Bitbucket Settings Screen. Select SSH Keys in the left side menu and click Add key.

For more detailed information check out the Bitbucket documentation:

Configure SSH

In ~/.ssh/ create a file called config with contents based on this:

 #user1 account
 Host bitbucket.org-user1
     HostName bitbucket.org
     User git
     IdentityFile ~/.ssh/user1
     IdentitiesOnly yes

 #user2 account
 Host bitbucket.org-user2
     HostName bitbucket.org
     User git
     IdentityFile ~/.ssh/user2
     IdentitiesOnly yes

Replace user1 or user2 with your Bitbucket usernames

Getting your keys on a keyring

This is only useful if you use a passphrase to protect your key. Otherwise you can skip this.

Depending on your operating system you’ll need to find out how best to do this.

Linux users can use GnomeKeyring.

Mac users can use the following command to permanently add keys to the Mac SSH Agent:

 ssh-add -K ~/.ssh/user1.rsa

Windows users can take a look here for more info: Git On Windows

You can check the keys on your keyring with:

 ssh-add -L

Configure your Git repo

If you don’t have a local copy of your repo, you have to run the following command to clone a Bitbucket repository:

 git clone git@bitbucket.org-user1:user1/your-repo-name.git    

If you already have a local copy, you’ll need to update the origin:

 git remote set-url origin git@bitbucket.org-user1:user1/your-repo-name.git

Now go to the local Git repo that you want to configure and enter:

git config user.name "user1"
git config user.email "user1@example.com"

Where user1 matches the values you used earlier in your ssh config.

You can now git push as normal and the correct key will automatically be used.

I hope you found this tip useful! Check out our earlier Tips of the Week or tweet your own tip suggestions to @pvdevoor.”


This post is based on the following sources:


This post was updated on 17 April 2018 to reflect the change of bitbucket.com to bitbucket.org.

Exit mobile version