學員反映「SSH 突然連不上」,紛紛猜測:「網卡壞了?」、「網路線鬆了?」、「客戶端問題?」。講師強調:排除法需有邏輯順序,不可隨意猜測。以下為標準排查流程 (SOP)。
ping 127.0.0.1
ping ::1 (IPv6)
/var/log/secure、/var/log/auth.log、/var/log/messages 近期錯誤history 或 last -n 20 確認是否有人修改 /etc/ssh/sshd_config、iptables/firewalld、SELinux 政策Port 22 改為非常用埠卻未同步防火牆/客戶端、PermitRootLogin no、PasswordAuthentication no 未設金鑰、AllowUsers/DenyUsers 誤鎖帳號# 檢查進程
ps -ef | grep sshd
# 應見到:/usr/sbin/sshd -D (主進程) + 可能的 privilege separation 子進程
# 檢查監聽埠
netstat -anp | grep sshd
ss -anp | grep sshd
# 應見到:LISTEN 0.0.0.0:22 或 :: :22、PID/程式名稱 sshd
-p PORTsystemctl restart sshd
# 或
service sshd restart
# SysVinit
/etc/init.d/sshd restart
journalctl -u sshd -n 50 --no-pager
# 或
cat /var/log/messages | grep sshd
| 常見錯誤 | 成因 | 修復 |
|---|---|---|
| Bind to port 22 failed: Address already in use | 殘留進程、其他服務佔用 22 埠 | kill -9 PID / 修改 sshd_config Port |
| Missing privilege separation directory | /var/run/sshd 或 /var/empty/sshd 缺失 | mkdir -p /var/run/sshd; restorecon -R /var/run/sshd |
| Bad configuration option: xxx | sshd_config 語法錯誤/不支援選項 | sshd -t 測試配置、修正或刪除錯誤行 |
| Permission denied (publickey,password) | 金鑰權限/擁有者錯誤、SELinux 標籤錯誤 | chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys; restorecon -R ~/.ssh |
| fatal: Cannot bind any address | ListenAddress 設定錯誤、IPv6 未啟用但設定 :: | 修正 ListenAddress 或啟用 IPv6 |