iptables 的作用及与 firewalld 的区别
1. iptables 是什么?有什么用?
iptables 是 Linux 系统上经典的防火墙工具,工作在网络层,主要功能包括:
-
数据包过滤
根据规则允许或阻止特定的网络流量(如 IP 地址、端口、协议等)
-
流量转发
控制数据包在不同网络接口间的转发
-
网络地址转换(NAT)
实现端口映射、IP 地址转换等功能
-
流量监控与统计
记录通过防火墙的数据包数量和字节数,这在网络调试、渗透测试等场景中非常有用
为什么使用 iptables 而不是 firewalld?
firewalld 是 RedHat 系 Linux 中较新的防火墙管理工具,它本质上是 iptables 的前端封装,提供了更友好的接口和动态更新机制。但在以下场景中,iptables 更具优势:
-
精细化流量统计
iptables 可以直接通过规则链记录特定 IP / 端口的流量数据,操作简单直接
-
脚本化自动化
iptables 命令更适合写入脚本,实现复杂的防火墙策略自动化配置
-
兼容性
所有 Linux 发行版都支持 iptables,而 firewalld 主要用于 RedHat 系
-
学习曲线
对于简单的过滤和监控需求,直接使用 iptables 命令比学习 firewalld 的区域(zone)概念更直观
了解更多查看:
【红帽Linux】RHEL 8 和 9 安全加固指南

如何使用 iptables 测量流量
实验步骤
首先查看一下当前 iptables 的状态,使用 -L 选项:
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
可以看到目前系统中的3个规则链,这里假设系统中已经有一些规则了,为了不破坏原有规则,我们需要自己创建一个专门用于测量流量的规则链,名字随意取,这里就叫"traffic"
使用命令 iptables -N 来创建一个新的规则链
[root@localhost ~]# iptables -N traffic
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain traffic (0 references)
target prot opt source destination
接着在 traffic 这个链上添加2个规则,用于测量本机到 192.168.88.100 这台服务器之间的流量;
[root@localhost ~]# iptables -A traffic -s 192.168.88.100
[root@localhost ~]# iptables -A traffic -d 192.168.88.100
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain traffic (0 references)
target prot opt source destination
all -- 192.168.88.100 anywhere
all -- anywhere 192.168.88.100
[root@localhost ~]#
接着,要将 traffic 链,接入(附加)到系统原先的"INPUT" 和 "OUTPUT" 规则链里,这样才能监控到流量
[root@localhost ~]# iptables -A INPUT -j traffic
[root@localhost ~]# iptables -A OUTPUT -j traffic[root@localhost ~]# iptables -LChain INPUT (policy ACCEPT)target prot opt source destinationtraffic all -- anywhere anywhereChain FORWARD (policy ACCEPT)target prot opt source destinationChain OUTPUT (policy ACCEPT)target prot opt source destinationtraffic all -- anywhere anywhereChain traffic (2 references)target prot opt source destination all -- 192.168.88.100 anywhere all -- anywhere 192.168.88.100[root@localhost ~]#
使用 iptables -Z 将流量统计信息清零,一切就绪,就可以触发流量进行观察了
[root@localhost ~]# iptables -Z
使用 iptables -L 规则链 -nvx 命令来查看与目标服务器之间传输了多少数据包以及字节; 建议配合 watch 命令进行持续监控
[root@localhost ~]# watch -d -n1 iptables -L traffic -nvx
Every 1.0s: iptables -L traffic -nvx Wed May 11 14:01:57 2022
Chain traffic (2 references)
pkts bytes target prot opt in out source destination
17 708 all -- * * 192.168.88.100 0.0.0.0/0
364 15940 all -- * * 0.0.0.0/0 192.168.88.100
可以看到,iptables 会记录你发送出去了多少包,以及流量大小; 也能记录对方发给你的流量
监控结束后,还原 iptables
1.首先清空系统原先的"INPUT" 和 "OUTPUT" 规则链里面的 "traffic"; 但首先要查看下该规则在链下的编号(也就是第几行)
删除规则的命令格式: iptables -D INPUT 规则编号
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
traffic all -- anywhere anywhere # <-- 这就是第一条规则,在第1行,所以编号是 1
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
traffic all -- anywhere anywhere # <-- 该链下的第一条规则,编号1
Chain traffic (2 references)
target prot opt source destination
all -- 192.168.88.100 anywhere
all -- anywhere 192.168.88.100
删除 INPUT OUTPUT 的"traffic"规则
[root@localhost ~]# iptables -D INPUT 1
[root@localhost ~]# iptables -D OUTPUT 1
# 再次验证,发现规则链下的规则被删除了
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain traffic (0 references)
target prot opt source destination
all -- 192.168.88.100 anywhere
all -- anywhere 192.168.88.100
2.清理 traffic 链里面的规则,使用 iptables -F 选项
[root@localhost ~]# iptables -F traffic
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain traffic (0 references)
target prot opt source destination
[root@localhost ~]#
3.删除 traffic 链,只剩下系统原先的3个规则链
[root@localhost ~]# iptables -X traffic
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@localhost ~]#