修改 SSH 監聽端口是基礎安全加固措施,能有效避開自動化掃描腳本,減少日誌噪音與暴力破解嘗試。
vi /etc/ssh/sshd_config
# 找到 #Port 22,取消註釋並修改為非常用端口(1024-65535)
Port 8284
# 建議保留 22 端口一段時間過渡,確認新端口正常後再刪除
# Port 22
systemctl restart sshd
# 或
service sshd restart
# 查看監聽端口
netstat -antp | grep sshd
ss -antp | grep sshd
# 客戶端連接測試
ssh -p 8284 user@server_ip
# firewalld (CentOS 7+)
firewall-cmd --permanent --add-port=8284/tcp
firewall-cmd --reload
# iptables
iptables -A INPUT -p tcp --dport 8284 -j ACCEPT
service iptables save
# 查看 SSH 端口上下文
semanage port -l | grep ssh_port_t
# 添加新端口
semanage port -a -t ssh_port_t -p tcp 8284
編輯 ~/.ssh/config:
Host myserver
HostName server_ip
User username
Port 8284
IdentityFile ~/.ssh/id_rsa
此後直接 ssh myserver 即可連接。
修改 /etc/ssh/sshd_config 中 Port 指令、重啟服務、同步防火牆/SELinux、更新客戶端配置,四步完成 SSH 端口遷移,顯著降低自動化攻擊面。