如何在 Linux 上禁用 SSH 主机密钥检查
在本文中,您将学习在 Linux 计算机(Ubuntu/Debian/CentOS/Fedora/Arch 以及任何其他运行 Linux 的系统)上禁用 SSH 主机密钥检查。在 SSH 主机密钥检查中,ssh 检查包含其曾经访问过的所有主机的标识的数据库。它在位于用户主目录的 ~/.ssh/known_hosts
文件中维护主机密钥。
$ ls -1 ~/.ssh/
authorized_keys
config
id_rsa
id_rsa.pub
known_hosts
当主机的标识发生更改时,ssh 客户端会发出警告并禁用密码身份验证,以确保不会发生中间人攻击或服务器欺骗。
用于控制此设置的参数是StrictHostKeyChecking。它有三个可能的值:
- yes :如果设置为“yes”,ssh 永远不会自动将主机密钥添加到
~/.ssh/known_hosts
文件中,并且会拒绝连接到主机密钥已更改的主机。 - no:当设置为“no”时,ssh 会自动将新的主机密钥添加到用户已知的主机文件中。
- ask:如果设置为“ask”(默认),只有在用户确认操作后,新的主机密钥才会添加到用户已知的主机文件中,并且 ssh 将拒绝连接到主机密钥已更改的主机。
要在 Linux 上禁用 SSH 主机密钥检查,必须将该值设置为 no 并将 UserKnownHostsFile 设置为重定向到 /dev/null。
如果您还没有 SSH 密钥,请生成它。设置密码是可选的。
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/debian/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): <optional>
Enter same passphrase again: <optional>
Your identification has been saved in /home/debian/.ssh/id_rsa.
Your public key has been saved in /home/debian/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:/2A71cIaTTuuDJ6C2gatFk5/6WAq3JyLCfppkAfdQzM debian@deb10
The key's randomart image is:
+---[RSA 2048]----+
| |
| E |
| . o o |
|. . o . |
| o . . S + o |
|o = . .. B . |
|o=o=+. .. += o |
|+.BO+.+. =o+. |
|.B=+oo..o +o. |
+----[SHA256]-----+
您本地用户的 ssh 目录是 ~/.ssh
$ ls -1 ~/.ssh
authorized_keys
id_rsa
id_rsa.pub
确保文件具有正确的权限。
for file in authorized_keys id_rsa; do
chmod 0400 ~/.ssh/${file}
done
创建本地 ssh 配置文件。
touch ~/.ssh/config
将以下设置添加到创建的配置文件中。
cat << EOF > ~/.ssh/config
Host *
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
EOF
为文件设置正确的所有权。
chmod 0400 ~/.ssh/config
您应该能够在不检查 SSH 主机密钥的情况下登录。
$ ssh [email
Warning: Permanently added '10.1.1.11' (ECDSA) to the list of known hosts.
Enter passphrase for key '/home/centos/.ssh/id_rsa':
...
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Tue Sep 17 17:35:34 2019 from 10.1.1.10
debian@deb:~$
有关 ssh 的更多信息。
- 使用 SELinux 更改 CentOS/RHEL 7/8 和 Fedora 上的 SSH 端口
- 如何在 Linux/Unix 系统中禁用 SSH 反向 DNS 查找
- 如何在 CentOS/RHEL 上为 SSH 设置两因素 (2FA) 身份验证
- 在 Linux CLI 上创建 SSH 隧道的简单方法
- 如何在 Linux/Unix 上更改或更新 SSH 密钥密码