How to Configure SSH for both Bitbucket accounts?
Asked by ?n?ei Theodorko:

```
andy@pop-os ~/.ssh> cut --characters -40 id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDJ
```

When I try this pub key bitbucket says "Someone has already added that key as an access key to a repository"  

I have a personal and corporate Bitbucket account. Why Bitbucket requires separate SSH for them? I use the same laptop.  

I checked all of them but my personal accounts did not have an SSH associated with them. Should I create two SSH for personal and corporate? How to use them at the same time?  

<br />

```

andy@pop-os ~> ls .ssh/
id_rsa id_rsa_corporate id_rsa_corporate.pub id_rsa.pub known_hosts
andy@pop-os ~> uname --nodename
pop-os
```

<br />

I have configured bitbucket to use my corporate key manually. Is there a Linux command to do it automatically?

```
andy@pop-os ~> cd /etc/ssh/
andy@pop-os /e/ssh> head ssh_config -50
head: invalid trailing option -- 5
Try 'head --help' for more information.
andy@pop-os /e/ssh [1]> head -30 ssh_config

Include /etc/ssh/ssh_config.d/*.conf

Host bitbucket-corporate
  HostName bitbucket.org
  IdentityFile ~/.ssh/id_rsa_corporate

Host *
# ForwardAgent no
# ForwardX11 no
# ForwardX11Trusted yes
# PasswordAuthentication yes
# HostbasedAuthentication no
# GSSAPIAuthentication no
# GSSAPIDelegateCredentials no
# GSSAPIKeyExchange no
# GSSAPITrustDNS no
# BatchMode no
# CheckHostIP yes
# AddressFamily any
# ConnectTimeout 0
# StrictHostKeyChecking ask
# IdentityFile ~/.ssh/id_rsa
# IdentityFile ~/.ssh/id_dsa
```

<br />

It works for corporate:

```
andy@pop-os ~> ssh -vT git@bitbucket.org
OpenSSH_8.9p1 Ubuntu-3, OpenSSL 3.0.2 15 Mar 2022
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for bitbucket.org
debug1: /etc/ssh/ssh_config line 26: Applying options for *
debug1: Connecting to bitbucket.org [104.192.141.1] port 22.
debug1: Connection established.
debug1: identity file /home/andy/.ssh/id_rsa_cogniflare type 0
debug1: identity file /home/andy/.ssh/id_rsa_cogniflare-cert type -1
...
```

<br />

But how do I manage SSH keys and change my personal Bitbucket account to use my personal \`id_rsa\` SSH private key on the same computer?  

Is there an easy way other than modifying \`/etc/ssh/ssh_config\`?


Answers:
author: Nic Brough -Adaptavist-
body: Welcome to the Community!

The rather brusque answer to this is that you should never share keys between accounts. It's fine to share an account's public key, that's what it is for, but you should never use the same key pair for two accounts.

What you should do here is generate a new key pair for one of the accounts. You'll then be able to add that to Bitbucket. From your question, I think you'll want to do that for your personal account, but I'm not sure if that's best or easiest for you.

comments: author: Bastian Stehmann
body: Completely agree with Nic, one additional reason why should not do that, is, that you are connecting with user git to bitbucket.

Bitbucket recognizes the user only via the public key added to the account.

If you would use one key pair for both accounts, there is no way for Bitbucket to find the right account.


author: Theodora Boudale
body: Hi [@?n?ei Theodorko](/t5/user/viewprofilepage/user-id/5053351) and welcome to the community!

What Nic and Bastian mentioned is correct. If we allowed the same public SSH key to be added to two Bitbucket Cloud accounts, we wouldn't have a way to distinguish the authenticated user who clones/pulls/pushes to a repo, and whether the user has access to do so.

If you want to use SSH for both accounts, you will need to use a different SSH key pair for each account.

Regarding your last query:
> But how do I manage SSH keys and change my personal Bitbucket account to use my personal \`id_rsa\` SSH private key on the same computer?
>
> Is there an easy way other than modifying \`/etc/ssh/ssh_config\`?

I'm not aware of any other way than by configuring the SSH config file, either /etc/ssh/ssh_config or \~/.ssh/config.

We have a blog post with step-by-step instructions on the configuration you need to make:

* <https://blog.developer.atlassian.com/different-ssh-keys-multiple-bitbucket-accounts/>

Please feel free to let us know if you have any other questions.

Kind regards,  
Theodora

comments: author: ?n?ei Theodorko
body: Thanks, namesake!  
I am sending lots of love to the Atlassian community.??  

The blog post answers everything. I had been looking everywhere but could not compose the search phrase to find it. The last step configuring the user in git fills in the missing piece.  
Otherwise, I was seeing:
> This user cannot be matched to an Atlassian account

But clone/push/pull works fine without \`git config user\`.


author: Theodora Boudale
body: You are very welcome, glad to have helped!

Please feel free to reach out if you ever need anything else!


Tags: bitbucket-cloud

cloud