Step 1. Prepare your default identity
Open the .hg/hgrc
file and locate your Mercurial clone URL at the default
value.
Update the remote URL with your Bitbucket username by replacing hg@bitbucket.org
with <username>@bitbucket.org
. For this step and the ones that follow, enter your username in place of <username>
.
Step 2. Create additional SSH keys
You'll need to create an additional SSH key for each extra Bitbucket account you have or each computer you use. For example, if you have four Bitbucket accounts, you need to generate 3 new SSH keys, meaning you'll have 4 keys in all.
Generate your new SSH key. In place of <username>
, enter the for which you're creating the SSH key.
$ ssh-keygen -f ~/.ssh/<username>
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/<username>/.ssh/<username>.
Your public key has been saved in /Users/<username>/.ssh/<username>.pub.
The key fingerprint is:
7a:9c:b2:9c:8e:4e:f4:af:de:70:77:b9:52:fd:44:97 <username>
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| .|
| Eo|
| . S . ..|
| . . o . ... .|
| . = = ..o o |
| . o X ... . .|
| .ooB.o .. |
+-----------------+
Add your new SSH key to your account. Make sure you're adding it to the right account.
$ ssh-add ~/.ssh/<username>
. (Mercurial only) Enable SSH compression
By default, Git automatically performs compression when sending or retrieving data, but Mercurial doesn't. Enabling SSH compression can speed up sending and retrieving data, drastically in some cases.
To enable SSH compression:
- Open the Mercurial global configuration file (
~/.hgrc
). Add line to the UI section:
When you are done the file should look similar to this:
- Save and close the file.
Step 4. Add the public key to your Account settings
Add each public SSH key into the corresponding account. If you have an account that you access from two different locations, add both keys to that account.
From Bitbucket, choose Personal settings from your avatar in the lower left.
The Account settings page opens.
Click SSH keys.
If you've already added keys, you'll see them on this page.
In your terminal window, copy the contents of your public key file.
From Bitbucket, enter a Label for your new key, for example, Public key #2
.
Paste the copied public key into the SSH Key field:
Click Save.
Bitbucket sends you an email to confirm the addition of the key.
Next time you clone a repository, use the SSH URL for that repository and replace hg
in the <username>@bitbucket.org
part of the URL with your Bitbucket username. For example:
hg clone ssh://<username>@bitbucket.org/teamsinspace/bitbucketspacestation
If you want to change the URL of an existing repository using HTTPS or a different SSH URL, change the remote URL for your repository.
25 Comments
Anonymous
I think it's important to show multiple accounts in the example - this is kinda dropped after step one. I believe this is where the difference of opinion might lie -> it would make sense to clone from an alias if you, indeed, have an alias set up in the .ssh/config file
Anonymous
Address with alias git@alias:accountname/reponame.git did not worked for me (osx).
After googling around i did that in : ~/.ssh/config
and used in .git/config:
Anonymous
Thanks. This helped a lot.
Anonymous
The minimal usable format for the SSH config file on the Mac is
Works fine.lked
Anonymous
I have not been able to get a 2nd identity to work. I started on one Linux machine, followed the procedures for defining and setting up a default ID and everything worked fine. Then on a different Linux system I generated a key pair for a different ID, I tried to follow the procedures where, but have been unable to gain access to Bitbucket via ssh on the 2nd Linux. On the 1st Linux system it still works accessing Bitbucket via ssh.
m
The issue is hard to diagnose without more information. You can first try the suggestions on our Troubleshooting SSH Issues. If that doesn't solve your problem, please send a support issue to support@bitbucket.org. Include in your issue, your accountname, the repo you are trying to access with SSH. The command you enter to connect and any information returned by the command.
Anonymous
Thanks for the troubleshooting link. Here is some of what I'm seeing:
$ ssh-add -l
2048 "What looks like a valid key" /home/tabberta/.ssh/atabbert (RSA)
$ ssh -v hg@bitbucket.org
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
debug1: Reading configuration data /home/tabberta/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to bitbucket.org [207.223.240.182] port 22.
debug1: connect to address 207.223.240.182 port 22: Connection timed out
debug1: Connecting to bitbucket.org [207.223.240.181] port 22.
debug1: connect to address 207.223.240.181 port 22: Connection timed out
ssh: connect to host bitbucket.org port 22: Connection timed out
Anonymous
I guess that's a fingerprint that's shown, not the key, my mistake. Anyway it is correct.
I am unclear on how Bitbucket knows which saved ssh key to verify the incoming request with. Specifically, how does it distinguish between a request from the "default" ID on one system, from a named ID on another system. It is rather confusing.
Thanks
m
Are you trying to connect to the same Bitbucket account from two different computers with two different keys? You'll need to make sure you have uploaded each public key from each system to your Bitbucket account. Your account will show two keys.
You can also just copy the
.ssh
from the working system to the second computer. In which case, the same key connects you from both and your account will show a single key. Copying can be tricky though so google instructions for your OS.henscu
This question really helped me as I want to log in to the same Bitbucket account from a Macbook and also a Windows PC. From the first line of the tutorial above I understood that I needed to set up and manage two identities:
If I had not read these comments before starting I would have wasted a lot of time, though being able to manage multiple identities is a very useful skill.
I will now just set up a default identity for my Macbook and copy the private key to my Windows PC.
It would be great to add a line to explicitly cover this case at the top of the tutorial to save people from unnecessary work.
Thanks again for your fantastically detailed tutorials!
Anonymous
Thanks for the quick response!
Yes to the 1st question, same Bitbucket account, from two different computers, with two different keys. I got the impression that I must do it this way because my login ID is different on the two computers.
If that doesn't matter, I will copy my .ssh directory from the working system to the other and remove the non-working key from Bitbucket.
m
SSH is "identity based authentication." It relies on the key/passphrase and isn't associated with the user account on the machine:
This is the complete explanation.
Anonymous
Thank you! I will give this a try, probably tomorrow. That makes sense.
Anonymous
Unlike the rest This important tutorial is extremely convoluted with steps that dont work in achieving the final goal and not explaning why do we exactly do those steps
m
This is a pretty advanced topic. If you want a beginners guide, please work through the beginners guide here: Set up SSH for Git -- deprecated. I'd be happy to address any specific issues you have with this page if you want to email support@bitbucket.org.
Anonymous
Examples are somewhat unclear. Could somebody give me an example please:
Let's say my repository address as reported is git@bitbucket.org:someuser/someproject.git
I have an ssh-agent running that has a key:
4096 <...> /home/user/.ssh/workid (RSA)
What is the URL I should use with git clone using these settings??
m
Bitbucket always displays for you the URL on the repo Overview page.
The Clone button will give you the entire command for cloning. How you configured your SSH key locally does not impact your choices of URL.
CM
On Mac you can use ssh-add -K [path to your private key] to persist the addition of the second key and store the passphrase in the os x keychain.
Chris Beck
What if I'm going through an extra machine using ssh-agent forwarding?
host
aliases forbitbucket.org
defined in~/.ssh/config
host
has it's own key and everything works peachyssh-add
ssh-add -l
on the server then I see both keys being forwarded~/.ssh/config
on the server but that didn't workIdentityFile
correctly on the server:IdentityFile ~/.ssh/bitbucket_ccmcbeck_rsa
Thanks for any help
Daniel Stevens
Hummm... I'm not sure I have the answer to this Chris. I would think it would see both identities in sequence:
You can try emailing the support team while I look for a better answer. SSH is something this writer is still getting up to snuff on.
Chris Beck
Thanks Dan. Perhaps I can see more of what's going on using ssh -v.
Chris Beck
Still having this problem, so I posted a more detailed description on Superuser
Daniel Stevens
I liked your post so I remember to follow it, not that I like your still having this problem. I'll try to devote some research time to this myself as well.
Chris Beck
Then you'll notice that I was able to solve the issue by:
IdentitiesOnly yes
property so sshd won't try all my forwarded keysm_reeves
When adding the key on a Mac, you can add the -K flag to permanently add the key to your Keychain.
$ ssh-add -K ~/.ssh/workid