【问题标题】:GitHub thinks I am first account despite using key from second account尽管使用第二个帐户的密钥,GitHub 认为我是第一个帐户
【发布时间】:2021-03-10 01:03:36
【问题描述】:

我已经设置了我的~/.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 设置中)。

我今天为我的 discord 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

【问题讨论】:

  • 检查ssh -vvv的输出,看看实际使用的是哪个键。
  • @iBug 我用我认为相关的行编辑了这个问题

标签: git macos github ssh


【解决方案1】:

问题

来自ssh_config (5)

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

可以在配置文件中指定多个身份文件;所有这些身份将被依次尝试。多个 IdentityFile 指令将添加到已尝试的身份列表中。

原来是这样的:

  • ssh 读取配置,应用Host * 配置,并将id_rsa 添加到密钥列表中。
  • 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 代理的密钥,但不会将“密​​钥搜索列表”限制在当前配置部分。 (事实上​​,没有办法这样做。)这意味着除非您从 SSH 配置文件中删除该指令,否则将尝试 id_rsa

解决方案

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

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

此外,您不需要在 Host * 部分中包含 IdentityFile ~/.ssh/id_rsa,因为此路径在 a default list 中,SSH 将在没有明确说明的情况下查找密钥。

【讨论】:

  • 你知道为什么Host alt 中的IdentitiesOnly yes 不起作用吗?
  • 我将Host * 部分移至底部,但行为仍然相同。我需要以某种方式刷新 ssh 吗?
  • @theonlygusti 在这种情况下,请尝试从Host * 部分删除IdentityFile - 您可能不需要它 - 以消除可能的干扰。我不确定 SSH 如何决定它尝试密钥的顺序。
  • 这行得通,但它仍然会为所有其他主机使用我的id_rsa 密钥吗?
  • @theonlygusti ~/.ssh/id_rsa 是几个default keys 之一,即使(尤其是在)未指定密钥时,SSH 也会尝试,因此无需担心。
猜你喜欢
  • 2012-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 2021-09-19
  • 2021-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多