How to maintain multiple bitbucket accounts with multiple ssh keys in the same system

GitBitbucket

Git Problem Overview


I have multiple Git accounts one is my personal use and one is for company use. Both accounts source need to be activated from my laptop. Here I generated two ssh keys like id_rsa.pub,id_benwork_rsa.pub and I configured the config of git as

Host sfsworkdid
 HostName bitbucket.org
 IdentityFile ~/.ssh/id_rsa
Host workdid
 HostName bitbucket.org
 IdentityFile ~/.ssh/id_benwork_rsa

So here is my problem: while pushing to any repo git asking the first ssh_key passphrase. Everytime I am changing the user.name in git config as git config user.name "mybitbucketusername". So please tell me how to maintain multiple git accounts with multiple ssh keys in the same system

I tried https://stackoverflow.com/questions/8924210/how-to-work-with-multiple-ssh-keys, https://stackoverflow.com/questions/8186441/multiple-bitbucket-accounts but no use

https://stackoverflow.com/questions/18725033/push-using-multiple-account-multiple-identity-on-github-bitbucket is somewhat helpful to reach up to now

Git Solutions


Solution 1 - Git

Create multiple identities for Mac OSX, GitBash, and Linux

You should at this point already have created at least a single default identity. To see if you have a default identity already, list the contents of your .ssh directory. Default identity files appear as a id_encrypt and id_encrypt.pub pair. The encrypt value is either rsa or dsa. Use the ssh-keygen command to create a new identity. In the example below, the identity is named personalid.

$ ssh-keygen -f ~/.ssh/personalid -C "personalid"
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/manthony/.ssh/personalid.
Your public key has been saved in /Users/manthony/.ssh/personalid.pub.
The key fingerprint is:
7a:9c:b2:9c:8e:4e:f4:af:de:70:77:b9:52:fd:44:97 personalid
The key's randomart image is:
+--[ RSA 2048]----+
|         |
|         |
|        .|
|        Eo|
|  .  S  . ..|
|  . . o . ... .|
|  . = = ..o o |
|  . o X ... . .|
|  .ooB.o ..  |
+-----------------+

If you have multiple Bitbucket accounts, you need to generate a new public/private key pair for each account.

Create a SSH config file

When you have multiple identity files, create a SSH config file mechanisms to create aliases for your various identities. You can construct a SSH config file using many parameters and different approaches. The format for the alias entries use in this example is:

Host alias
HostName bitbucket.org
IdentityFile ~/.ssh/identity

To create a config file for two identities (workid and personalid), you would do the following:

  1. Open a terminal window.

  2. Edit the ~/.ssh/config file. If you don't have a config file, create one.

  3. Add an alias for each identity combination for example:

    Host workid
     HostName bitbucket.org
     IdentityFile ~/.ssh/workid
    Host personalid
     HostName bitbucket.org
     IdentityFile ~/.ssh/personalid
    
  4. Close and save the file.

Now, you can substitute the alias for portions of the repository URL address as below: [email protected]:accountname/reponame.git -> git@alias:accountname/reponame.git

Load each key into the appropriate Bitbucket account

enter image description here

Ensure the ssh-agent is running and loaded with your keys

enter image description here

Clone a repository using SSH and your alias configuration

To clone a repository with one of multiple SSH identities that you configured, you clone the repo and using your alias in the SSH URL. To try this for yourself, log into Bitbucket and do the following:

  1. Navigate to the repository Overview.
  2. Display the SSH URL.
    For example, ssh URL as:
    [email protected]:accountname/reponame.git
    then clone the repository using:
    git clone git@personalid:accountname/reponame.git

This refers to official solution Configure multiple SSH identities for GitBash, Mac OSX, & Linux, It works fine for me!

Solution 2 - Git

Edit your ~/.ssh/config file as following !

Host bitbucket.org-yourusername
    HostName bitbucket.org
    User yourusername
    IdentityFile ~/.ssh/yoursshkey
    IdentitiesOnly yes

Change your remote git url to have your username before '@bitbucket.org'

git remote add origin yourusername@bitbucket.org:company/app.git

or

