DEV Community

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

Posted on

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

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
Enter fullscreen mode Exit fullscreen mode

Ensure SSH Agent is running

eval $(ssh-agent)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

ssh-add -D
Enter fullscreen mode Exit fullscreen mode

Add your newly generated SSH Keys to your SSH Agent

ssh-add ~/.ssh/key-1
ssh-add ~/.ssh/key-2
Enter fullscreen mode Exit fullscreen mode

Check that your SSH Keys have been added

ssh-add -l

# Sample output
4096 SHA256:Mo+oiomJASfjlksc6Qj0KcQpUXRVpyvsEIHdXNYDL1c user@website1.com (RSA)
4096 SHA256:6Jt908009upoASFjjkljsflkj9iaGebl7pNr6vuezPI user@website2.com (RSA)
Enter fullscreen mode Exit fullscreen mode

Create your SSH Config file

touch config
Enter fullscreen mode Exit fullscreen mode

The format of our SSH Config

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

Create your different SSH Configs

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

# Bitbucket - SSH Key 2
Host bitbucket-key-2
  HostName bitbucket.org
  User git
  IdentityFile ~/.ssh/key-2-id_rsa
  IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

Important At this point, reload your terminal

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

# Usual format
git clone USER@HOST:ACCOUNT_NAME/REPOSITORY_NAME.git
# Example
git clone git@bitbucket.org:username/repository.git

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

Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
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'

Collapse
 
bashar profile image
Bashar Al-Abdulhadi

you just made my day, thank you!

Collapse
 
shane profile image
Shane McGowan

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

Collapse
 
neerajpro profile image
Neeraj Goswami

yes it is git clone -c core.sshCommand="ssh -i ~/.ssh/your-ssh-fileName" git@github.com:orgname/repo.git

Collapse
 
sorindediu profile image
Sorin Dediu

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