5

I have two accounts on Bitbucket and an SSH key for each one. This is how my ~/.ssh/config looks like:

Host bitbucket.org
  User git
  IdentityFile ~/.ssh/bitbucket/account1

Host bitbucket.org
  User git
  IdenfityFile ~/.ssh/bitbucket/account2

The problem is that the SSH agent seems to be unable to deal with two keys for the same host at the same time and I have to remove one of them to be able to use the other one.

Is there a workaround?

2 Answers 2

10

By the looks of it, Bitbucket has a wildcard DNS entry in place; any random subdomain (*.bitbucket.org) will resolve to the same server(s) as bitbucket.org.

$ host bitbucket.org
bitbucket.org has address 207.223.240.182
bitbucket.org has address 207.223.240.181
$ host random.bitbucket.org
bitbucket.org has address 207.223.240.182
bitbucket.org has address 207.223.240.181
$ host clearlynotasubdomain.bitbucket.org
bitbucket.org has address 207.223.240.182
bitbucket.org has address 207.223.240.181

Because of this, you can setup different entries in your ~/.ssh/config with different hostnames:

Host project1.bitbucket.org
  User git
  IdentityFile ~/.ssh/bitbucket/account1

Host project2.bitbucket.org
 User git
 IdenfityFile ~/.ssh/bitbucket/account2

In fact, I'd venture a guess that Bitbucket has this wildcard inplace for this exact purpose.

8

You can create two separate SSH aliases, i.e.

Host bitbucket1
  Hostname bitbucket.org
  User git
  IdentityFile ~/.ssh/bitbucket/account1

Host bitbucket2
  Hostname bitbucket.org
  User git
  IdentityFile ~/.ssh/bitbucket/account2

As long as your IdentityFile's are not the default ones (see my question here), then you can use these aliases to select which account to connect to.

5
  • This is a better, more general ssh-native friendly solution than Rain's answer. Rain's answer works only because of how bitbucket's DNS is configured. Note that with eldering's answer, you simply type ssh bitbucket1 or ssh bitbucket2 to connect. The Hostname setting in your .ssh/config handles the hostname for you. Commented Mar 28, 2013 at 22:05
  • @DougHarris I agree; I was unaware of this feature.
    – Rain
    Commented Mar 28, 2013 at 22:29
  • So, you try ssh bitbucket1, and if it fails, you try it again? Or then immediately try ssh bitbucket2 to see if that connects? This answer seems like it would more problems/ambiguity than the answer by @Rain.
    – killermist
    Commented Mar 28, 2013 at 22:41
  • @killermist: No, you use ssh bitbucket1 to connect to account1 (and vice versa for account2). Bitbucket determines your account from the SSH key you use to connect. The point is that the line Host X determines a host alias, which need not be a real hostname; that is specified by Hostname within the entry. Commented Mar 29, 2013 at 0:20
  • @DougHarris actually Rain's answer has the advantage that it can be applied to git submodules too. This answer does not work in that case, unless you assume that all the devs in the team will set up the exact same aliases on their machine. Commented Dec 1, 2021 at 21:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .