使用来自 ssh-agent 的指定密钥

svr*_*ist 15 ssh github ssh-agent

沿着如何告诉 git 使用哪个私钥? 我想在特定情况下使用特定的 ssh 密钥。

我的问题是,即使我指定了“-i something”,ssh 也会按照添加顺序使用来自我的 ssh-agent 的密钥。

我的具体情况:

  • 我有两个 github 用户,每个用户都有我想要的自己的密钥 - 例如通过 ssh-config - 为每个克隆指定要使用的密钥:
   Host USER1.git
     Hostname github.com
     User git
     IdentityFile ~/.ssh/USER1.id_rsa
Run Code Online (Sandbox Code Playgroud)

ssh -vt USER1.gitUSER2.id_rsa如果这是首先添加到 ssh-agent 的密钥,则仍将使用。

tti*_*sdf 13

重点是使用指令内的公钥文件IdentityFile

Host USER1.git
  User git
  HostName github.com
  IdentityFile ~/.ssh/USER1.id_rsa.pub

Host USER2.git
  User git
  HostName github.com
  IdentityFile ~/.ssh/USER2.id_rsa.pub
Run Code Online (Sandbox Code Playgroud)

如果我们在 SSH 配置中指定私钥,并且私钥已加密,则 SSH 代理将无法选择正确的密钥。

stackexchange 上的类似问题:https ://unix.stackexchange.com/a/495785/264704


dgo*_*o.a 7

我终于让它工作了:

Host USER1.git
  User git
  HostName github.com
  IdentityFile ~/.ssh/USER1.id_rsa

Host USER2.git
  User git
  HostName github.com
  IdentityFile ~/.ssh/USER2.id_rsa
Run Code Online (Sandbox Code Playgroud)
  • 缩进很重要。
  • 执行ssh-add -l并确保您的两个密钥都已添加。
    • 将每个路径复制/粘贴ssh-add -l到适当的行中~/.ssh/config以避免拼写错误。如果~/.ssh/configUSER1存在身份文件路径拼写错误,则将改用错误的密钥(USER2 的密钥)。

我在 BitBucket 得到了说明。他们应该为 GitHub 工作,因为唯一的区别是HostNamehttp : //confluence.atlassian.com/pages/viewpage.action?pageId=271943168#ConfiguringMultipleSSHIdentitiesforGitBashMacOSXLinux-CreateaSSHconfigfile

要使用代理转发使其在远程服务器上工作,请尝试下面的@stijn-hoop 建议(在本答案的评论部分)。

  • 对于您对代理转发的最后评论,请参阅此答案:http://superuser.com/questions/273037/using-the-identityfile-directive-in-ssh-config-when-agentforwarding-is-in-use (3认同)

小智 2

IdentitiesOnly yes在这些主机下面的 .ssh/config 中使用。

  • 上面的评论是不正确的——你可以从你的 ssh-agent 中选择身份。另请参阅我上面已经提到的答案,http://superuser.com/questions/273037/using-the-identityfile-directive-in-ssh-config-when-agentforwarding-is-in-use (10认同)