Search This Blog

14 August 2012

Multiple Accounts/SSH keys on BitBucket

Theoretically, I can add my SSH key to any server I have access to.  With BitBucket, that's not always true.

BitBucket does not allow the same key to be used for two different accounts.  Security measure, I'm sure.  It does make it difficult to access my company account and my personal account from the same machine.

The workaround is to do the initial download via https, then change the protocol.  More specifically....

Setup:

Assuming 'WORK' and 'HOME' are two separate BitBucket accounts and you are using a single client (laptop, whatever) to access them both...  Decide which one should be less effort -- in this case, I chose WORK.

1. Create an SSH key for WORK
ssh-keygen
Choose the default /home/username/.ssh/id_rsa
2. Copy id_rsa.pub into your BitBucket WORK account
3. Create an SSH key for HOME
ssh-keygen
Choose /home/username/.ssh/id_personal_rsa or something else obvious
4. Copy id_personal_rsa.pub into your BitBucket HOME account

The Good:

For WORK repos, you can simply do the normal
hg clone ssh://hg@bitbucket.org/WORK/REPO

The Ok, I Guess:

For HOME repos, you have to take an extra step.
hg clone https://USERNAME@bitbucket.org/USERNAME/REPO

Then, edit REPO/.hg/hgrc
Change
default = hg clone https://USERNAME@bitbucket.org/USERNAME/REPO
to
default = hg clone ssh://hg@bitbucket.org/USERNAME/REPO

Then, add this into that file:
[ui]
ssh = ssh -C -i ~/.ssh/id_personal_rsa.pub -o IdentitiesOnly=yes

Now you should be able to push/pull using SSH.


Conclusion

You are probably asking yourself why all of this is necessary.  From scavenging the web and looking at all the "Won't Fix" answers...  My best understanding is because the ssh url BitBucket gives out does not include the username.