3

I'm trying to get multiple Bitbucket accounts working from the same machine via SSH.

In my .ssh\config file, I have:

Host account1.bitbucket.org
  Hostname bitbucket.org
  PreferredAuthentications publickey
  IdentitiesOnly yes
  IdentityFile ~/.ssh/account1

Host account2.bitbucket.org
  Hostname bitbucket.org
  PreferredAuthentications publickey
  IdentitiesOnly yes
  IdentityFile ~/.ssh/account2

Then I'm changing the remote for the git repo to use the alias:

git@account1.bitbucket.org:myUserName/myRepoName.git
git@account2.bitbucket.org:myUserName/myRepoName.git

Then in Pageant (in the system tray), I'm adding both my PPK keys.

Unfortunately if I've added both keys to Pageant, only one of the accounts work.

If I only have one key in Pageant at a time, then it'll work for whichever Bitbucket account I added to Pageant.

So it looks like pageant isn't using the correct key if there's more than one key in its list.

Any ideas on why it's doing this?

Dan
  • 5,692
  • 3
  • 35
  • 66

2 Answers2

2

This link on Atlassian Answers explains how to use PuTTY and SSH with multiple bitbucket account:

One way to resolve this is to perform the same configuration in PuTTY too to disambiguate what key to send (and therefore which user to authenticate as).

Start PuTTY (download it from putty.org if you don't have it)

Type 'bitbucket.org' in the host name field

Go to Connection > SSH > Auth in the tree

Specify the key to use for the BB user

Go back to 'Session' in the tree

Type an alias name underneath 'Saved Sessions' (e.g. bb-user1) and Save

Repeat 2-6 for each BB user and save as a different session name

Then in your remote URLs, replace 'bitbucket.org' with the session name (e.g. bb-user1) to disambiguate what SSH key to send first. This is identical to using IdentityFile in OpenSSH.

Finally, you can add the keys in pageant to cache the key passcodes.

Stephen Hartley
  • 945
  • 1
  • 11
  • 17
0

I got this working by not using Pageant. I'm using Sourcetree as a Git GUI, and in the options, there's a dropdown saying whether to use OpenSSH or Putty/Plink.

Based on the description by Steve Streeting here:

https://answers.atlassian.com/questions/164479/sourcetree-support-multiple-ssh-keys

It sounds like Pageant doesn't use the .ssh\config configuration anyway.

By changing to OpenSSH, then regenerating the keys using ssh-keygen, and adding the private keys via Sourcetree's "Tools .. Add SSH Keys" menu, this is now working perfectly using different keys across multiple accounts. I just need to alter the remote url of each git repo to use the correct alias in the config file (as described in my initial post).

Update:

I've written a blog post about this here: http://www.danclarke.com/multiple-ssh-keys-for-git/

Dan
  • 5,692
  • 3
  • 35
  • 66