DEV Community

Cover image for How to use different SSH Keys for different Bitbucket (git) Repositories

How to use different SSH Keys for different Bitbucket (git) Repositories

shane profile image shane ・2 min read

Bitbucket doesn't allow you to use the same SSH Key on more than one account. This means if you have more than 1 account but need to access the repositories on both (in my case a work account and a personal account) you will need to configure your SSH agent to allow you to choose which SSH Key to use when cloning over SSH.

Navigate to your SSH Directory

cd ~/.ssh

Ensure SSH Agent is running

eval $(ssh-agent)

Create your SSH Keys

# Run the following replacing EMAIL_ADDRESS with the email address associated with your bitbucket account
ssh-keygen -t rsa -b 4096 -C "EMAIL_ADDRESS"

# When prompted to enter a file name, enter a name to help you differentiate your two keys
Enter file in which to save the key (/c/Users/shane/.ssh/id_rsa): key-1

(optional) Remove previous SSH Keys for your SSH Agent (this does not delete the actual key from your ~/.ssh directory)

ssh-add -D

Add your newly generated SSH Keys to your SSH Agent

ssh-add ~/.ssh/key-1
ssh-add ~/.ssh/key-2

Check that your SSH Keys have been added

ssh-add -l

# Sample output
4096 SHA256:Mo+oiomJASfjlksc6Qj0KcQpUXRVpyvsEIHdXNYDL1c (RSA)
4096 SHA256:6Jt908009upoASFjjkljsflkj9iaGebl7pNr6vuezPI (RSA)

Create your SSH Config file

touch config

The format of our SSH Config

Host bitbucket-key-1 # Friendly name
  HostName # The server we opening the SSH connection with
  User git # The SSH user (which for bitbucket is git eg.
  IdentityFile ~/.ssh/id_rsa # The private key file
  IdentitiesOnly yes # Tells SSH to only use keys provided by IdentityFile above

Create your different SSH Configs

# Bitbucket - SSH Key 1
Host bitbucket-key-1
  User git
  IdentityFile ~/.ssh/key-1-id_rsa
  IdentitiesOnly yes

# Bitbucket - SSH Key 2
Host bitbucket-key-2
  User git
  IdentityFile ~/.ssh/key-2-id_rsa
  IdentitiesOnly yes

Important At this point, reload your terminal

Now when cloning via SSH we replace the usual host name ( with our new "friendly" name

# Usual format
# Example
git clone

# Format using our new SSH Host config (replace with the user friendly host name from our .config file)
git clone git@bitbucket-key-1:username/repository.git

Discussion (4)

Editor guide
neerajpro profile image
Neeraj Goswami

Great article. There is an alternative to creating a SSH config file. Through your terminal you can tell any github repo to point to a particular ssh key file. just set sshcommand for that repo.

git -c core.sshCommand="ssh -i private_key_file" clone host:repo.git

git config core.sshCommand 'ssh -i private_key_file'

shane profile image
shane Author

Cool! Do you know if there is a way to choose your SSH Key for use with git clone when first cloning the repo?

neerajpro profile image
Neeraj Goswami

yes it is git clone -c core.sshCommand="ssh -i ~/.ssh/your-ssh-fileName"

sorindediu profile image
Sorin Dediu

Thanks, I was looking for something similar.
Great article!