如何配置 SSH 使其不会自动尝试所有身份文件?

cwd*_*cwd 153 linux ssh unix authentication

我一直将我的 ssh 身份文件放在我的~/.ssh/文件夹中。我那里可能有大约 30 个文件。

当我连接到服务器时,我将指定要使用的身份文件,例如

ssh -i ~/.ssh/client1-identity client1@10.1.1.10

但是,如果我不指定身份文件,而只需使用以下内容:

ssh user123@example.com

我收到错误

user123 的身份验证失败太多

我的理解是因为如果没有指定身份文件,并且ssh可以找到身份文件,那么它会尝试所有这些。

我也明白我可以编辑~/.ssh/config文件并指定如下内容:

主机example.com
PreferredAuthentications 键盘交互,密码

以防止该连接尝试已知身份文件。

所以,我想我可以将我的身份文件移到目录之外~/.ssh/,或者我可以在配置文件中指定我想要禁用身份文件身份验证的每个主机,但是有什么方法可以告诉 SSH 默认不搜索身份文件?或者指定它将搜索的那些?

小智 154

您可以使用该IdentitiesOnly=yes选项IdentityFile(请参阅ssh_config 手册页)。这样,您可以指定它应该查找哪些文件。

在此示例中,ssh 将查看 ssh_config 文件中给出的身份 + 命令行中列出的 4 个身份(代理提供的身份将被忽略):

ssh -o IdentitiesOnly=yes \
    -o IdentityFile=id1.key \
    -o IdentityFile=id2.key \
    -i id3.key \
    -i id4.key \
    user123@example.com
Run Code Online (Sandbox Code Playgroud)

形式-i-o IdentityFile=可以互换。

在 中.ssh/config,您可以包含这样的配置:

