香港服务器防火墙配置指南:iptables与firewalld实战教程及安全加固
防火墙是香港服务器安全第一道防线,能有效阻挡扫描、爆破、DDoS等攻击。香港服务器位于国际网络枢纽,CN2 GIA线路虽低延迟,但暴露公网风险更高。本文围绕CentOS/Ubuntu系统,详细科普iptables与firewalld配置方法,提供Web、数据库、邮件等常见场景规则,并结合香港服务器免备案、高稳定性特点,给出最佳实践。掌握这些,您可在交付后10分钟内完成安全加固,避免99%常见入侵。
防火墙基础:为什么香港服务器必须配置
裸服上线即被扫描:SSH 22端口每分钟数百次爆破。香港服务器免备案、隐私保护,吸引黑灰产关注。防火墙核心作用:
- 端口控制:仅开放业务端口,隐藏管理入口。
- IP限制:白名单仅允许办公IP登录。
- 速率限制:防SYN洪水、CC攻击。
- 日志审计:记录异常,便于溯源。
后浪云香港服务器交付时已完成一次性安全设置(端口优化、防火墙规则、账号加固),但业务变化需自行调整。
iptables vs firewalld:选型对比
| 特性 | iptables | firewalld | 推荐场景 |
|---|---|---|---|
| 系统支持 | 所有Linux | CentOS7+/Ubuntu18+ | iptables老系统 |
| 配置复杂度 | 中等 | 简单(zone) | firewalld新手 |
| 动态规则 | 需脚本 | 原生支持 | firewalld业务多变 |
| 性能开销 | 低 | 略高 | iptables高并发 |
香港服务器多为独立服务器,推荐CentOS8+用firewalld,Ubuntu用ufw(后文附)。
交付后立即执行:基础安全加固
登录香港服务器后,先执行:
# 1. 更新系统 yum update -y # CentOS apt update && apt upgrade -y # Ubuntu # 2. 修改SSH端口(避开22) sed -i 's/#Port 22/Port 2222/' /etc/ssh/sshd_config systemctl restart sshd # 3. 禁用root远程登录 sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config systemctl restart sshd # 4. 创建普通用户 useradd adminuser passwd adminuser usermod -aG wheel adminuser # CentOS usermod -aG sudo adminuser # Ubuntu
firewalld快速上手(CentOS)
# 安装 yum install firewalld -y systemctl start firewalld systemctl enable firewalld # 查看状态 firewall-cmd --state # 默认zone firewall-cmd --set-default-zone=public
常见业务防火墙规则配置
Web服务器(Nginx/Apache)
firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --permanent --add-port=80/tcp firewall-cmd --permanent --add-port=443/tcp firewall-cmd --reload
仅办公IP访问管理后台:
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="公司IP/32" port port="8080" protocol="tcp" accept' firewall-cmd --reload
数据库(MySQL/MariaDB)
默认仅本地访问:
# 绑定127.0.0.1 sed -i 's/bind-address.*/bind-address = 127.0.0.1/' /etc/my.cnf systemctl restart mysqld
需远程:
firewall-cmd --permanent --add-port=3306/tcp firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="应用服务器IP" port port="3306" protocol="tcp" accept' firewall-cmd --reload
邮件服务器(Postfix)
firewall-cmd --permanent --add-service=smtp firewall-cmd --permanent --add-service=smtps firewall-cmd --permanent --add-service=submission firewall-cmd --reload
自定义应用(Node.js 3000端口)
firewall-cmd --permanent --add-port=3000/tcp firewall-cmd --reload
高级防护:防爆破与DDoS
SSH防爆破(fail2ban)
yum install fail2ban -y cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local sed -i 's/bantime = 10m/bantime = 1h/' /etc/fail2ban/jail.local sed -i 's/maxretry = 5/maxretry = 3/' /etc/fail2ban/jail.local systemctl start fail2ban systemctl enable fail2ban
速率限制(iptables)
# 限制新连接 iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 50 -j DROP iptables -A INPUT -p tcp --dport 80 -m state --state NEW -m recent --set iptables -A INPUT -p tcp --dport 80 -m state --state NEW -m recent --update --seconds 60 --hitcount 20 -j DROP
保存规则:
service iptables save
DDoS初步防御
香港服务器无硬件防御,建议:
- 联系后浪云客服升级高防IP。
- 前端加Cloudflare免费CDN。
- SYN Cookies:
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
iptables完整配置脚本(兼容香港服务器)
#!/bin/bash # 适用于CentOS香港服务器 iptables -F iptables -X iptables -Z # 策略 iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT # 建立链 iptables -N SYN_FLOOD # 允许loopback iptables -A INPUT -i lo -j ACCEPT # 已建立连接 iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # SSH(改2222端口) 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 iptables -A INPUT -p tcp --dport 2222 -j ACCEPT # Web iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp --dport 443 -j ACCEPT # 数据库(仅应用服务器) iptables -A INPUT -p tcp -s 应用服务器IP --dport 3306 -j ACCEPT # ICMP(限速) iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/second -j ACCEPT # 防SYN iptables -A SYN_FLOOD -m limit --limit 100/second --limit-burst 200 -j RETURN iptables -A SYN_FLOOD -j DROP iptables -A INPUT -p tcp --syn -j SYN_FLOOD # 记录丢弃 iptables -A INPUT -j LOG --log-prefix "IPTABLES-DROP: " --log-level 7 iptables -A INPUT -j DROP # 保存 service iptables save
保存为setup_fw.sh,chmod +x后执行。
监控与维护:防火墙长期有效
- 查看规则:firewall-cmd --list-all
- 日志分析:tail -f /var/log/messages | grep DROP
- 自动备份:crontab每周备份iptables规则
- 7x24支持:后浪云工单快速响应
后浪云香港服务器:安全部署优选
后浪云香港服务器支持CentOS/Ubuntu/Windows,CN2 GIA线路,免备案交付。访问后浪云香港独立服务器,推荐安全配置:
- 入门建站:E3-1230 v2 16GB/240GB SSD+1TB HDD/30Mbps BGP,450元/月(12h试用)。交付即含防火墙规则。
- 企业应用:双E5-2660 32GB/10Mbps CN2 GIA,1150元/月。测试IP:154.39.251.254。
- 高防需求:百兆服务器,3180元起,联系客服加防御。
总之,合理配置防火墙,香港服务器可实现企业级安全。立即访问香港服务器,开启安全之旅!
版权声明:
作者:后浪云
链接:https://idc.net/help/442227/
文章版权归作者所有,未经允许请勿转载。
THE END