git remote set-url origin yourusername@bitbucket.org:company/app.git    

If you have not yet cloned your repository:

git clone [email protected]:company/app.git

Solution 3 - Git

An alternative to the ~/.ssh/config method above is to specify the configuration variable core.sshCommand in the clone command itself. For example,

git clone --config core.sshCommand='ssh -i/home/username/.ssh/id_ed25519' [email protected]:the_best/awesome_repo.git

This will set the local repository configuration value and make subsequent push/pull commands 'just work'.

$ git config --local --get core.sshCommand
ssh -i/home/username/.ssh/id_ed25519

This is supported in git versions 2.10 and later.

Solution 4 - Git

After searching a lot on the web and with the community help I figured out how to configure 2 differents bitbuckets account on my Mac - MacOS Monterey.

Suppose that you have 2 bitbucket accounts witch usernames are username1 and username12.

  1. Open the terminal and create 2 ssh file2 for both usernames:

ssh-keygen -f ~/.ssh/username1-Bitbucket

ssh-keygen -f ~/.ssh/username2-Bitbucket

  1. Start ssh-agent:

eval $(ssh-agent)

  1. Create or edit the ~/.ssh/config file:

Create:

touch ~/.ssh/config

open ~/.ssh/config

Edit:

open ~/.ssh/config

The ~/.ssh/config file should look like this:

Host username1-Bitbucket
	HostName bitbucket.org
	User username1
	IdentityFile ~/.ssh/username1-Bitbucket

Host username2-Bitbucket
	HostName bitbucket.org
	User username2
	IdentityFile ~/.ssh/username2-Bitbucket

Host *
	UseKeychain yes
	AddKeysToAgent yes
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/username1-Bitbucket
	IdentityFile ~/.ssh/username2-Bitbucket
	IdentitiesOnly yes
	PreferredAuthentications keyboard-interactive,password

4. Add the keys to the ssh-agent:

ssh-add --apple-use-keychain ~/.ssh/username1-Bitbucket

ssh-add --apple-use-keychain ~/.ssh/username2-Bitbucket

  1. Copy the ssh key and add it to your bitbuckets accounts. See link:

pbcopy < ~/.ssh/username1-Bitbucket.pub

pbcopy < ~/.ssh/username1-Bitbucket.pub

  1. Check if connection to bitbucket succeeded (At least one) in order to add Bitbucket as a known hosts:

ssh -T [email protected]

  1. Clone your repositories using ssh and cd on the terminal to the repository folder.

  2. Check the reposiroties remote url:

git remote -v

You'll get information like this one:

[email protected]:companyUserName/repositoryName.git

  1. Change the repositories remote url:

git remote set-url origin username1@username1-Bitbucket:username1/repositoryName.git

git remote set-url origin username2@username2-Bitbucket:username2/repositoryName.git

So we changed [email protected]:companyUserName/repositoryName.git to username1@username1-Bitbucket:username1/repositoryName.git

git -> username1 or username2, the username of the accounts.

bitbucket -> username1-Bitbucket or username2-Bitbucket, the host alias of the config file.

companyUserName -> It's the username of the account that holds the repository, in our case the repositories are owned by username1 or username2.

repositoryName -> The name of the repository.

  1. Test your connection on both repositories creating a new branch and trying to push it:

git checkout -b newBranch

git push origin newBranch

If everything works correctly on both repositories, you are done!

Hope that this solution works for every Mac user.

That's all folk!

Solution 5 - Git

FRONT EDIT: It appears that Bitbucket has now stopped supporting the mechanism described below. As of today, I'm using the solutioon of @shannon-chou above, and it's working just fine. Remainder of post left here for historical reference. --JBC, 2019-06-23

In 2016, it appears that BitBucket added support for a somewhat simpler solution that doesn't involve extra futzing with the .ssh config file. Specifically, it's now possible to use the ssh username to indicate which account you're accessing. For instance, rather than using the git url

[email protected]:efhutton/squanzle.git

you can use the git url

[email protected]:efhutton/squanze.git

