Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

SSH 服務啟動失敗完整排查與修復指南

編輯:關於伺服器

故障現象

VPS 共用環境下,SSH 服務突然無法啟動,終端機顯示:

Starting sshd:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0777 for '/etc/ssh/ssh_host_rsa_key' are too open.
...
Could not load host key: /etc/ssh/ssh_host_rsa_key
sshd: no hostkeys available -- exiting.
[FAILED]

根因分析

SSH 金鑰檔案權限過於開放(0777),且 /var/empty/sshd 目錄擁有者非 root,導致 sshd 拒絕啟動。這通常是因為誤執行 chmod -R 777 或檔案傳輸導致屬性異常。

分階段修復方案

階段 1:修正 SSH 金鑰權限

cd /etc/ssh
chmod 600 ssh_host_rsa_key ssh_host_dsa_key ssh_host_key sshd_config
chmod 620 moduli
chmod 644 ssh_config ssh_host_rsa_key.pub ssh_host_dsa_key.pub ssh_host_key.pub
service sshd start

階段 2:修正 /var/empty/sshd 目錄權限(關鍵)

若階段 1 後出現:/var/empty/sshd must be owned by root and not group or world-writable

# 檢查現狀
ls -ld /var/empty/sshd/
# 輸出範例:drwxr-xr-x 2 vu00106 root 1024 ... /var/empty/sshd/  ← 擁有者錯誤

# 修正擁有者與權限
chown root:root /var/empty/sshd/
chmod 744 /var/empty/sshd/
service sshd restart

階段 3:驗證套件完整性(進階)

rpm -V openssh-server
# 若輸出含 .....U.. /var/empty/sshd 表示屬主異常,確認修正後重試

一鍵修復指令碼

chown -R root:root /var/empty/sshd
chmod 744 /var/empty/sshd
chmod 600 /etc/ssh/ssh_host_*_key /etc/sshd_config
chmod 644 /etc/ssh/ssh_host_*_key.pub /etc/ssh/ssh_config
chmod 620 /etc/ssh/moduli
service sshd restart

預防建議

  • 避免對 /etc/ssh 目錄執行遞迴 chmod
  • 定期備份 SSH 設定與金鑰
  • 變更 SSH 埠號並啟用金鑰認證,停用密碼登入
Copyright © Windows教程網 All Rights Reserved