萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux sshd 啟動報錯 "Could not load host key" 解決:ED25519 金鑰生成與修復

升級 OpenSSH 或遷移配置後啟動 sshd 服務報錯 Could not load host key: /etc/ssh/ssh_host_ed25519_key,原因是新版 OpenSSH(7.8+)默認啟用 Ed25519 主機金鑰,但舊系統缺少該算法的金鑰文件。本文提供一鍵生成、驗證與預防方案。

錯誤現場


# systemctl start sshd
# 或
/etc/init.d/sshd start

# 錯誤輸出:
Could not load host key: /etc/ssh/ssh_host_ed25519_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key   # 可能同時缺少

根因分析

  • OpenSSH 7.8+ 默認支持 Ed25519、ECDSA、RSA 三種主機金鑰
  • 舊系統 ssh-keygen -A(或安裝腳本)僅生成 RSA/DSA 金鑰
  • sshd 啟動時嘗試加載所有配置的 HostKey,缺失即報錯並拒絕啟動

一鍵修復:生成缺失金鑰


# 生成所有缺失的標準主機金鑰(RSA、ECDSA、ED25519)
ssh-keygen -A

# 或單獨生成 ED25519
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""

# 單獨生成 ECDSA
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ""

# 驗證生成結果
ls -l /etc/ssh/ssh_host_*_key
# 應包含:
# ssh_host_rsa_key
# ssh_host_ecdsa_key
# ssh_host_ed25519_key
# (DSA 已棄用,不建議生成)

驗證修復


# 重啟服務
systemctl restart sshd
# 或
service sshd restart

# 檢查狀態
systemctl status sshd

# 測試連接
ssh -o HostKeyAlgorithms=ssh-ed25519 localhost
# 應成功建立連接(首次需接受指紋)

配置文件優化(/etc/ssh/sshd_config)

建議顯式聲明啟用的金鑰類型,避免未來算法變更導致啟動失敗:


# 明確指定主機金鑰文件(按優先級排序)
HostKey /etc/ssh/ssh_host_ed25519_key
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key

# 禁用弱算法
# HostKey /etc/ssh/ssh_host_dsa_key  # DSA 已棄用,勿啟用

# 算法優化(現代兼容性平衡)
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes128-ctr
MACs hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,umac-128-etm@openssh.com
HostKeyAlgorithms ssh-ed25519,ssh-rsa,ecdsa-sha2-nistp256

權限修復(關鍵:金鑰文件權限嚴格)


chmod 600 /etc/ssh/ssh_host_*_key
chmod 644 /etc/ssh/ssh_host_*_key.pub
chown root:root /etc/ssh/ssh_host_*_key*
restorecon -R /etc/ssh/  # SELinux 環境恢復上下文

檢查當前支持的算法


# 查看服務端提供的主機金鑰算法
ssh -Q HostKeyAlgorithms

# 查看客戶端支持的算法
ssh -Q key

# 測試特定算法連接
ssh -o HostKeyAlgorithms=ssh-ed25519 user@host

自動化修復腳本(納入初始化流程)


#!/bin/bash
# /usr/local/bin/fix_sshd_hostkeys.sh
# 確保所有標準主機金鑰存在

set -euo pipefail

KEYS=(
    "rsa:/etc/ssh/ssh_host_rsa_key"
    "ecdsa:/etc/ssh/ssh_host_ecdsa_key"
    "ed25519:/etc/ssh/ssh_host_ed25519_key"
)

for entry in "${KEYS[@]}"; do
    algo="${entry%%:*}"
    path="${entry##*:}"
    if [[ ! -f "$path" ]]; then
        echo "Generating missing $algo host key: $path"
        ssh-keygen -t "$algo" -f "$path" -N "" -q
    fi
done

# 修復權限
chmod 600 /etc/ssh/ssh_host_*_key
chmod 644 /etc/ssh/ssh_host_*_key.pub
chown root:root /etc/ssh/ssh_host_*_key*

# 重啟服務
systemctl reload sshd 2>/dev/null || systemctl restart sshd

echo "Host keys verified and service reloaded."

chmod +x /usr/local/bin/fix_sshd_hostkeys.sh
# 可加入啟動腳本或 Ansible playbook

常見誤區

誤區正確認知
用 ssh-keygen -t dsa 生成 ed25519 金鑰算法不匹配,DSA 已棄用不安全
刪除報錯的 HostKey 配置行治標不治本,降低安全性,應生成金鑰
以為是客戶端金鑰問題這是服務端主機金鑰,與用戶 ~/.ssh/id_* 無關

執行 ssh-keygen -A 或針對性生成缺失的 Ed25519/ECDSA 金鑰,配合權限修復與 sshd_config 顯式聲明,即可徹底解決啟動報錯。建議將金鑰生成納入服務器初始化標準流程(Ansible/Kickstart/Cloud-init),避免重複踩坑。

copyright © 萬盛學電腦網 all rights reserved