為什麼RHEL8系統遺失SSH主機金鑰時不自動產生?

為什麼RHEL8系統遺失SSH主機金鑰時不自動產生?

在 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

甚至重新啟動節點systemctl restart sshd就夠了。

但從次要版本 RHEL 8.7 開始,這可能不再起作用,並且sshd崩潰會抱怨日誌日誌中缺少主機金鑰。為什麼?我該如何解決這個問題?

答案1

sshd服務預設調用sshd-keygen.target,它檢查/etc/ssh目錄中主機密鑰的可用性並在丟失時生成它。

然而,這個眾所周知的功能可能會被新版本的cloud-init.截至cloud-init-22.1-5.el8.noarch有新文件:

/etc/systemd/system/[email protected]/disable-sshd-keygen-if-cloud-init-active.conf

內容:

# 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 [email protected] 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

因此,當您使用時,cloud-init您現在有 2 個選擇:

  1. 手動產生主機金鑰ssh-keygen -A(請參閱如何更改 SSH 主機金鑰?了解更多詳細資訊和選項。
  2. 評論條件

只需將#標誌放在前面即可ConditionPathExists...

[Unit]
#ConditionPathExists=!/run/systemd/generator.early/multi-user.target.wants/cloud-init.target

然後使用 重新載入 systemd 設定systemctl daemon-reload。通常的行為應該再次發揮作用。

答案2

如果您使用的是 cloud-init,則可以透過在 cloud-init 設定檔的「cloud_init_modules」部分中新增「ssh」模組來修復它。參考雲端初始化文檔

這將在首次引導期間產生 ssh 主機金鑰。

您可以在遇到問題的情況下進行測試:

cloud-init config file: /etc/cloud/cloud.cfg
Check if you have 'ssh' module in "cloud_init_modules" section
Run this command to verify cloud-init action. NOTE: This will REBOOT your instance and run the cloud-init action from the scratch.
# cloud-init clean --reboot

驗證 /etc/ssh/ 目錄中的 ssh 主機金鑰和 sshd 服務狀態。

答案3

嘗試在 cloud.cfg 中明確設定 ssh 金鑰類型

resize_rootfs_tmp: /dev
ssh_deletekeys:   1
ssh_genkeytypes: ['rsa', 'ecdsa', 'ed25519']

相關內容