【问题标题】:Use multiple SSH keys with VSTS在 VSTS 中使用多个 SSH 密钥
【发布时间】:2018-03-02 02:56:41
【问题描述】:

我试图弄清楚如何在 VSTS 中使用多个 SSH 密钥。我有两个不同的 VSTS 帐户和一个与每个帐户关联的 SSH 密钥。只要我一次只向 SSH-Agent 添加一个密钥,我就可以很好地连接到该帐户的存储库。但是,一旦我将两个密钥都添加到代理中,第二个帐户总是会登录失败,因为它将使用第一个帐户的密钥。我认为这可能是与 Windows 上的 SSH 相关的行为,但我在 OSX 上也得到了相同的行为。

>git clone ssh://account2@vs-ssh.visualstudio.com:22/DefaultCollection/_ssh/your-repo Cloning into 'your-repo'... remote: remote: Your Git command did not succeed. remote: Details: remote: Public key authentication failed. remote: fatal: Could not read from remote repository.

我的 .ssh/config 的相关部分目前如下所示。我弄乱了主机值,但我尝试过的没有任何改变。

Host account1@vs-ssh.visualstudio.com
    Hostname vs-ssh.visualstudio.com
    User account1
    IdentityFile ~/.ssh/id_account1_vsts
    AddKeysToAgent yes
    UpdateHostKeys yes

Host account2@vs-ssh.visualstudio.com
    Hostname vs-ssh.visualstudio.com
    User account2
    IdentityFile ~/.ssh/id_account2_vsts
    AddKeysToAgent yes
    UpdateHostKeys yes

【问题讨论】:

  • 如果您使用不同的主机名称怎么办?像a1和a2? git clone a2:DefaultCollection/_ssh/your-repo:这将保证实际使用您的 ssh 配置文件。
  • 从我在 SSH 和 Git 上发现的所有内容中,并没有明显看出主机是应该使用的 URL 的别名,而不仅仅是配置条目的某种唯一标识符。我的配置中有大量条目,但它们都是针对不同的主机的,所以我不必进行这种精神上的飞跃。感谢@VonC 的缺失部分。
  • 等等...我会发布它作为答案。
  • 我不确定这里的礼仪。您是第一个获得基本信息的人,但@starian chen-MSFT 是第一个给出非常清晰和完整答案的人。
  • 只是我评论的简单副本,没关系...

标签: git ssh azure-devops


【解决方案1】:

你不能使用Host这样的值,像这样替换它:

Host hostaccount1
        Hostname vs-ssh.visualstudio.com
        User account1
        IdentityFile ~/.ssh/id_account1_vsts
        AddKeysToAgent yes
        UpdateHostKeys yes

   Host hostaccount2
        Hostname vs-ssh.visualstudio.com
        User account2
        IdentityFile ~/.ssh/id_account2_vsts
        AddKeysToAgent yes
        UpdateHostKeys yes

然后,您可以像这样克隆存储库

git clone ssh://hostaccount2:22/_ssh/{repo}

git clone ssh://hostaccount1:22/_ssh/{repo}

(将account1@vs-ssh.visualstudio.com替换为hostaccount1,同hostaccount2)

【讨论】:

  • 那么...我刚刚评论了什么?
  • 这个答案中缺少一件我最近学到的东西是需要的。您还应该将 IdentitiesOnly yes 添加到每个 AzDOS 主机条目。否则可能会先使用其他主机的缓存凭据,从而导致连接错误。
猜你喜欢
  • 1970-01-01
  • 2012-07-09
  • 2014-11-29
  • 2015-05-15
  • 2020-04-10
  • 2015-11-19
  • 2015-09-19
  • 2014-04-18
  • 2020-04-09
相关资源
最近更新 更多