ssh:身份验证失败次数过多

blu*_*oon 0 git authentication ssh ssh-keys

我有时在尝试 ssh 到主机时会看到此问题。最常见的是通过 git 时(因为我使用 git 进行 ssh 的频率比直接进行 ssh 的频率更高)。

ssh 的输出如下所示:

Received disconnect from <server ip> port 22:2: Too many authentication failures
Disconnected from <server ip> port 22
Run Code Online (Sandbox Code Playgroud)

我还使用了 SSH 代理,在这种情况下,输出如下所示:

Received disconnect from UNKNOWN port 65535:2: Too many authentication failures
Disconnected from UNKNOWN port 65535
Killed by signal 1.
Run Code Online (Sandbox Code Playgroud)

从错误消息的表面来看,我的帐户似乎已被锁定 - 也就是说,如果您多次错误地输入密码,通常会发生什么情况。但我只使用 ssh 密钥进行身份验证,并且这在几天前就有效了。

查看详细输出(使用 ssh -v),我没有看到更多有帮助的信息,但它确实告诉我,它已尝试使用IdentityFile我的文件中为此主机模式配置的所有 s.ssh/config以及中可用的所有身份我的 ssh 代理。

由于我们每季度进行密钥轮换,我最近向代理添加了新密钥。

blu*_*oon 5

使用检查详细输出ssh -v并查看正在使用哪些键以及有多少个键。

\n

例如,输出可能包含以下内容:

\n
debug1: get_agent_identities: bound agent to hostkey\ndebug1: get_agent_identities: agent returned 7 keys\ndebug1: Will attempt key: .ssh/Key-2022-Q3 ED25519 SHA256:xxxxxxxx explicit agent\ndebug1: Will attempt key: .ssh/Key-2022-03-14 RSA SHA256:xxxxxxxx explicit agent\ndebug1: Will attempt key: .ssh/Key-2023-02-13 RSA SHA256:xxxxxxxx explicit agent\ndebug1: Will attempt key: Key-2023-01-20 ED25519 SHA256:xxxxxxxx agent\ndebug1: Will attempt key: Key-2022-04-12 ED25519 SHA256:xxxxxxxx agent\ndebug1: Will attempt key: Key-2022-12-14 ED25519 SHA256:xxxxxxxx agent\ndebug1: Will attempt key: Key-2023-03-20 ED25519 SHA256:xxxxxxxx agent\n
Run Code Online (Sandbox Code Playgroud)\n

在上面的示例中,我们看到 ssh 将尝试使用 7 个密钥与远程服务器进行身份验证。.ssh/config前 3 个是通过文件中与主机模式匹配的条目或通过-i命令行参数显式指定的。

\n

就我而言,我的.ssh/config文件包含以下内容:

\n
<global section>\nIdentityFile .ssh/Key-2022-03-14\n\n...\nHost *.<domain match>\n    IdentityFile .ssh/Key-2022-Q3\n\n...\nHost <exact match>\n    IdentityFile .ssh/Key-2023-02-13\n
Run Code Online (Sandbox Code Playgroud)\n

基本上,有 3 个符合该主机条件的身份文件:

\n
    \n
  1. 全球比赛
  2. \n
  3. 子域匹配
  4. \n
  5. 主机精确匹配
  6. \n
\n

ssh 将尝试所有这三个键。

\n

ssh-agent所有这些键也与 4 个附加键一起加载到我的跑步中。

\n

运行ssh-add -L以获取代理将提供的密钥列表。就我而言,它包括上面列出的所有 7 个键。

\n

这里的问题是,提供的每个不正确的密钥并失败,都会被视为失败的登录尝试。在这种情况下,在我们获得正确的密钥之前,将会有多次失败的登录尝试,这会导致“身份验证失败次数过多”错误。

\n

为了解决这个问题,我们更新.ssh/config文件并告诉 ssh 明确仅使用此密钥。我们需要将以下两行添加到精确主机匹配部分:

\n
    IdentitiesOnly yes\n    IdentityAgent none\n
Run Code Online (Sandbox Code Playgroud)\n

第一个告诉 ssh 仅使用配置文件中指定的身份文件,第二个告诉 ssh 在使用此主机时禁用代理:

\n
     IdentitiesOnly\n             Specifies that ssh(1) should only use the configured authentication identity and certificate files (either the default files, or those explicitly configured in the ssh_config files or passed on the ssh(1) command-line), even if ssh-agent(1) or a PKCS11Provider or\n             SecurityKeyProvider offers more identities.  The argument to this keyword must be yes or no (the default).  This option is intended for situations where ssh-agent offers many different identities.\n\n     IdentityAgent\n             Specifies the UNIX-domain socket used to communicate with the authentication agent.\n\n             This option overrides the SSH_AUTH_SOCK environment variable and can be used to select a specific agent.  Setting the socket name to none disables the use of an authentication agent.  If the string "SSH_AUTH_SOCK" is specified, the location of the socket will be read\n             from the SSH_AUTH_SOCK environment variable.  Otherwise if the specified value begins with a \xe2\x80\x98$\xe2\x80\x99 character, then it will be treated as an environment variable containing the location of the socket.\n
Run Code Online (Sandbox Code Playgroud)\n

如果我们在进行ssh这些更改后尝试执行以下操作,我们会看到提供的密钥列表减少到只有一个:

\n
debug1: Will attempt key: .ssh/Key-2023-02-13 RSA SHA256:xxxxxxxx explicit\n
Run Code Online (Sandbox Code Playgroud)\n

它也不再列为agent该密钥的来源。

\n

现在,第一个密钥的身份验证成功,并且授予访问权限。

\n