How to create an SSH key that works with Bitbucket and Sourcetree
![Oscar de la Hera Gomez](https://www.delasign.com/CDN/headshots/profile.webp)
![Three flowers that represent Bitbucket, Terminal and Sourcetree side by side. Beneath them sits the text "SSH Key for Bitbucket & Sourcetree"](https://www.delasign.com/CDN/images/How-to-create-an-SSH-key-that-works-with-Bitbucket-and-Sourcetree.webp)
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.](https://www.delasign.com/CDN/images/SSH-V.webp)
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.](https://www.delasign.com/CDN/images/SSH-Key-Gen.webp)
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](https://www.delasign.com/CDN/images/ADD_2023-06-09-193947_egtl.webp)
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.](https://www.delasign.com/CDN/images/COPY.webp)
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.