为什么 SSH 会为 IP 地址添加 known_host 条目?

Zor*_*che 23 ssh

我有一个名为 nms.example.org 的主机。在我的/etc/ssh/ssh_known_hosts我有一个带有 RSA 密钥的主机条目。这个条目和所有其他条目都由我的配置管理系统管理。

nms.example.org ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZqfmVPs/XqTS...
Run Code Online (Sandbox Code Playgroud)

此外,我在我/etc/ssh/ssh_config的特定主机中有一个条目,用于设置主机密钥别名。如果我正确理解一切,这意味着只有nms.example.org应该重要。

Host nms.example.org nms.example nms
    HostKeyAlias nms.example.org
    HostName nms.example.org
Run Code Online (Sandbox Code Playgroud)

那么,为什么当我从客户端连接时,ssh 似乎仍然认为它需要使用主机的IP向我的每个用户 known_hosts 添加一个密钥?

$ ssh nms -v
OpenSSH_6.0p1 Debian-4+deb7u4, OpenSSL 1.0.1e 11 Feb 2013
debug1: Reading configuration data /home/zoredache/.ssh/config
debug1: /home/zoredache/.ssh/config line 61: Applying options for *
debug1: /home/zoredache/.ssh/config line 71: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 84: Applying options for nms
debug1: /etc/ssh/ssh_config line 363: Applying options for *
debug1: Connecting to nms.example.org [104.236.190.144] port 22.
debug1: Connection established.
debug1: identity file /home/zoredache/.ssh/zoredache-20140204.id_rsa type 1
...
debug1: Server host key: RSA 6b:5f:b6:e9:13:c3:b7:39:1e:ec:74:05:33:64:4d:5e
debug1: using hostkeyalias: nms.example.org
debug1: Host 'nms.example.org' is known and matches the RSA host key.
debug1: Found key in /etc/ssh/ssh_known_hosts:104
Warning: Permanently added the RSA host key for IP address '192.0.2.144' to the list of known hosts.
debug1: ssh_rsa_verify: signature correct
...
Run Code Online (Sandbox Code Playgroud)

SSH 知道我的主机是有效的(请参阅:),Host 'nms.example.org' is known and matches the RSA host key那么为什么要将 IP 的密钥添加到用户配置文件中?

这非常令人恼火,因为当我重新安装机器时,我的配置管理系统可以很好地处理收集主机密钥并将其分发给所有系统。但是,在每次使用的 known_host 文件中,会留下与 IP 相关联的冲突密钥,这些密钥会导致连接尝试时出现警告,从而阻止脚本连接。

$ ssh nms -v
OpenSSH_6.0p1 Debian-4+deb7u4, OpenSSL 1.0.1e 11 Feb 2013
...
debug1: Local version string SSH-2.0-OpenSSH_6.0p1 Debian-4+deb7u4
debug1: using hostkeyalias: nms.example.org
...
debug1: Server host key: RSA 6b:5f:b6:e9:13:c3:b7:39:1e:ec:74:05:33:64:4d:5e
debug1: using hostkeyalias: nms.example.org
debug1: Host 'nms.example.org' is known and matches the RSA host key.
debug1: Found key in /etc/ssh/ssh_known_hosts:104
Warning: the RSA host key for 'nms.example.org' differs from the key for the IP address '192.0.2.144'
Offending key for IP in /home/zoredache/.ssh/known_hosts:25
Matching host key in /etc/ssh/ssh_known_hosts:104
Are you sure you want to continue connecting (yes/no)?
Run Code Online (Sandbox Code Playgroud)

如何防止 ssh 在每个用户 known_hosts 中缓存这个 per-IP 值?或者有什么安全原因让我不得不忍受这种烦人的行为?这也让我感到沮丧,因为几个服务器有一些动态 IP 地址。我的配置管理处理 DNS 更新。但是我将这些剩余的每个 IP 主机密钥填满了我的每个用户 known_host 文件。

Gil*_*il' 29

我认为这是为了完成CheckHostIP工作。

如果此标志设置为“是”,ssh(1) 将另外检查known_hosts文件中的主机 IP 地址。这允许 ssh 检测主机密钥是否因 DNS 欺骗而更改。如果该选项设置为“否”,则不会执行检查。默认值为“是”。

如果使用此选项配置错误或受到攻击,您将获得稍微更好的诊断,但它实际上并没有以我能想到的任何方式提高安全性。

如果关闭,CheckHostIP则当您按名称连接到新主机时,SSH(从 OpenSSH 6.7p1 开始)不会记录 IP 地址。因此,将其添加到您的.ssh/config

CheckHostIP no
Run Code Online (Sandbox Code Playgroud)

Host如果您只想为特定主机(尤其是具有动态 IP 地址的主机)关闭它,则可以将其添加到某个部分。