我在尝试ssh到主机时有时会看到这个问题。最常见的是在使用git时(因为我使用git ssh的次数比直接ssh的次数多)。
ssh的输出如下:
Received disconnect from <server ip> port 22:2: Too many authentication failures
Disconnected from <server ip> port 22
我还使用了SSH代理,在这种情况下输出如下所示:
Received disconnect from UNKNOWN port 65535:2: Too many authentication failures
Disconnected from UNKNOWN port 65535
Killed by signal 1.
从错误信息的表面来看,我的账户似乎被锁定了——也就是说,如果你多次输入错误的密码,通常会发生什么。但是我只使用ssh密钥进行身份验证,而且这在几天前还有效。
查看详细输出(使用ssh -v),我没有看到更多的帮助,但它确实告诉我,它已经尝试在我的.ssh/config
文件中使用为这个主机模式配置的所有IdentityFile
,以及ssh-agent中可用的所有身份。
由于我们的季度密钥轮换,我最近向代理添加了新的密钥。
使用ssh -v
检查详细输出,查看使用了哪些键以及使用了多少键。
例如,输出可能包含以下内容:
debug1: get_agent_identities: bound agent to hostkey
debug1: get_agent_identities: agent returned 7 keys
debug1: Will attempt key: .ssh/Key-2022-Q3 ED25519 SHA256:xxxxxxxx explicit agent
debug1: Will attempt key: .ssh/Key-2022-03-14 RSA SHA256:xxxxxxxx explicit agent
debug1: Will attempt key: .ssh/Key-2023-02-13 RSA SHA256:xxxxxxxx explicit agent
debug1: Will attempt key: Key-2023-01-20 ED25519 SHA256:xxxxxxxx agent
debug1: Will attempt key: Key-2022-04-12 ED25519 SHA256:xxxxxxxx agent
debug1: Will attempt key: Key-2022-12-14 ED25519 SHA256:xxxxxxxx agent
debug1: Will attempt key: Key-2023-03-20 ED25519 SHA256:xxxxxxxx agent
在上面的示例中,我们看到ssh将尝试使用7个密钥与远程服务器进行身份验证。前3个是通过.ssh/config
文件中匹配Host模式的条目或通过-i
命令行参数显式指定的。
在我的例子中,我的.ssh/config
文件包含以下内容:
<global section>
IdentityFile .ssh/Key-2022-03-14
...
Host *.<domain match>
IdentityFile .ssh/Key-2022-Q3
...
Host <exact match>
IdentityFile .ssh/Key-2023-02-13
基本上,3个身份文件匹配这个主机的条件:
全球匹配- 子域匹配
- 精确主机匹配
ssh将尝试这三个密钥。
所有这些密钥也加载到我运行的ssh-agent
以及4个额外的密钥。
运行ssh-add -L
以获取代理将提供的密钥列表。在我的例子中,它包括上面列出的所有7个键。
这里的问题是,提供的每个不正确的密钥失败,都被视为失败的登录尝试。在这种情况下,在我们获得正确的密钥之前,将会有多次失败的登录尝试,这将导致&;Too many authentication failures&;错误。
为了解决这个问题,我们更新.ssh/config
文件,并告诉ssh只显式地使用这个密钥。我们需要将以下两行添加到确切的主机匹配部分:
IdentitiesOnly yes
IdentityAgent none
第一个命令告诉ssh只使用配置文件中指定的Identity文件,第二个命令说在使用这个主机时禁用代理:
IdentitiesOnly
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
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.
IdentityAgent
Specifies the UNIX-domain socket used to communicate with the authentication agent.
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
from the SSH_AUTH_SOCK environment variable. Otherwise if the specified value begins with a ‘$’ character, then it will be treated as an environment variable containing the location of the socket.
如果我们在做了这些更改后尝试ssh
,我们会看到提供的键列表减少到只有一个:
debug1: Will attempt key: .ssh/Key-2023-02-13 RSA SHA256:xxxxxxxx explicit
它也不再列出agent
作为这个键的来源。
第一个密钥验证成功,并被授予访问权限。