Host example
User user123
Hostname example.com
IdentityFile ~/.ssh/id_rsa_example
IdentityFile ~/.ssh/id_rsa_example2
IdentitiesOnly yes
Run Code Online (Sandbox Code Playgroud)

  • [示例](https://gist.github.com/rubo77/e01ac25450df5521d6fa) 会很好 (7认同)
  • @DimitriosMistriotis 根据 ssh_config 手册页,可以接受:`配置选项可以由空格或可选空格分隔,并且正好是一个 '='; 后一种格式有助于避免在使用 ssh、scp 和 sftp -o 选项指定配置选项时需要引用空格。` (6认同)
  • “IdentitiesOnly”可能并不总是有效,您可能必须专门排除主机;请参阅https://superuser.com/questions/859661/how-can-i-force-ssh-to-ignore-the-identityfile-listed-in-host-for-one-specif (2认同)

chr*_*and 97

user76528 的简短回答是正确的,但我刚刚遇到了这个问题,并认为一些详细说明会很有用。如果您想知道“为什么 ssh 忽略我的身份文件配置选项”,您可能还会关心这个解决方案?

首先,与 ssh_config 中的所有其他选项不同,ssh 不使用IdentityFile它找到的第一个选项。相反,该IdentityFile选项将该文件添加到所使用的身份列表中。您可以堆叠多个IdentityFile选项,ssh 客户端将尝试所有选项,直到服务器接受一个或拒绝连接。

其次,如果您使用 ssh-agent,ssh 将自动尝试使用代理中的密钥,即使您没有在 ssh_config 的 IdentityFile(或 -i)选项中指定它们。这是您可能会收到Too many authentication failures for user错误消息的常见原因。使用该IdentitiesOnly yes选项将禁用此行为。

如果您以多个用户的身份通过 ssh 连接到多个系统,我建议将IdentitiesOnly yesssh_config 的全局部分放入,并将每个部分放入IdentityFile适当的 Host 子部分中。

  • 很好解释,谢谢。参数“IdentitiesOnly”的意思不明显是*TakeOnlyWhatIExplicitlySpecifyThenFailoverToPassword*。显然, ./ssh/id_rsa 键仍然被列出。 (8认同)
  • 将 `IdentitiesOnly yes` 放在 ssh_config 的全局部分对我来说是这样做的。谢谢! (4认同)
  • 感谢您的详细评论。我曾经使用('\'换行符)“Host * \ IdentityFile ~/.ssh/mykey”作为配置选项,起初,对于特定站点有不同的条目似乎很奇怪,例如“Hostspecial\IdentityFile” ~/.ssh/specialkey \ IdentitiesOnly yes` 继续提供 `mykey` 而不是 `specialkey`。当然还不清楚,直到我意识到(从你的答案中)IdentityFile 条目是按评估顺序堆叠的,并且将使用最后定义的条目。删除“IdentityFile ~/.ssh/mykey”解决了问题,并使用了正确的单个密钥。 (3认同)
  • 在尝试之前,我注意到我的 `git pull/push` 命令正在尝试加载到我的代理中的每个身份。这不是问题,直到有一次我有太多的钥匙。 (2认同)

slm*_*slm 33

我通常这样做:

$ ssh -o IdentitiesOnly=yes -F /dev/null -i ~/path/to/some_id_rsa root@server.mydom.com
Run Code Online (Sandbox Code Playgroud)

选项如下:

  • -o IdentitiesOnly=yes- 告诉 SSH 仅使用通过 CLI 提供的密钥,而不使用来自$HOME/.sshssh-agent 或 ssh-agent 的密钥
  • -F /dev/null - 禁止使用 $HOME/.ssh/config
  • -i ~/path/to/some_id_rsa - 您明确希望用于连接的密钥

例子

$ ssh -v -o IdentitiesOnly=yes -F /dev/null -i ~/my_id_rsa root@someserver.mydom.com
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /dev/null
debug1: Connecting to someserver.mydom.com [10.128.12.124] port 22.
debug1: Connection established.
debug1: identity file /Users/sammingolelli/my_id_rsa type 1
debug1: identity file /Users/sammingolelli/my_id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5*
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA f5:60:30:71:8c:a3:da:a3:fe:b1:6d:0b:20:87:23:e1
debug1: Host 'someserver' is known and matches the RSA host key.
debug1: Found key in /Users/sammingolelli/.ssh/known_hosts:103
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/sammingolelli/my_id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 535
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Authenticated to someserver.mydom.com ([10.128.12.124]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
Last login: Tue Dec  8 19:03:24 2015 from 153.65.219.15
someserver$
Run Code Online (Sandbox Code Playgroud)

请注意,在上面的输出中,sshmy_id_rsa通过 CLI识别了私钥,并使用它来连接到某个服务器。

特别是这些部分:

debug1: identity file /Users/sammingolelli/my_id_rsa type 1
debug1: identity file /Users/sammingolelli/my_id_rsa-cert type -1
Run Code Online (Sandbox Code Playgroud)

和:

debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/sammingolelli/my_id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 535
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这是唯一完整的解决方案。显然,`-F /dev/null` 是其他答案中缺失的部分。 (6认同)

小智 14

在您有很多密钥的情况下,您总是会遇到“身份验证失败太多”错误。如果您有密码,并且只想使用密码登录,请按以下步骤操作。

要仅使用密码身份验证而不使用公钥,并且不使用有些误导的“键盘交互”(这是一个包含密码的超集),您可以从命令行执行此操作:

ssh -o PreferredAuthentications=password user@example.com
Run Code Online (Sandbox Code Playgroud)


And*_*ewD 13

使用 IdentityFile 但继续使用 ssh-agent 以避免密码短语重新提示

公认的 using 解决方案IdentitiesOnly yes意味着您将永远无法利用 ssh-agent,从而导致在加载密钥时重复提示您输入密码。

要继续使用ssh-agent并避免“身份验证失败过多”错误,请尝试以下操作:

  1. 删除任何自动将密钥加载到ssh-agent.

  2. 添加AddKeysToAgent yes到您客户端的 ssh 配置中。这将在第一次连接时提示您输入密码,然后将密钥添加到您的代理。

  3. 使用ssh-add -D时,你会得到“太多的验证”的错误。这只是“重置”(删除)您的 ssh-agent 缓存。然后在同一会话中再次尝试连接。系统将提示您输入密码,一旦接受,它将被添加到您的代理中。由于您的代理中只有一把钥匙,您将被允许连接。然后 ssh-agent 在同一会话期间仍然存在以供将来连接以避免重新提示。

    Host ex example.com
       User joe
       HostName example.com
       PreferredAuthentications publickey,password
       IdentityFile /path/to/id_rsa
       AddKeysToAgent yes
    
    Run Code Online (Sandbox Code Playgroud)


mik*_*ini 8

ssh 客户端和 ssh 客户端ssh-agent通过 Unix 域套接字进行通信,其名称由SSH_AUTH_SOCK环境变量指定给客户端(由代理在启动时设置)。

因此,为了防止客户端的单个调用查询代理,可以将该变量显式设置为无效的值,例如空字符串;

$ SSH_AUTH_SOCK= ssh user@server
Run Code Online (Sandbox Code Playgroud)

像这样的客户端调用将无法与代理通信,并且只能将可用的身份作为 中的文件~/.ssh/或在命令行上使用 指定的任何身份提供-i给服务器。

debug1: pubkey_prepare: ssh_get_authentication_socket: Connection refused
Run Code Online (Sandbox Code Playgroud)

  • +1 **如果您需要在配置文件中进行设置,请将 `IdentityAgent` 设置为 `none`**。 (3认同)

小智 5

将此添加到~/.ssh/config文件末尾以防止对非配置服务器使用密钥:

Host *
IdentitiesOnly=yes
Run Code Online (Sandbox Code Playgroud)