CentOS SSH配置中的常見問題及解決方法
無法遠程連接的首要原因是服務未運行。使用systemctl status sshd
檢查服務狀態,若未啟動,執行systemctl start sshd
啟動;若需開機自啟,執行systemctl enable sshd
。若啟動失敗,需檢查配置文件語法(sshd -t
)或系統日志(journalctl -xe -u sshd
)定位具體錯誤。
修改/etc/ssh/sshd_config
(如更改端口、禁用root登錄)后,語法錯誤會導致服務無法重啟。使用sshd -t
命令檢查配置文件語法,若有錯誤,根據提示修正(如遺漏“:”或拼寫錯誤),修正后重啟服務。
firewalld
,需開放SSH端口(默認22):firewall-cmd --permanent --add-service=ssh
,然后firewall-cmd --reload
;若使用iptables
,執行iptables -A INPUT -p tcp --dport 22 -j ACCEPT
并保存規則。enforcing
模式,臨時禁用setenforce 0
,或永久修改/etc/selinux/config
(將SELINUX=enforcing
改為permissive
),重啟系統生效。netstat -tulnp | grep 22
檢查端口是否被其他進程占用,若有,終止占用進程(kill -9 PID
)或修改SSH端口(/etc/ssh/sshd_config
中修改Port
項,需同步更新防火墻/SELinux規則)。netstat
無22端口輸出,需確認/etc/ssh/sshd_config
中ListenAddress
設置為0.0.0.0
(監聽所有接口),重啟服務。600
(chmod 600 ~/.ssh/id_rsa
),服務器端~/.ssh/authorized_keys
權限為600
,且公鑰內容正確。PasswordAuthentication no
),需確保密鑰認證正常;若啟用,檢查用戶密碼是否正確。/etc/ssh/sshd_config
,設置UseDNS no
,重啟服務,避免SSH反向解析客戶端主機名。GSSAPIAuthentication no
,減少認證延遲。/etc/ssh/sshd_config
中啟用TCPKeepAlive yes
、ClientAliveInterval 60
、ClientAliveCountMax 3
,保持連接活躍,防止超時斷開。/etc/ssh/sshd_config
中PermitRootLogin no
,需用普通用戶登錄后切換root(su -
);若需允許,設置為yes
(不推薦,降低安全性)。AllowUsers user1 user2
(/etc/ssh/sshd_config
)限制可登錄用戶,修改后重啟服務。