78

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 How to work with multiple ssh keys, Multiple bitbucket accounts but no use

push using multiple account / multiple identity on github / bitbucket is somewhat helpful to reach up to now

Daniel Holmes
  • 1,952
  • 2
  • 17
  • 28
RamBen
  • 990
  • 1
  • 9
  • 17
  • `user.name` has nothing to do with your bitbucket account. According to the git docs: "user.name: Your full name to be recorded in any newly created commits. Can be overridden by the GIT_AUTHOR_NAME and GIT_COMMITTER_NAME environment variables. See git-commit-tree(1)." What is your issue and what are you trying to achieve? – Attila Szeremi Jan 28 '14 at 11:39
  • Did you check this one? https://gist.github.com/jexchan/2351996 – chepukha Jul 21 '14 at 03:40
  • can you please add your ".git/config" from of your projects, of course change your username and project to USERNAME and PROJECT – Tzook Bar Noy Jul 19 '15 at 11:48
  • possible duplicate of [Specify an SSH key for git push for a given domain](http://stackoverflow.com/questions/7927750/specify-an-ssh-key-for-git-push-for-a-given-domain) – Tobia Tesan Sep 23 '15 at 04:55
  • 1
    You must change remote url to include your alias name instead of 'bitbucket.org'. – kool79 Feb 12 '20 at 15:09
  • 1. dont touch user.name. It is not used for authorization. It is used only for git history and can be 2. You must change remote url to include your alias name (sfsworkdid or workdid) instead of 'bitbucket.org'. I.e change "git@bitbucket.org:my-group/my-repo.git" to "git@sfsworkid:my-group/my-repo.git" or "git@workid:my-group/my-repo.git". In this way ssh will know which config it should use. With "git@bitbucket.org" ssh will use default identity (id_rsa.pub). – kool79 Feb 12 '20 at 15:18

9 Answers9

114

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: git@bitbucket.org: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:
    git@bitbucket.org: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!

Community
  • 1
  • 1
Shannon Chou
  • 1,367
  • 1
  • 8
  • 11
  • as complement in the .ssh/config, I have to add "IdentitiesOnly yes", else there is a try with my default id_rsa on bitbucket and failed the flow because it's associated to an existing other account (on bitbucket), that is used to login. – David Bernard Oct 01 '18 at 12:33
  • 1
    The main thing which worked for me was ````For example, ssh URL as: git@bitbucket.org:accountname/reponame.git then clone the repository using: git clone git@personalid:accountname/reponame.git```` – Sunil Kumar Dec 03 '18 at 05:37
  • I don't understand the `git@alias:accountname/reponame.git` bit. What if your repository is namespaced under a Bitbucket project? Can you provide more examples? – alex Aug 30 '19 at 13:13
  • 1
    Works like a charm! if you already have a repository just edit `.git/config`, replace `bitbucket.org` domain for `alias` and you're good to go. On `[remote "origin"]` => `url = git@:/.git` – luis19mx Jul 01 '21 at 15:01
  • 2
    This bloody thing works flawlessly, thanks! – alanko Aug 18 '21 at 12:07
  • This was missing only the `User yourusername` from @SudarP s answer. Once I added it - it started working – marcinsdance Jun 01 '22 at 09:39
  • I have no words to express my gratitude. Thank you so much – Ganesh Patil Oct 04 '22 at 04:59
  • this is one hell of an explanation...this needs to be done as you cannot provide one public key to more than one bitbucket account. – Mihir Bhatt Nov 09 '22 at 13:00
21

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 yourusername@bitbucket.org:company/app.git
wal
  • 17,409
  • 8
  • 74
  • 109
SudarP
  • 906
  • 10
  • 12
  • 2
    this worked for me however with one slight modification - the `Host` field must be the same in both cases, e.g. `Host bitbucket.org` instead of `Host bitbucket.org-yourusername` – wal Jun 26 '20 at 01:30
11

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' git@bitbucket.org: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.

ctuffli
  • 3,559
  • 4
  • 31
  • 43
8

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
  1. 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 git@bitbucket.org

  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:

git@bitbucket.org: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 git@bitbucket.org: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!

Gastón Antonio Montes
  • 2,559
  • 2
  • 12
  • 15
2

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

git@bitbucket.org:efhutton/squanzle.git

you can use the git url

efhutton@bitbucket.org:efhutton/squanze.git

(or ssh://efhutton@bitbucket.org/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?

John Clements
  • 16,895
  • 3
  • 37
  • 52
  • Did this for a while until it suddenly stopped working. Switched to the old way: http://blog.developer.atlassian.com/different-ssh-keys-multiple-bitbucket-accounts/ Works perfect. – Jacquen Jun 19 '19 at 22:57
  • Per `efhutton@bitbucket.org:efhutton/squanze.git` - what if `squanze.git` is located under a Bitbucket project? Do you know what the URL look like in this case? – alex Aug 30 '19 at 13:16
2

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

0

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.

KayakinKoder
  • 3,243
  • 3
  • 23
  • 37
0

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

0

If you wants to use different identity keys for different projects. You can simply use git command and configuration as below:

  1. While cloning:

    • git clone -c "core.sshCommand=ssh -i ~/.ssh/BitbucketKey1 -F /dev/null" {your repo url}
  2. For existing repo:

    • Go to your .git folder.
    • Edit config file.
    • Add below line under [core] section
    • sshCommand = "ssh -i ~/.ssh/BitbucketKey1 -F /dev/null"
Yash Mochi
  • 769
  • 6
  • 15