檢查與安裝 OpenSSH
# 1. 查詢是否已安裝
rpm -qa | grep openssh
# 期望見到:openssh-server-*, openssh-clients-*, openssh-*
# 2. 未安裝時從安裝光盤安裝
mount -t iso9660 /dev/cdrom /mnt/cdrom
cd /mnt/cdrom/RedHat/RPMS/
rpm -ivh openssh-*.rpm openssh-server-*.rpm openssh-clients-*.rpm
# 若有依賴缺失,同目錄下尋找對應包安裝
啟動與驗證服務
# 啟動 sshd
service sshd start
# 或
/etc/rc.d/init.d/sshd start
# 設置開機自啟
chkconfig sshd on
# 驗證端口監聽
netstat -an | grep :22
# 輸出應含:
# tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
# tcp 0 0 :::22 :::* LISTEN
防火牆放行 (關鍵步驟)
# 臨時關閉測試 (生產環境請精確放行)
service iptables stop
# 或精確放行
iptables -I INPUT -p tcp --dport 22 -j ACCEPT
service iptables save
客戶端連接測試
- Windows:PuTTY / Xshell / SecureCRT / Windows 10+ 自帶
ssh user@ip。
- Linux:
ssh user@server_ip (需安裝 openssh-clients)。
- 本機測試:
ssh localhost。
常見問題排查
| 現象 | 可能原因 | 解決 |
| Connection refused | sshd 未啟動或端口非 22 | service sshd status、檢查 /etc/ssh/sshd_config Port |
| Connection timed out | 防火牆阻擋 / 網絡不通 | 檢查 iptables/firewalld、雲廠商安全組 |
| Permission denied (publickey,password) | 密碼錯誤 / 禁用密碼登錄 / 用戶無 Shell | 檢查 PasswordAuthentication yes、用戶 /etc/passwd Shell 欄位 |
| Host key verification failed | 服務器指紋變更 (重裝/中間人) | 刪除 ~/.ssh/known_hosts 對應行 |
安全加固建議 (/etc/ssh/sshd_config)
Port 2222 # 修改默認端口
PermitRootLogin no # 禁用 root 直接登錄
PasswordAuthentication no # 僅允許密鑰登錄
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 2
AllowUsers admin@192.168.1.* # 限制用戶及來源 IP
修改後 service sshd restart 生效。