UFW 防火墙(推荐新手)
apt install ufw -y
ufw default deny incoming
ufw default allow outgoing
ufw allow 2222/tcp # SSH 端口
ufw allow 80/tcp # HTTP
ufw allow 443/tcp # HTTPS
ufw enable
ufw status 常用规则
# 允许特定 IP 访问 SSH
ufw allow from 你的IP to any port 2222 # 删除规则
ufw delete allow 80/tcp # 查看编号
ufw status numbered iptables 基础
UFW 底层使用 iptables。如需更精细控制:
# 限制 SSH 连接频率
iptables -A INPUT -p tcp --dport 2222 -m state --state NEW -m recent --set
iptables -A INPUT -p tcp --dport 2222 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 -j DROP 注意事项
- 配置防火墙前确保 SSH 端口已放行,避免被锁在外面
- 建议通过 VNC/控制台测试规则后再断开连接
相关阅读:SSH 安全加固