GitHub认为我是第一个帐户,尽管使用第二个帐户的密钥



我已经设置了我的~/.ssh/config文件,允许我在两个不同的帐户上使用ssh连接到GitHub:

Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
Host alt
AddKeysToAgent yes
HostName github.com
IdentityFile ~/.ssh/id_ed25519

我使用~/.ssh/id_rsa键为我的第一个GitHub帐户(id_rsa.pub粘贴在GitHub设置那里)。

我今天为我的不和谐alt生成了一个新密钥,并将id_ed25519.pub粘贴到我的alt的GitHub设置中。

现在,当我执行ssh git@github.com时,我得到(如预期的)

PTY allocation request failed on channel 0
Hi original_account! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

但是我似乎无法用新帐户进行认证,看看这个

现在,当我执行ssh git@github.com时,我得到(如预期的)

$ ssh git@alt
PTY allocation request failed on channel 0
Hi original_account! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

为什么它还认为我是original_account?甚至

$ ssh -o "IdentitiesOnly=yes" -i ~/.ssh/id_ed25519 git@alt
PTY allocation request failed on channel 0
Hi original_account! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

我如何强制git识别我想使用的两个私钥之间的区别,以便我可以在同一台笔记本电脑上通过ssh使用这两个帐户?


ssh -vvv -o "IdentitiesOnly=yes" -i ~/.ssh/id_ed25519 git@alt

显示一些行

debug1: Connecting to github.com port 22.
debug1: Connection established.
debug1: identity file /Users/theonlygusti/.ssh/id_ed25519 type 3
debug1: identity file /Users/theonlygusti/.ssh/id_ed25519-cert type -1
debug1: identity file /Users/theonlygusti/.ssh/id_rsa type 0
debug1: identity file /Users/theonlygusti/.ssh/id_rsa-cert type -1
debug1: identity file /Users/theonlygusti/.ssh/id_ed25519 type 3
debug1: identity file /Users/theonlygusti/.ssh/id_ed25519-cert type -1
...
debug1: Will attempt key: /Users/theonlygusti/.ssh/id_rsa RSA SHA256:avZRhZ1AbwN1ECfb1A/93OG8dm0ewx33pD6asJs/I2Y explicit agent
debug1: Will attempt key: /Users/theonlygusti/.ssh/id_ed25519 ED25519 SHA256:XWhJ7Gkt13Rh3Whqwf9yH/K4lL15HaoX1+cYvbxgfpY explicit agent
debug1: Will attempt key: /Users/theonlygusti/.ssh/id_ed25519 ED25519 SHA256:XWhJ7Gkt13Rh3Whqwf9yH/K4lL15HaoX1+cYvbxgfpY explicit

问题

Fromssh_config(5):

对于每个参数,将使用第一个获得的值。

可以在配置文件中指定多个身份文件;所有这些恒等式将依次进行验证。多个IdentityFile指令将添加到尝试的标识列表中。

结果是这样的:

  • ssh读取配置,应用Host *配置,并将id_rsa添加到key list
  • ssh看到Host alt部分并应用Host github.com,并将id_ed25519添加到键列表中。(AddKeysToAgent从这个部分被丢弃,虽然它没有什么区别)
  • ssh连接到git@github.com,开始逐个尝试密钥。由于id_rsa在配置中较早出现,因此首先尝试。
  • GitHub报告id_rsa的认证成功并建立SSH会话。

为什么IdentitiesOnly不能工作?

指定ssh(1)应该只使用配置的认证身份和证书文件(默认文件,或者在ssh_config文件中显式配置的文件,或者在ssh(1)命令行中传递的文件)。

如前所述,IdentitiesOnly告诉SSH忽略来自SSH代理的密钥,但不限制"密钥搜索列表";到当前配置部分。(事实上,没有办法做到这一点。)这意味着将尝试id_rsa,除非您从SSH配置文件中删除该指令。

的解决方案解决方案是将Host *部分放在.ssh/config文件的底部,这样任何特定于主机的覆盖都可以优先考虑。这也由手册页指导:

由于使用了每个参数的第一个获得的值,因此应该在文件开头附近给出更多特定于主机的声明,并在末尾给出一般默认值。

此外,您不需要在Host *部分中包含IdentityFile ~/.ssh/id_rsa,因为该路径位于缺省列表中,SSH将在没有显式规范的情况下查找密钥。

最新更新