为什么RHEL8系统在丢失SSH主机密钥时不自动生成?

Jar*_*era 4 ssh ssh-keys cloud-init rhel8

在 RHEL 8 及更早版本上,通常情况下,SSH 主机密钥在丢失时/etc/ssh由服务自动生成sshd。通常应该有:

/etc/ssh/ssh_host_ecdsa_key
/etc/ssh/ssh_host_ecdsa_key.pub
/etc/ssh/ssh_host_ed25519_key
/etc/ssh/ssh_host_ed25519_key.pub
/etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_rsa_key.pub
Run Code Online (Sandbox Code Playgroud)

甚至重新启动节点systemctl restart sshd就足够了。

但从次要版本 RHEL 8.7 开始,这可能不再起作用,并且sshd崩溃会抱怨日志日志中缺少主机密钥。为什么?我该如何解决这个问题?

Jar*_*era 9

sshd服务默认调用sshd-keygen.target,它检查/etc/ssh目录中主机密钥的可用性并在丢失时生成它。

然而,这个众所周知的功能可能会被新版本的cloud-init. 截至cloud-init-22.1-5.el8.noarch有新文件:

/etc/systemd/system/sshd-keygen@.service.d/disable-sshd-keygen-if-cloud-init-active.conf
Run Code Online (Sandbox Code Playgroud)

内容:

# In some cloud-init enabled images the sshd-keygen template service may race
# with cloud-init during boot causing issues with host key generation.  This
# drop-in config adds a condition to sshd-keygen@.service if it exists and
# prevents the sshd-keygen units from running *if* cloud-init is going to run.
#
[Unit]
ConditionPathExists=!/run/systemd/generator.early/multi-user.target.wants/cloud-init.target
Run Code Online (Sandbox Code Playgroud)

因此,当您使用时,cloud-init您现在有 2 个选择:

  1. 手动生成主机密钥ssh-keygen -A(请参阅如何更改 SSH 主机密钥?了解更多详细信息和选项。
  2. 评论条件

只需将#标志放在前面即可ConditionPathExists...

[Unit]
#ConditionPathExists=!/run/systemd/generator.early/multi-user.target.wants/cloud-init.target
Run Code Online (Sandbox Code Playgroud)

然后使用 重新加载 systemd 配置systemctl daemon-reload。通常的行为应该再次发挥作用。