(or ssh://[email protected]/efhutton/squanze.git , which appears to be equivalent)

The basic issue is that your ssh client is going to present ssh-key identities in a fixed order. Let's say that your work account is named bobobogo, and your private one is called efhutton, and your ssh client is configured to offer the key registered with bobobogo first. If you're trying to, say, fetch on an account associated with efhutton, then your ssh client offers the bobobogo key, bitbucket accepts it, and then observes that the bobobogo account doesn't have access to the efhutton/squanze repo, and blocks you. Using the new mechanism, you're telling bitbucket that you want to use a key that's authorized for the efhutton account, and so when your ssh client presents the key registered for bobobogo, bitbucket turns it down, and your ssh client can present the next key, which is registered with the efhutton account.

Details in this blog post

https://bitbucket.org/blog/better-support-multiple-ssh-keys

EDIT: you saw the message at the top, right?

Solution 6 - Git

Please follow this Github gist for reference. https://gist.github.com/shakeeb91/cd3d3c387f339fbd93ac7388b3c885e0

1)Create Config file inside ~/.ssh/config in home directory. 2)Add a code:

Host myaccount2access
   HostName bitbucket.org
   User git
   IdentityFile /home/shakeeb/.ssh/newsshkey
   IdentitiesOnly yes
and then clone the repository.

Than run below. Make sure use your own repo details.

> git clone git@myaccount2access:shakeeb91/repository.git

Solution 7 - Git

If you receive an "ssh: Could not resolve hostname : Name or service not known" error, the following may be helpful.

As pointed out by Shannon Chou's answer, you want to create SSH aliases. GitHub, BitBucket etc. have instructions on how to do this, but I encountered one problem on Windows 10 that may help others. SSH has two different config files: a system-wide config file and a user-specific config file. The instructions I read, including Shannon Chou's, all say to add the aliases to the user-specific config file which is located at ~/.ssh/.config

In my case, I needed to add the aliases to the system-wide configuration file, which when using Git on Windows 10 is typically located here: C:\Program Files\Git\etc\ssh\ssh_config, in Git's directory.

You can determine which config file SSH is using by running this command, "myalias" can be any string" all we're interested in is the config file path that this will output:

ssh -vv myalias

OpenSSH_7.1p2, OpenSSL 1.0.2d 9 Jul 2015
debug1: Reading configuration data /etc/ssh/ssh_config

Note in the output the file path, "/etc/ssh/ssh_config". This tells us that SSH is looking for aliases there and not in the ~/.ssh/.config file.

Solution 8 - Git

In case the other comments don't work, here is what I did for my bitbucket accounts.

    Host *
         StrictHostKeyChecking=no
         UserKnownHostsFile=/dev/null

    Host nameOfSSH-Bitbucket bitbucket.org
    HostName bitbucket.org
    User myBITBUCKETUserName
    IdentityFile /Users/luisconstante/.ssh/nameOfSSH-Bitbucket

    Host nameOf2ndSSH-Bitbucket bitbucket.org
    HostName bitbucket.org
    User myBITBUCKET2ndUserName
    IdentityFile /Users/luisconstante/.ssh/nameOf2ndSSH-Bitbucket

    git remote add origin myBITBUCKETUserName@bitbucket.org:mybitbucketteam/my-cool-app.git
    git remote add origin myBITBUCKETUserName2@bitbucket.org:mybitbucketteam/my-cool-app2.git

if you dont want to input your passphrase everytime, (it may be insecure) you can create a new ssh key by leaving the password prompt empty.

I've tried UseKeychain yes but it failed. This is what worked for me.

Let me know if I'm missing something, this is complementary to the other comments.

2019

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRamBenView Question on Stackoverflow
Solution 1 - GitShannon ChouView Answer on Stackoverflow
Solution 2 - GitSudarPView Answer on Stackoverflow
Solution 3 - GitctuffliView Answer on Stackoverflow
Solution 4 - GitGastón Antonio MontesView Answer on Stackoverflow
Solution 5 - GitJohn ClementsView Answer on Stackoverflow
Solution 6 - Gitshakeeb91View Answer on Stackoverflow
Solution 7 - GitKayakinKoderView Answer on Stackoverflow
Solution 8 - GitLuis ConstanteView Answer on Stackoverflow