git 如何在同一系统中使用多个 ssh 密钥维护多个 bitbucket 帐户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21139926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:35:19  来源:igfitidea点击:

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

gitbitbucket

提问by RamBen

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.puband I configured the configof git as

我有多个 Git 帐户,一个是我个人使用的,一个是供公司使用的。两个帐户源都需要从我的笔记本电脑激活。在这里,我生成了两个 ssh 密钥,如 id_rsa.pub,id_benwork_rsa.pub,并将git的配置配置

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

所以这是我的问题:在推送到任何 repo git 时询问第一个 ssh_key 密码。每次我将 git config 中的 user.name 更改为git config user.name "mybitbucketusername"。所以请告诉我如何在同一系统中使用多个 ssh 密钥维护多个 git 帐户

I tried How to work with multiple ssh keys, Multiple bitbucket accountsbut no use

我尝试了如何使用多个 ssh 密钥多个 bitbucket 帐户但没有用

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

在 github/bitbucket 上使用多个帐户/多个身份进行推送对达到现在有些帮助

回答by Shannon Chou

Create multiple identities for Mac OSX, GitBash, and Linux

为 Mac OSX、GitBash 和 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 目录的内容。默认身份文件显示为 id_encrypt 和 id_encrypt.pub 对。加密值为 rsa 或 dsa。使用 ssh-keygen 命令创建新身份。在下面的示例中,身份被命名为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.

如果您有多个 Bitbucket 账户,则需要为每个账户生成一个新的公私钥对。

Create a SSH config file

创建 SSH 配置文件

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:

当您有多个身份文件时,创建一个 SSH 配置文件机制来为您的各种身份创建别名。您可以使用许多参数和不同的方法来构建 SSH 配置文件。本示例中使用的别名条目的格式为:

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

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

要为两个身份(workid 和personalid)创建配置文件,您将执行以下操作:

  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.

  1. 打开终端窗口。
  2. 编辑 ~/.ssh/config 文件。如果您没有配置文件,请创建一个。
  3. 为每个身份组合添加别名,例如:

    Host workid
     HostName bitbucket.org
     IdentityFile ~/.ssh/workid
    Host personalid
     HostName bitbucket.org
     IdentityFile ~/.ssh/personalid
    
  4. 关闭并保存文件。

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

现在,您可以用别名替换部分存储库 URL 地址,如下所示: [email protected]:accountname/reponame.git -> git@alias:accountname/reponame.git

Load each key into the appropriate Bitbucket account

将每个密钥加载到相应的 Bitbucket 帐户中

enter image description here

在此处输入图片说明

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

确保 ssh-agent 正在运行并加载了您的密钥

enter image description here

在此处输入图片说明

Clone a repository using SSH and your alias configuration

使用 SSH 和您的别名配置克隆存储库

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:

要使用您配置的多个 SSH 身份之一克隆存储库,请克隆该存储库并在 SSH URL 中使用您的别名。要亲自尝试,请登录 Bitbucket 并执行以下操作:

  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
  1. 导航到存储库概览。
  2. 显示 SSH URL。
    例如,ssh URL 为:
    [email protected]:accountname/reponame.git
    然后使用以下命令克隆存储库:
    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!

这是指官方的解决方案Configure multiple SSH identity for GitBash, Mac OSX, & Linux,对我来说很好用!

回答by ctuffli

An alternative to the ~/.ssh/configmethod above is to specify the configuration variable core.sshCommandin the clone command itself. For example,

上述~/.ssh/config方法的替代方法是core.sshCommand在 clone 命令本身中指定配置变量。例如,

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.

这在 git 版本 2.10 及更高版本中受支持。

回答by John Clements

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

正面编辑:Bitbucket 现在似乎已停止支持下面描述的机制。截至今天,我正在使用上面@shannon-chou 的解决方案,它运行良好。剩下的帖子留在这里供历史参考。--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

在 2016 年,BitBucket 似乎增加了对更简单的解决方案的支持,该解决方案不涉及对 .ssh 配置文件的额外检查。具体来说,现在可以使用 ssh 用户名来指示您正在访问的帐户。例如,而不是使用 git url

[email protected]:efhutton/squanzle.git

[email protected]:efhutton/squanzle.git

you can use the git url

你可以使用 git url

[email protected]:efhutton/squanze.git

[email protected]:efhutton/squanze.git

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

(或 ssh://[email protected]/efhutton/squanze.git ,这似乎是等效的)

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 isregistered with the efhutton account.

基本问题是您的 ssh 客户端将以固定顺序显示 ssh 密钥身份。假设您的工作帐户名为 bobobogo,您的私人帐户名为 efhutton,并且您的 ssh 客户端配置为首先提供在 bobobogo 注册的密钥。例如,如果您尝试获取与 efhutton 关联的帐户,则您的 ssh 客户端提供 bobobogo 密钥,bitbucket 接受它,然后观察到 bobobogo 帐户无权访问 efhutton/squanze 存储库,并且阻止你。使用新机制,您告诉 bitbucket 您要使用为 efhutton 帐户授权的密钥,因此当您的 ssh 客户端提供为 bobobogo 注册的密钥时,bitbucket 拒绝它,而您的 ssh 客户端可以提供下一个关键的,这注册了efhutton账户。

Details in this blog post

这篇博文中的详细信息

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

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

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

编辑:您看到顶部的消息,对吗?

回答by Luis Constante

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

如果其他评论不起作用,这是我为我的 bitbucket 帐户所做的。

    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 [email protected]:mybitbucketteam/my-cool-app.git
    git remote add origin [email protected]: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.

如果您不想每次都输入密码(这可能不安全),您可以通过将密码提示留空来创建一个新的 ssh 密钥。

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

我试过了,UseKeychain yes但失败了。这对我有用。

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

如果我遗漏了什么,请告诉我,这是对其他评论的补充。

2019

2019年

回答by SudarP

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

编辑你的 ~/.ssh/config 文件如下!

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 url 以在“@bitbucket.org”之前使用您的用户名,例如

git remote add origin [email protected]:company/app.git

回答by shakeeb91

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

请遵循此 Github 要点以供参考。 https://gist.github.com/shakeeb91/cd3d3c387f339fbd93ac7388b3c885e0

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

1) 在主目录的 ~/.ssh/config 中创建配置文件。2)添加代码:

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

git clone git@myaccount2access:shakeeb91/repository.git

回答by KayakinKoder

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

如果您收到“ssh:无法解析主机名:名称或服务未知”错误,以下内容可能会有所帮助。

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-specificconfig file which is located at ~/.ssh/.config

正如 Shannon Chou 的回答所指出的,您想创建 SSH aliases。GitHub、BitBucket 等有关于如何执行此操作的说明,但我在 Windows 10 上遇到了一个可能对其他人有帮助的问题。SSH 有两个不同的配置文件:一个系统范围的配置文件和一个用户特定的配置文件。我阅读的说明,包括 Shannon Chou 的说明,都说要将别名添加到位于 ~/.ssh/.config的用户特定配置文件中

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

就我而言,我需要将别名添加到系统范围的配置文件中,在 Windows 10 上使用 Git 时,该文件通常位于:C:\Program Files\Git\etc\ssh\ssh_config,在 Git 的目录中。

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 正在使用哪个配置文件,“myalias”可以是任何字符串”我们感兴趣的是它将输出的配置文件路径:

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.

请注意输出中的文件路径“/etc/ssh/ssh_config”。这告诉我们 SSH 正在那里寻找别名,而不是在 ~/.ssh/.config 文件中。