iptables是Linux系统中用来配置防火墙的命令。
四表:
- filter(用于过滤)
- nat(用于 NAT)
- mangle(用于修改分组数据)
- raw(用于原始数据包)
最常用的是filter 和 nat。对应下图中的绿色块。
五链:
- PREROUTING:用于路由判断前所执行的规则,比如,对接收到的数据包进行 DNAT。
- POSTROUTING:用于路由判断后所执行的规则,比如,对发送或转发的数据包进行 SNAT 或 MASQUERADE。
- OUTPUT: 类似于 PREROUTING,但只处理从本机发送出去的包。
- INPUT: 类似于 POSTROUTING,但只处理从本机接收的包。
下图中白色背景方框,则表示链(chain)
iptables -L 列出规则,默认为filter表的规则。
[root@node100 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy DROP)
target prot opt source destination
DOCKER-USER all -- anywhere anywhere
DOCKER-ISOLATION-STAGE-1 all -- anywhere anywhere
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
DOCKER all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain DOCKER (1 references)
target prot opt source destination
Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target prot opt source destination
DOCKER-ISOLATION-STAGE-2 all -- anywhere anywhere
RETURN all -- anywhere anywhere
Chain DOCKER-ISOLATION-STAGE-2 (1 references)
target prot opt source destination
DROP all -- anywhere anywhere
RETURN all -- anywhere anywhere
Chain DOCKER-USER (1 references)
target prot opt source destination
RETURN all -- anywhere anywhere
iptables -t nat -L 列出nat表的规则。
[root@node100 ~]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DOCKER all -- anywhere anywhere ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
DOCKER all -- anywhere !loopback/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- 172.17.0.0/16 anywhere
Chain DOCKER (2 references)
target prot opt source destination
RETURN all -- anywhere anywhere
iptables中的第一个选项可以是-A, 表明向链(chain)中添加一条新的规则,也可以是-I,表明将新的规则插入到规则集的开头。接下来的参数指定了链。
所谓链就是若干条规则的集合
OUTPUT链它可以控制所有的出站流量(outgoing traffic)。
INPUT链它能够控制所有的入站流量(incoming traffic)。
-d指定了所要匹配的分组目的地址,
-s指定了分组的源地址。
-j指示iptables执行到特定的处理(action)
实战