Using Different SSH Profiles for Different Bitbucket Accounts
For various reasons I have multiple Bitbucket accounts with different organisations, and Bitbucket does not let you use the same SSH key for different accounts under different organisations - probably for a good reason - even though I am using the same machine to access them anyway.
This is just a reminder for myself on how to make use of the SSH config file to handle different profiles for different Bitbucket accounts, so hopefully next time I won’t have to come up with this again. This should work for any git server other than Bitbucket.
- Generate SSH keys for each account, e.g.:
ssh-keygen -f ~/.ssh/bitbucket_org1_key
,_org2_key
, etc - Set up a specific host per Bitbucket account in
~/.ssh/config
, e.g.:Host bitbucket_org1 User git Hostname bitbucket.org IdentityFile /home/yun/.ssh/bitbucket_org1_key IdentitiesOnly yes Host bitbucket_org2 User git Hostname bitbucket.org IdentityFile /home/yun/.ssh/bitbucket_org2_key IdentitiesOnly yes
- (Not related, but you might need this line in your host config too -
AddressFamily inet
- to make sure it uses IPV4 only - otherwise Bitbucket jut won’t let you.)
- (Not related, but you might need this line in your host config too -
- Now test the connection with e.g.
ssh -vv bitbucket_org1
. If something is wrong, go back and figure it out. - Now when you clone a new repo, instead of using the original url, use
git clone git@bitbucket_org1:org_name/repo_name.git
, depending on which account should be use to access the repo.- If it’s an existing repo, modify the remote url with
git remote set-url origin git@bitbucket_org1:org_name/repo_name.git
.
- If it’s an existing repo, modify the remote url with
- Now you should never have to worry about it again. Hopefully.