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

随声附和 提交于 2019-11-29 20:43:26
Shannon Chou

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

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

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!

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.

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.

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

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?

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

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

And change your remote git url to have your username before '@bitbucket.org' for eg

git remote add origin yourusername@bitbucket.org:company/app.git
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!