Multiple Bitbucket Cloud accounts with SSH authentication

December 7, 2021 – Samuli Seppänen

In Bitbucket usernames are unique across whole of Bitbucket. Moreover, the same SSH key can only be configured for one user. If you registered your Bitbucket account using a corporate email and used your primary SSH key with it, you're pretty much hosed if you then need to create another corporate Bitbucket account and wanted to use the same SSH key with it - that's just not going to happen. The solution to this dilemma is to create another SSH keypair and somehow make SSH, Git and Bitbucket co-operate. As the correct solution was not immediately obvious it is documented here.

The first step is to create a new SSH keypair:

$ ssh-keygen -t ed25519 -f ~/.ssh/john_company_b

NOTE: RSA key's won't work with Bitbucket.

Once you have the key add entries for all your Bitbucket accounts in ~/.ssh/config:

Host bitbucket.org-john_company_a
     HostName bitbucket.org
     User git
     IdentityFile ~/.ssh/john_company_a
     IdentitiesOnly yes

Host bitbucket.org-john_company_b
     HostName bitbucket.org
     User git
     IdentityFile ~/.ssh/john_company_b
     IdentitiesOnly yes

Now comes the important part: you need to change the remote URLs so that Git will know which of these Host sections to use for each Git repository.

To clone a repository from "Company A":

git clone [email protected]_company_a:company_a/some-repository.git

To clone a repository from "Company B":

git clone [email protected]rg-john_company_b:company_b/some-repository.git

If you tried to just use

git clone [email protected]:company_b/some-repository.git

you would get Forbidden unless Git happened to use the correct SSH key by default.

Want to talk to an expert?

If you want to reach us, just send us a message or book a free call!
Categories

Tags

#aad #Access #acl #alertmanager #ansible #ansible module development #Apache #API #augeas #authentication #authorization #automation #automatization #aws #azure #backup #bash #bitbucket #buildbot #cache #centos #cloud #cloud-init #cloudflare #cloudfront #cluster #connectionsJpa #control repo #custom fact #database #debian #devops #digital sovereignty #DNS #docker #domain mode #duplo #ejabberd #email #encryption #erb #europe #eyaml #fabric #facter #facts #fargate #fedora #file #finnish #foreman #freeipa #git #github #gitlab #gnome #google #grafana #hammer #hiera #IAM #import #infinispan #Infrastructure as Code #ipmi #irc #jboss #jdk #jenkins #JMESPath #kanban #keycloak #librarian-puppet #librenms #linkedin #Linux #Location #loop #marketing #mautic #Mellon #mfa #monitoring #mysql #nagios #network-manager #oauth #oauth2 #office365 #open source #openvpn #oxygen #packer #paranormal #pdk #people #php #pkcs7 #pomodoro #Powershell #preseed #presentation #profiles #prometheus #provisioning #puppet #puppet-bolt #puppet-litmus #puppetboard #puppetdb #Puppetfile #puppetserver #puppet types and providers #pxeboot #qemu #quality #r10k #recruitment #redirect #Restrict #Reverse Proxy #roles #rspec #ruby #SAML #sem #shell #showsql #snmp #snmpd #software developement #spam #ssh #sso #standardization #systemd #systemd-resolved #teams #terraform #ubuntu #user-data #vagrant #vanity awards #variable #vim #virtualbox #visualstudio #webdevelopment #wildfly #Windows #wireguard #wordpress #workflow #x11 #xmpp #zimbra
We are
 Puppeteers
menucross-circle