在 Linux 系统中,防火墙管理主要依赖于不同的工具和框架。以下是常见的防火墙相关命令和工具的详细说明:
一、iptables(传统防火墙工具)
基础操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14# 查看当前规则(默认表为filter)
iptables -L -n -v
# 查看NAT表规则
iptables -t nat -L -n -v
# 清空所有规则(慎用)
iptables -F
# 删除自定义链
iptables -X
# 重置计数器
iptables -Z规则管理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# 允许来自特定IP的流量
iptables -A INPUT -s 192.168.1.100 -j ACCEPT
# 拒绝特定端口的流量
iptables -A INPUT -p tcp --dport 22 -j DROP
# 允许回环接口
iptables -A INPUT -i lo -j ACCEPT
# 插入规则到链的顶部
iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT
# 删除某条规则(需先查看规则编号)
iptables -L INPUT --line-numbers
iptables -D INPUT 2网络地址转换(NAT)
1
2
3
4
5# 源地址转换(SNAT)
iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 1.2.3.4
# 目的地址转换(DNAT)
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.100:80持久化规则
1
2
3
4
5
6
7
8# 保存规则到文件(Debian/Ubuntu)
iptables-save > /etc/iptables/rules.v4
# 恢复规则(重启后生效)
iptables-restore < /etc/iptables/rules.v4
# CentOS/RHEL 使用以下命令保存
service iptables save
二、nftables(iptables的现代替代品)
基本操作
1
2
3
4
5# 查看规则集
nft list ruleset
# 清空所有规则
nft flush ruleset规则管理
1
2
3
4
5
6
7
8
9
10# 创建表和链
nft add table inet my_table
nft add chain inet my_table my_chain { type filter hook input priority 0 \; }
# 添加规则(允许SSH)
nft add rule inet my_table my_chain tcp dport 22 accept
# 删除规则(需指定句柄ID)
nft --handle list ruleset
nft delete rule inet my_table my_chain handle 2持久化规则
1
2
3
4
5# 保存规则
nft list ruleset > /etc/nftables.conf
# 加载规则
nft -f /etc/nftables.conf
三、firewalld(RHEL/CentOS/Fedora的默认工具)
服务管理
1
2
3# 启动/停止服务
systemctl start firewalld
systemctl enable firewalld区域(Zones)管理
1
2
3
4
5
6
7
8# 查看默认区域
firewall-cmd --get-default-zone
# 修改默认区域
firewall-cmd --set-default-zone=public
# 查看所有可用区域
firewall-cmd --get-zones端口和服务管理
1
2
3
4
5
6
7
8
9
10
11# 开放HTTP服务
firewall-cmd --zone=public --add-service=http --permanent
# 开放自定义端口
firewall-cmd --zone=public --add-port=8080/tcp --permanent
# 移除规则
firewall-cmd --zone=public --remove-port=8080/tcp --permanent
# 重新加载配置
firewall-cmd --reload高级配置
1
2
3
4
5# 允许特定IP访问SSH
firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="ssh" accept'
# 端口转发(将80端口转发到内部IP)
firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.100
四、ufw(Ubuntu的简化防火墙工具)
基础操作
1
2
3
4
5
6# 启用/禁用防火墙
ufw enable
ufw disable
# 查看状态
ufw status verbose规则管理
1
2
3
4
5
6
7
8
9
10
11
12# 允许SSH
ufw allow ssh
# 允许特定端口
ufw allow 80/tcp
# 拒绝特定IP
ufw deny from 192.168.1.100
# 删除规则(按编号)
ufw status numbered
ufw delete 2高级配置
1
2
3
4
5# 限制SSH连接速率
ufw limit 22/tcp
# 允许特定子网
ufw allow from 192.168.1.0/24
五、其他工具
ebtables(以太网桥防火墙)1
2# 查看规则
ebtables -Lshorewall(高级配置工具)- 通过编辑配置文件(如
/etc/shorewall/rules)管理规则。
- 通过编辑配置文件(如
六、日志与调试
查看防火墙日志
1
2
3
4
5# iptables日志(需先在规则中添加LOG动作)
iptables -A INPUT -j LOG --log-prefix "IPTABLES-DROP: "
# 查看日志(通常位于/var/log/syslog或/var/log/messages)
tail -f /var/log/syslog | grep "IPTABLES-DROP"追踪数据包路径
1
2
3
4
5# 使用traceroute
traceroute example.com
# 使用tcpdump抓包
tcpdump -i eth0 port 80
七、总结
工具选择建议:
- RHEL/CentOS/Fedora:优先使用
firewalld。 - Debian/Ubuntu:推荐使用
ufw。 - 高级用户/自定义需求:直接使用
iptables或nftables。
- RHEL/CentOS/Fedora:优先使用
注意事项:
- 修改规则前备份现有配置。
- 谨慎使用
DROP和REJECT避免锁定自己。 - 测试规则后及时持久化。
通过上述命令,您可以灵活管理 Linux 系统的防火墙配置。