Multiple bitbucket accounts
Asked Answered
M

5

80

I have a Bitbucket account for my 9-5 job and I also have a personal Bitbucket account. My goal is to be able to use both on the same computer. I have installed the latest git on a Windows 7 pc.

So currently everything with my companies Bitbucket account works fine, I can pull/push with no problems. I created a new ssh key using ssh-keygen and assigned a new name in my case "tech". But I am having issues on how to tell a local repo to use the new ssh key I created. I am assuming everytime I try to connect it uses the first ssh key.

I get the error:

$ git push conq: repository access denied. fatal: The remote end hung up unexpectedly

I found some advice on the internet but it seems to relate to a linux/git setup, for example I can't find the "config" file on windows.

Mcgurn answered 18/11, 2011 at 17:30 Comment(1)
Possible duplicate of Multiple GitHub Accounts & SSH ConfigGranlund
B
53

You may get this error if you haven't added the key to the key manager (ssh-agent). To do this:

ssh-add ~/.ssh/tech

By the way, if you have multiple Bitbucket accounts, you'll need a unique key for each account. You can't reuse keys.

Boyd answered 28/12, 2011 at 20:20 Comment(1)
That fixed my problem, the step is missing from the documentation the bitbucket sit. thanks.Decretive
A
125

This blog post describes a straightforward way to add multiple ssh keys to a single computer and use one ssh key per a bitbucket account. It is much clearer than the official bitbucket documentation. To summarize:

First, make sure you have a default account setup through a tutorial like this one on Github.

For the second account:

  1. Create a new ssh key:

    ssh-keygen -f ~/.ssh/<your second account name> -C "<you email>"
    
  2. Add the ssh key:

    ssh-add ~/.ssh/<key file name> 
    
  3. Use pbcopy < ~/.ssh/<your second account name>.pub to copy the public key and add this key to your bitbucket account (in the settings area)

(On Windows you can copy the ssh key using ssh-keygen -f ~/.ssh/<your account name> -c "<your email>" | clip or on Linux you can follow these instructions.)

  1. Add the following to your ~/.ssh/config file. The first sets the default key for bitbucket.org. The second sets your second key to an alias bitbucket-account2 for bitbucket.org:

    Host bitbucket.org
      Hostname bitbucket.org
      IdentityFile ~/.ssh/id_rsa
    
    Host bitbucket-account2
      Hostname bitbucket.org
      PreferredAuthentications publickey
      IdentityFile ~/.ssh/<your second account name>
    
  2. You can now clone projects with your default account the same way as before:

    git clone [email protected]:username/project.git
    
  3. To clone a project with the second identity, replace bitbucket.org with the Host that you specified in the ~/.ssh/config file (i.e. bitbucket-account2 above):

    git clone git@bitbucket-account2:username/project.git
    

That's it!

Advocation answered 11/2, 2014 at 20:22 Comment(6)
I had to add the ssh key using the following command : ssh-add ~/.ssh/<key file name>Aerodontia
As @SamikBandyopadhyay points out, on any system using ssh-agent (e.g. Mac since Leopard) you will also need to add the key to the agent using ssh-add. I found without doing this step, ssh was still using the standard id_rsa for the aliased domain, and even when using the ssh -i parameter to explicitly path to the second identity. Very confusing! ssh-add immediately fixed it.Nehru
There is no pbcopy command on windows, so this instruction is incomplete.Awakening
@StepanYakovenko updated to include Windows and Linux pbcopy equivalents.Advocation
This answer should be marked as a solution - really!Chuffy
@SamikBandyopadhyay thanks for the suggestion - added this to the answer.Advocation
B
53

You may get this error if you haven't added the key to the key manager (ssh-agent). To do this:

ssh-add ~/.ssh/tech

By the way, if you have multiple Bitbucket accounts, you'll need a unique key for each account. You can't reuse keys.

Boyd answered 28/12, 2011 at 20:20 Comment(1)
That fixed my problem, the step is missing from the documentation the bitbucket sit. thanks.Decretive
C
0

As stated, you only need to generate your pubkey once - since you're already setup with BitBucket, where is your id_rsa (or whatever you named yours) file? On our Windows installs, it's under the user's home directory in the hidden folder .ssh. You should be able to create a config file there.

Complimentary answered 18/11, 2011 at 17:41 Comment(0)
P
-1

You should generate public/private key pair only once. Then all hosts which have your public key do allow connections from you if you provide the private key.

Packton answered 18/11, 2011 at 17:34 Comment(6)
If you have only one public/private key pair, then you cannot access two repositories on bitbucket. When you try to add your public ssh key to the second one, it will complain that that key has already been added to an account.Cutout
@JonCrowell Sounds weird. I've not used bitbucket, but github allows assign one key to several repos. Several keys have sense for different devices. E.g. one for laptop, another for desktop. If your laptop is stolen, you could revoke its key but continue to use the desktop.Packton
I don't think this should be the accepted answer since accessing Bitbucket is a requirement in the question.Wesleyanism
@Wesleyanism "You only have to create two identities (keys) if you have two different Bitbucket accounts". It tells if you have two accounts, in the question there are two repositories, not accounts.Packton
@Packton - didn't the question say "I have a Bitbucket account for my 9-5 job and I also have a personal Bitbucket account. My goal is to be able to use both on the same computer."? It sounds to me like he has 2 Bitbucket accounts?Wesleyanism
@Wesleyanism Hm, looks like you are right. However, it is weird why it works for him, probably he has different setup.Packton
R
-1

You can add your company email in your personal bitbucket account, In bitbucket account manage page:

enter image description here

You can login your personal email account, and access both personal projects and company projects in single bitbucket account, which is using only one ssh private key.

Refine answered 22/7, 2014 at 15:34 Comment(1)
Wrong! You get [email protected] is already in use by another user.Pacificas

© 2022 - 2024 — McMap. All rights reserved.