iptables与firewalld防火墙配置完全指南
1. 防火墙基础概念
1.1 什么是防火墙
防火墙是一种网络安全设备,用于监控和控制网络流量,根据预定义的安全规则来允许或阻止数据包通过。Linux系统中主要有两种防火墙解决方案:iptables和firewalld。
1.2 iptables vs firewalld
- • iptables:传统的Linux防火墙工具,直接操作内核的netfilter框架
- • firewalld:动态防火墙管理器,提供更高级的抽象和动态配置能力
2. iptables详解
2.1 iptables基本概念
2.1.1 表(Tables)
2.1.2 链(Chains)
- • POSTROUTING:在路由决策后处理数据包
2.1.3 目标(Targets)
2.2 iptables基本语法
iptables [-t table] -[ADI] chain rule-specification
iptables [-t table] -[FLZ] [chain]
iptables [-t table] -N chain
iptables [-t table] -X [chain]
iptables [-t table] -P chain target
2.3 iptables常用命令
2.3.1 查看规则
# 查看所有规则
iptables -L -n -v
# 查看特定表的规则
iptables -t nat -L -n -v
# 查看规则编号
iptables -L INPUT --line-numbers
2.3.2 添加规则
# 允许SSH连接
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许HTTP和HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许特定IP访问
iptables -A INPUT -s 192.168.1.100 -j ACCEPT
# 允许本地回环
iptables -A INPUT -i lo -j ACCEPT
# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
2.3.3 删除规则
# 删除特定规则
iptables -D INPUT -p tcp --dport 80 -j ACCEPT
# 按行号删除
iptables -D INPUT 3
# 清空所有规则
iptables -F
iptables -X
iptables -Z
2.3.4 设置默认策略
# 设置默认拒绝策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
2.4 iptables高级配置
2.4.1 端口范围和多端口
# 端口范围
iptables -A INPUT -p tcp --dport 3000:3010 -j ACCEPT
# 多端口
iptables -A INPUT -p tcp -m multiport --dports 80,443,8080 -j ACCEPT
2.4.2 时间限制
# 只在工作时间允许访问
iptables -A INPUT -p tcp --dport 22 -m time --timestart 09:00 --timestop 18:00 -j ACCEPT
2.4.3 连接限制
# 限制并发连接数
iptables -A INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j REJECT
# 限制连接速率
iptables -A INPUT -p tcp --dport 22 -m limit --limit 5/min --limit-burst 10 -j ACCEPT
2.4.4 NAT配置
# SNAT(源地址转换)
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
# DNAT(目标地址转换)
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.100:8080
2.5 iptables规则持久化
2.5.1 CentOS/RHEL系统
# 保存规则
service iptables save
# 或者手动保存
iptables-save > /etc/sysconfig/iptables
# 恢复规则
iptables-restore < /etc/sysconfig/iptables
2.5.2 Ubuntu/Debian系统
# 安装iptables-persistent
apt-get install iptables-persistent
# 保存规则
netfilter-persistent save
# 恢复规则
netfilter-persistent reload
3. firewalld详解
3.1 firewalld基本概念
3.1.1 区域(Zones)
3.1.2 服务(Services)
预定义的服务配置,包含端口、协议等信息。
3.1.3 富规则(Rich Rules)
提供更复杂的规则配置语法。
3.2 firewalld基本命令
3.2.1 服务管理
# 启动firewalld
systemctl start firewalld
# 停止firewalld
systemctl stop firewalld
# 重启firewalld
systemctl restart firewalld
# 查看状态
systemctl status firewalld
firewall-cmd --state
3.2.2 区域管理
# 查看默认区域
firewall-cmd --get-default-zone
# 设置默认区域
firewall-cmd --set-default-zone=public
# 查看活动区域
firewall-cmd --get-active-zones
# 查看所有区域
firewall-cmd --get-zones
# 查看区域信息
firewall-cmd --zone=public --list-all
3.2.3 服务管理
# 查看可用服务
firewall-cmd --get-services
# 查看已开放的服务
firewall-cmd --list-services
# 添加服务
firewall-cmd --add-service=http
firewall-cmd --add-service=https
# 删除服务
firewall-cmd --remove-service=http
# 永久添加服务
firewall-cmd --permanent --add-service=http
3.2.4 端口管理
# 添加端口
firewall-cmd --add-port=8080/tcp
# 删除端口
firewall-cmd --remove-port=8080/tcp
# 查看开放端口
firewall-cmd --list-ports
# 永久添加端口
firewall-cmd --permanent --add-port=8080/tcp
3.3 firewalld高级配置
3.3.1 自定义服务
# 创建自定义服务配置文件
cat > /etc/firewalld/services/myapp.xml << EOF
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>MyApp</short>
<description>My Application Service</description>
<port protocol="tcp" port="8080"/>
<port protocol="tcp" port="8443"/>
</service>
EOF
# 重新加载配置
firewall-cmd --reload
# 添加自定义服务
firewall-cmd --add-service=myapp
3.3.2 富规则配置
# 允许特定IP访问特定端口
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="22" accept'
# 限制连接速率
firewall-cmd --add-rich-rule='rule service name="ssh" limit value="10/m" accept'
# 阻止特定IP
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.200" drop'
# 端口转发
firewall-cmd --add-rich-rule='rule family="ipv4" forward-port port="80" protocol="tcp" to-port="8080"'
3.3.3 接口绑定
# 将接口绑定到区域
firewall-cmd --zone=internal --add-interface=eth1
# 查看接口绑定
firewall-cmd --get-zone-of-interface=eth1
# 更改接口区域
firewall-cmd --zone=public --change-interface=eth1
3.3.4 IP伪装和端口转发
# 启用IP伪装
firewall-cmd --add-masquerade
# 端口转发
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.100
# 查看转发规则
firewall-cmd --list-forward-ports
4. 实战配置案例
4.1 Web服务器防火墙配置
4.1.1 iptables配置
#!/bin/bash
# Web服务器防火墙配置脚本
# 清空现有规则
iptables -F
iptables -X
iptables -Z
# 设置默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# 允许本地回环
iptables -A INPUT -i lo -j ACCEPT
# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许SSH(限制连接数)
iptables -A INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j REJECT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许HTTP和HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许FTP
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -j ACCEPT
# 允许DNS
iptables -A INPUT -p udp --dport 53 -j ACCEPT
# 保存规则
service iptables save
4.1.2 firewalld配置
#!/bin/bash
# Web服务器防火墙配置脚本
# 设置默认区域
firewall-cmd --set-default-zone=public
# 添加服务
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=ftp
# 限制SSH连接
firewall-cmd --permanent --add-rich-rule='rule service name="ssh" limit value="3/m" accept'
# 重新加载配置
firewall-cmd --reload
4.2 数据库服务器防火墙配置
4.2.1 iptables配置
#!/bin/bash
# 数据库服务器防火墙配置
# 清空现有规则
iptables -F
iptables -X
iptables -Z
# 设置默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# 允许本地回环
iptables -A INPUT -i lo -j ACCEPT
# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许SSH(仅限管理网段)
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT
# 允许MySQL(仅限应用服务器)
iptables -A INPUT -s 192.168.1.100 -p tcp --dport 3306 -j ACCEPT
iptables -A INPUT -s 192.168.1.101 -p tcp --dport 3306 -j ACCEPT
# 保存规则
service iptables save
4.2.2 firewalld配置
#!/bin/bash
# 数据库服务器防火墙配置
# 创建数据库区域
firewall-cmd --permanent --new-zone=database
# 设置默认区域
firewall-cmd --set-default-zone=database
# 添加SSH服务(限制源IP)
firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="ssh" accept'
# 添加MySQL服务(限制源IP)
firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.1.100" service name="mysql" accept'
firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.1.101" service name="mysql" accept'
# 重新加载配置
firewall-cmd --reload
5. 故障排查和优化
5.1 常见问题排查
5.1.1 规则不生效
# 检查规则是否正确添加
iptables -L -n -v
firewall-cmd --list-all
# 检查服务状态
systemctl status iptables
systemctl status firewalld
# 检查日志
tail -f /var/log/messages
journalctl -u firewalld -f
5.1.2 连接被拒绝
# 启用日志记录
iptables -A INPUT -j LOG --log-prefix "INPUT DROP: "
# 查看日志
tail -f /var/log/messages
# firewalld日志
firewall-cmd --set-log-denied=all
5.2 性能优化
5.2.1 规则优化
# 将常用规则放在前面
iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT
# 使用状态匹配减少规则数量
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 使用multiport匹配多个端口
iptables -A INPUT -p tcp -m multiport --dports 80,443,8080 -j ACCEPT
5.2.2 监控和统计
# 查看规则统计
iptables -L -n -v --line-numbers
# 重置计数器
iptables -Z
# 实时监控
watch -n 1 'iptables -L -n -v'
6. 安全最佳实践
6.1 基本安全原则
- 2. 白名单策略:默认拒绝所有连接,只允许必要的连接
6.2 配置建议
# 设置合理的默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# 启用连接跟踪
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 限制连接速率
iptables -A INPUT -p tcp --dport 22 -m limit --limit 5/min --limit-burst 10 -j ACCEPT
# 防止扫描
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
6.3 备份和恢复
# 备份iptables规则
iptables-save > /root/iptables.backup.$(date +%Y%m%d)
# 恢复iptables规则
iptables-restore < /root/iptables.backup.20231201
# 备份firewalld配置
cp -r /etc/firewalld /root/firewalld.backup.$(date +%Y%m%d)
7. 总结
iptables和firewalld都是Linux系统中强大的防火墙工具。iptables提供了更底层的控制能力,适合对防火墙规则有详细要求的场景;firewalld则提供了更友好的管理界面和动态配置能力,更适合日常运维管理。
选择使用哪种工具主要取决于具体的应用场景和个人偏好。在实际部署中,建议根据系统的具体需求制定合适的防火墙策略,并定期进行安全审计和规则优化。
记住防火墙只是网络安全的一个组成部分,还需要结合其他安全措施(如入侵检测、日志监控、定期更新等)来构建完整的安全防护体系。