How to create an SSH key that works with Bitbucket and 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.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 06/09/2023 at 15:04
Last Updated on 06/09/2023 at 15:53
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.

Subscribe

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:

git@bitbucket.org: Permission denied (publickey).

fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

- null

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
How to install Homebrew

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.

How to add an SSH key to BitbucketHow to connect Sourctree to a Bitbucket account

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.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe