我可以为同一主机和用户使用多个 SSH 密钥吗?

szx*_*szx 4 ssh

我在 Bitbucket 上有两个帐户,每个帐户都有一个 SSH 密钥。这是我的~/.ssh/config样子:

Host bitbucket.org
  User git
  IdentityFile ~/.ssh/bitbucket/account1

Host bitbucket.org
  User git
  IdenfityFile ~/.ssh/bitbucket/account2
Run Code Online (Sandbox Code Playgroud)

问题是 SSH 代理似乎无法同时处理同一主机的两个密钥,我必须删除其中一个才能使用另一个。

有解决方法吗?

Rai*_*ain 9

看起来,Bitbucket 有一个通配符 DNS 条目;任何随机子域 ( *.bitbucket.org) 都将解析为与 相同的服务器bitbucket.org

$ host bitbucket.org
bitbucket.org has address 207.223.240.182
bitbucket.org has address 207.223.240.181
$ host random.bitbucket.org
bitbucket.org has address 207.223.240.182
bitbucket.org has address 207.223.240.181
$ host clearlynotasubdomain.bitbucket.org
bitbucket.org has address 207.223.240.182
bitbucket.org has address 207.223.240.181
Run Code Online (Sandbox Code Playgroud)

因此,您可以~/.ssh/config使用不同的主机名设置不同的条目:

Host project1.bitbucket.org
  User git
  IdentityFile ~/.ssh/bitbucket/account1

Host project2.bitbucket.org
 User git
 IdenfityFile ~/.ssh/bitbucket/account2
Run Code Online (Sandbox Code Playgroud)

事实上,我敢于猜测 Bitbucket 为这个确切的目的而设置了这个通配符。


Jaa*_*ing 8

您可以创建两个单独的 SSH 别名,即

Host bitbucket1
  Hostname bitbucket.org
  User git
  IdentityFile ~/.ssh/bitbucket/account1

Host bitbucket2
  Hostname bitbucket.org
  User git
  IdentityFile ~/.ssh/bitbucket/account2
Run Code Online (Sandbox Code Playgroud)

只要您的 IdentityFile 不是默认的(请参阅此处的问题),您就可以使用这些别名来选择要连接的帐户。