My SSH key was setup for GitHub and working fine, but I wanted to add a separate one for Bitbucket1. It was daunting playing around with this when everything was already working largely as I expected, but it was simple to get this as I wanted.
First, backup your existing keys first in case something goes wrong and you accidentally overwrite them. (You're backing up your data anyway, right?)
Create a new
key. If you've
got an id_rsa
key already, you can name the new key e.g.
~/.ssh/id_rsa.1
(or 2, 3…). (Initially, I put the new key in a
separate Bitbucket directory and I don't think that the ssh-agent
would
automatically pick it up there, so I moved it back to the .ssh
directory.)
Add the following to ~/.ssh/config
:
Host github.com
User git
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
Host bitbucket.org
User git
IdentityFile ~/.ssh/id_rsa.1
IdentitiesOnly yes
IdentitiesOnly
ensures that only the specified key is used for that host; if you don't
have this, you may end up failing to
authenticate
as too many incorrect keys have been passed to the server.
If you're using the key in this session, you might need to ssh-add
it.
In my case, on Ubuntu 12.04, on subsequent logins, the new key was made
available automatically.
-
Unlike GitHub, Bitbucket only matches commits when the commit contains an email address that has been confirmed on Bitbucket, so any commits you make aren't matched to your user account. You can override this by setting aliases, but this is on a per repository basis and you can't do this unless you're an admin of the repository. ↩