VPS配置防火墙规则教程2026:iptables与firewalld详解
前言
在VPS安全防护体系中,防火墙是抵御外部攻击的第一道防线。无论是防范端口扫描、DDoS攻击,还是限制恶意访问,合理配置防火墙规则都至关重要。
2026年,随着网络攻击手段的日益复杂,仅仅依靠默认的防火墙配置已经无法满足安全需求。本文将系统讲解Linux系统下两大主流防火墙工具——iptables和firewalld的配置方法,帮助您构建坚固的VPS安全屏障。
无论您是刚购买VPS的新手站长,还是负责服务器运维的工程师,掌握防火墙配置技能都是必备的基本功。
一、Linux防火墙基础概念
1.1 防火墙工作原理
Linux防火墙基于内核的netfilter框架实现,通过在网络协议栈的关键位置设置钩子(hook)来拦截和处理数据包。
数据包处理流程:
数据包到达 → PREROUTING(路由前处理) → 路由判断 →
↓
INPUT(本地进程) ← 目标为本机
↓
FORWARD(转发) ← 目标为其他主机
↓
OUTPUT(本地发出) → POSTROUTING(路由后处理) → 离开本机
1.2 iptables与firewalld的关系
| 特性 | iptables | firewalld |
|---|---|---|
| 出现时间 | 较早(1998年) | 较新(RHEL 7引入) |
| 配置方式 | 直接操作规则链 | 基于区域(zone)的抽象层 |
| 规则生效 | 立即生效,需手动保存 | 动态管理,自动保存 |
| 学习曲线 | 陡峭 | 相对平缓 |
| 适用系统 | 所有Linux发行版 | RHEL/CentOS 7+,Fedora |
| 底层实现 | 直接操作netfilter | 底层仍使用iptables/nftables |
重要说明: firewalld是iptables的前端管理工具,底层仍然调用iptables或nftables。对于Ubuntu/Debian系统,通常使用ufw(Uncomplicated Firewall)作为iptables的前端。
1.3 防火墙基本术语
- 表(Table):功能分类,包括filter(过滤)、nat(地址转换)、mangle(修改数据包)、raw(连接跟踪)
- 链(Chain):规则集合,包括INPUT、OUTPUT、FORWARD、PREROUTING、POSTROUTING
- 规则(Rule):具体的匹配条件和动作
- 目标(Target):规则匹配后的动作,如ACCEPT(允许)、DROP(丢弃)、REJECT(拒绝)
二、iptables配置详解
2.1 iptables基本命令结构
iptables [-t 表名] 命令选项 链名 匹配条件 -j 动作
常用命令选项:
- -A:在链末尾追加规则
- -I:在链开头插入规则
- -D:删除规则
- -R:替换规则
- -L:列出规则
- -F:清空规则
- -P:设置默认策略
- -N:创建自定义链
- -X:删除自定义链
2.2 实战:配置基础防火墙规则
场景:为一台Web服务器配置防火墙
#!/bin/bash
# iptables基础防火墙配置脚本
# 1. 清除现有规则
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
# 2. 设置默认策略(拒绝所有流量)
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# 3. 允许本地回环接口
iptables -A INPUT -i lo -j ACCEPT
# 4. 允许已建立和相关的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 5. 允许SSH连接(端口22)
# 警告:修改SSH端口前请确保安全,避免被锁在服务器外
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 6. 允许HTTP和HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 7. 允许Ping(ICMP)
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
# 8. 防止SYN Flood攻击
iptables -A INPUT -p tcp --syn -m limit --limit 1/s -j ACCEPT
# 9. 防止Ping Flood
iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit 1/s -j ACCEPT
# 10. 记录被拒绝的连接(可选)
iptables -A INPUT -j LOG --log-prefix "IPTABLES_DROPPED: "
# 11. 保存规则(Ubuntu/Debian)
# iptables-save > /etc/iptables/rules.v4
# 12. 保存规则(CentOS/RHEL 6)
# service iptables save
echo "防火墙规则配置完成!"
2.3 iptables规则保存与恢复
Ubuntu/Debian系统:
# 安装iptables-persistent
sudo apt-get install -y iptables-persistent
# 保存当前规则
sudo netfilter-persistent save
# 或
sudo iptables-save > /etc/iptables/rules.v4
# 恢复规则
sudo iptables-restore < /etc/iptables/rules.v4
CentOS/RHEL系统:
# RHEL 6及之前
service iptables save
chkconfig iptables on
# RHEL 7+(使用firewalld)
# 如果坚持使用iptables
systemctl stop firewalld
systemctl disable firewalld
yum install -y iptables-services
systemctl enable iptables
systemctl start iptables
service iptables save
2.4 高级iptables技巧
1. 端口范围与多端口匹配
# 允许端口范围
iptables -A INPUT -p tcp --dport 30000:31000 -j ACCEPT
# 允许多个不连续端口
iptables -A INPUT -p tcp -m multiport --dports 22,80,443,3306 -j ACCEPT
2. IP地址限制
# 允许特定IP访问SSH
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.100 -j ACCEPT
# 封锁特定IP
iptables -A INPUT -s 10.0.0.5 -j DROP
# 允许IP段
iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT
# 排除特定IP
iptables -A INPUT -s ! 203.0.113.0/24 -j DROP
3. 连接限制(防暴力破解)
# 限制SSH连接频率(每分钟最多3个新连接)
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m limit --limit 3/min -j ACCEPT
# 使用recent模块封锁暴力破解IP
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 -j DROP
4. 时间限制
# 仅允许在工作时间访问
iptables -A INPUT -p tcp --dport 80 -m time --timestart 09:00 --timestop 18:00 --weekdays Mon,Tue,Wed,Thu,Fri -j ACCEPT
三、firewalld配置详解
3.1 firewalld区域(Zone)概念
firewalld引入了"区域"的概念,不同区域对应不同的信任级别:
| 区域 | 信任级别 | 说明 |
|---|---|---|
| drop | 最低 | 丢弃所有入站流量,只允许出站 |
| block | 低 | 拒绝所有入站流量,返回icmp-host-prohibited |
| public | 中低 | 允许选定的连接,默认区域 |
| external | 中 | 用于外部网络,启用NAT伪装 |
| internal | 中高 | 用于内部网络,信任度较高 |
| dmz | 中 | 非军事区,允许有限访问 |
| work | 高 | 工作网络,信任大部分主机 |
| home | 更高 | 家庭网络,信任所有主机 |
| trusted | 最高 | 信任所有流量 |
3.2 firewalld基本操作
# 查看firewalld状态
sudo firewall-cmd --state
# 启动/停止/重启firewalld
sudo systemctl start firewalld
sudo systemctl stop firewalld
sudo systemctl restart firewalld
# 查看当前活动区域
sudo firewall-cmd --get-active-zones
# 查看默认区域
sudo firewall-cmd --get-default-zone
# 设置默认区域
sudo firewall-cmd --set-default-zone=public
# 查看指定区域的配置
sudo firewall-cmd --zone=public --list-all
3.3 使用firewalld配置防火墙
场景:配置Web服务器防火墙
# 1. 设置默认区域为public
sudo firewall-cmd --set-default-zone=public
# 2. 允许HTTP和HTTPS服务(永久生效)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
# 3. 允许SSH(默认已允许)
sudo firewall-cmd --permanent --add-service=ssh
# 4. 允许自定义端口
sudo firewall-cmd --permanent --add-port=8080/tcp
# 5. 允许特定IP访问特定端口
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="3306" accept'
# 6. 拒绝特定IP
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.5" drop'
# 7. 端口转发(将80端口转发到8080)
sudo firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080
# 8. 启用NAT伪装(用于网关)
sudo firewall-cmd --permanent --add-masquerade
# 9. 重新加载配置(使永久配置生效)
sudo firewall-cmd --reload
# 10. 验证配置
sudo firewall-cmd --list-all
3.4 富规则(Rich Rules)详解
富规则提供了更精细的控制能力:
# 语法格式
firewall-cmd --add-rich-rule='rule [family="ipv4|ipv6"] [source address="address[/mask]"] [destination address="address[/mask]"] [service name="service"] [port port="port" protocol="tcp|udp"] [icmp-block name="icmptype"] [masquerade] [forward-port port="port" protocol="tcp|udp" [to-port="port"] [to-addr="address"]] [accept|reject|drop]'
# 示例1:允许特定IP访问SSH
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.5" service name="ssh" accept'
# 示例2:拒绝特定IP访问HTTP
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="198.51.100.0/24" service name="http" drop'
# 示例3:限制连接速率
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" service name="ssh" limit value="3/m" accept'
# 示例4:记录被拒绝的连接
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" service name="http" log prefix="HTTP_DENIED: " level="warning" drop'
四、Ubuntu/Debian的UFW防火墙
4.1 UFW简介与基本配置
UFW(Uncomplicated Firewall)是Ubuntu默认的防火墙配置工具,基于iptables但提供了更简单的命令行接口。
# 启用/禁用UFW
sudo ufw enable
sudo ufw disable
# 查看状态和规则
sudo ufw status verbose
# 重置UFW(删除所有规则)
sudo ufw reset
# 设置默认策略
sudo ufw default deny incoming
sudo ufw default allow outgoing
4.2 UFW常用规则配置
# 允许SSH
sudo ufw allow 22/tcp
# 或
sudo ufw allow ssh
# 允许HTTP和HTTPS
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# 允许特定IP访问所有端口
sudo ufw allow from 192.168.1.100
# 允许IP段访问特定端口
sudo ufw allow from 192.168.1.0/24 to any port 3306
# 拒绝特定IP
sudo ufw deny from 10.0.0.5
# 删除规则(先查看编号)
sudo ufw status numbered
sudo ufw delete 1
# 插入规则到指定位置
sudo ufw insert 1 allow from 203.0.113.5 to any port 22
五、防火墙安全加固策略
5.1 防DDoS攻击策略
# iptables防DDoS规则
# 1. 限制SYN请求速率
iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j ACCEPT
# 2. 限制单个IP的新连接速率
iptables -A INPUT -p tcp -m state --state NEW -m recent --set
iptables -A INPUT -p tcp -m state --state NEW -m recent --update --seconds 60 --hitcount 20 -j DROP
# 3. 防止Ping Flood
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
# 4. 限制UDP流量(防止UDP Flood)
iptables -A INPUT -p udp -m limit --limit 10/s -j ACCEPT
# 5. 丢弃无效数据包
iptables -A INPUT -m state --state INVALID -j DROP
# 6. 限制连接数
iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 20 -j REJECT
5.2 防端口扫描策略
# 检测并记录端口扫描
iptables -N PORTSCAN
iptables -A PORTSCAN -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j LOG --log-prefix "Port Scan: "
iptables -A PORTSCAN -j DROP
# 将新连接跳转到PORTSCAN链
iptables -A INPUT -p tcp --syn -j PORTSCAN
5.3 地理位置封锁(GeoIP)
# 安装xtables-addons(包含GeoIP模块)
# Ubuntu/Debian
sudo apt-get install -y xtables-addons-common
# 下载GeoIP数据库
cd /usr/share/xt_geoip
sudo ./xt_geoip_dl
sudo ./xt_geoip_build
# 封锁特定国家(如封锁来自某国的流量)
iptables -A INPUT -m geoip --src-cc CN,RU -j DROP
六、防火墙故障排查
6.1 常见问题与解决方法
问题1:配置后无法连接服务器
原因: 误将SSH端口封锁
解决方法:
1. 通过VPS控制台的救援模式登录
2. 清除防火墙规则:iptables -F 或 ufw disable
3. 重新配置正确的规则
预防措施:
- 配置防火墙前,先添加允许SSH的规则
- 使用crontab设置定时任务,5分钟后自动清除防火墙规则(测试阶段)
- 保留一个已登录的SSH会话作为备用
问题2:规则不生效
排查步骤:
# 1. 检查规则是否正确添加
iptables -L -n -v
# 2. 检查数据包计数器(确认规则是否匹配)
iptables -L -n -v --line-numbers
# 3. 开启日志追踪
iptables -A INPUT -j LOG --log-prefix "DEBUG: "
# 4. 查看系统日志
tail -f /var/log/kern.log | grep DEBUG
# 5. 检查是否有其他防火墙服务冲突
systemctl status firewalld
systemctl status ufw
6.2 防火墙性能优化
1. 规则排序优化
将匹配频率最高的规则放在前面,减少匹配次数:
# 不好的做法:将通用规则放在前面
iptables -A INPUT -p tcp --dport 22 -j ACCEPT # SSH规则在最后
iptables -A INPUT -p tcp --dport 80 -j ACCEPT # HTTP规则在最后
# 好的做法:将高频规则提前
# 先添加已建立连接的快速通道
iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 然后添加新连接规则
2. 使用ipset提升大批量IP过滤性能
# 创建ipset集合
ipset create blacklist hash:ip
# 添加IP到集合
ipset add blacklist 192.168.1.100
ipset add blacklist 10.0.0.5
# 在iptables中使用ipset
iptables -A INPUT -m set --match-set blacklist src -j DROP
# 保存ipset
ipset save > /etc/ipset.conf
# 恢复ipset
ipset restore < /etc/ipset.conf
总结
防火墙配置是VPS安全运维的基础技能。本文详细讲解了iptables、firewalld和UFW三种防火墙工具的配置方法,以及防DDoS、防端口扫描等高级安全策略。
核心要点回顾:
1. 理解防火墙工作原理和数据包处理流程
2. 根据Linux发行版选择合适的防火墙工具
3. 遵循"默认拒绝,白名单允许"的安全原则
4. 配置规则时注意顺序,高频规则前置
5. 定期审查防火墙日志,及时发现异常
安全建议:
- 不要暴露不必要的端口
- 使用非标准端口增加攻击难度
- 配合VPS安全加固教程2026:SSH安全与防暴力破解加强整体安全
- 考虑使用云防火墙或CDN作为额外防护层
相关文章推荐
防火墙配置是VPS安全体系的重要组成部分,以下文章将帮助您构建完整的安全防护:
- VPS安全加固教程2026:SSH安全与防暴力破解 - 配合防火墙实现深度防御
- VPS监控资源使用教程2026:Zabbix/Prometheus监控搭建 - 监控防火墙日志和异常流量
- VPS搭建VPN教程2026:WireGuard/OpenVPN配置指南 - 通过防火墙配置VPN端口转发
- VPS优化网络速度教程2026:BBR加速与TCP优化 - 防火墙与网络性能调优
- VPS定时任务设置教程2026:crontab配置与自动化任务 - 自动化备份防火墙规则

评论(0)