How to create an SSH key that works with Bitbucket and Sourcetree

Oscar de la Hera Gomez
Three flowers that represent Bitbucket, Terminal and Sourcetree side by side. Beneath them sits the text "SSH Key for Bitbucket & Sourcetree"

A step by step guide on creating and using an SSH key that works with Bitbucket and Sourcetree which solves the Permission denied (publickey) error.

The following tutorial solves the issue that arises by which you can perform operations on a Bitbucket git repository using Terminal but not with Sourcetree.

This issue produces the following error message:

The error arises if you do not create an rsa ssh key. Here's how to create one.

Step One: Install OpenSSH

A screenshot of Terminal showing the "ssh -v" command.

Run the following line in Terminal to check if you have OpenSSH installed

ssh -V

If you don't install it using Homebrew, by running the line below:

brew install openssh

Step Two: Create SSH Key

A screenshot of Terminal showing you how to create an rsa key by running ssh-keygen -t rsa -b 2048.

Run the following line in Terminal to create an rsa ssh key:

ssh-keygen -t rsa -b 2048

Step Three: Add to id_rsa

A screenshot of Terminal showing you how to add your key to your ssh agent by running ssh-add ~/.ssh/id_rsa

Run the command below to add the key to your systems ssh agent:

ssh-add ~/.ssh/id_rsa

Step Four: Copy to the key to your clipboard

A screenshot of Terminal showing you how to copy the key to your clipboard by running cat id_rsa.pub | pbcopy.

Run the following command in Terminal to copy the key.

cat id_rsa.pub | pbcopy

Use this key to create a new SSH key in Bitbucket.

Remaining Steps

In order to successfully use this key with Bitbucket and Sourcetree you must:

  • Add the ssh key to your Bitbucket account.
  • Connect your Bitbucket account with Sourcetree.

To learn how to do this, consult the guides below.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details