图-1 步骤 实现此案例需要按照如下步骤进行。
1)列出filter表的规则 filter表是iptables缺省操作的表,因此 -t filter 通常被省略。 filter表默认包括INPUT、OUTPUT、FORWARD这三个规则链。
[root@svr5 ~]# 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@svr5 ~]# iptables -L INPUT //只看INPUT链 Chain INPUT (policy ACCEPT) target prot opt source destination2)列出nat表的规则 nat表默认包括PREROUTING、POSTROUTING、OUTPUT这三个规则链。
[root@svr5 ~]# iptables -t nat -L Chain PREROUTING (policy ACCEPT) target prot opt source destination Chain POSTROUTING (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destinationraw表默认包括PREROUTING、OUTPUT这两个规则链。
[root@svr5 ~]# iptables -t raw -L Chain PREROUTING (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination4)列出mangle表的规则 mangle表默认包括所有的五种规则链。
[root@svr5 ~]# iptables -t mangle -L Chain PREROUTING (policy ACCEPT) target prot opt source destination 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 POSTROUTING (policy ACCEPT) target prot opt source destination1)-A,在末尾追加一条新的防火墙规则 允许访问本机的所有TCP数据包:
[root@svr5 ~]# iptables -A INPUT -p tcp -j ACCEPT //添加一条规则 [root@svr5 ~]# iptables -L INPUT //查看结果 Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- anywhere anywhere在查看规则时,可以结合-n选项以数字形式显示地址、端口等信息:
[root@svr5 ~]# iptables -nL INPUT Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0还可以结合–line-numberrs来显示规则的行号(顺序号):
[root@svr5 ~]# iptables -nL INPUT --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 //当前链的第1条规则2)-I,在开头或指定位置插入一条新的防火墙规则 在INPUT链的开头插入规则,允许所有的UDP协议数据包: [root@svr5 ~]# iptables -I INPUT -p udp -j ACCEPT [root@svr5 ~]# iptables -nL INPUT --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT udp – 0.0.0.0/0 0.0.0.0/0 //新增规则作为第一条 2 ACCEPT tcp – 0.0.0.0/0 0.0.0.0/0 //原第一条变为第二条 插入INPUT链的第2条规则,允许所有的ICMP协议数据包: [root@svr5 ~]# iptables -I INPUT 2 -p icmp -j ACCEPT [root@svr5 ~]# iptables -nL INPUT --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT udp – 0.0.0.0/0 0.0.0.0/0 2 ACCEPT icmp – 0.0.0.0/0 0.0.0.0/0 //新增的第2条规则 3 ACCEPT tcp – 0.0.0.0/0 0.0.0.0/0
1)-D,删除指定的规则 删除INPUT链的第3条规则:
[root@svr5 ~]# iptables -D INPUT 3 //删除第3条规则 [root@svr5 ~]# iptables -nL INPUT --line-numbers //确认删除结果 Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0删除指定具体内容的规则:
[root@svr5 ~]# iptables -D INPUT -p udp -j ACCEPT //删除UDP放行规则 [root@svr5 ~]# iptables -nL INPUT --line-numbers //确认删除结果 Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 2)-F,清空规则清空filter表的所有规则:
[root@svr5 ~]# iptables -F [root@svr5 ~]# iptables -nL --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination清空nat表的所有规则:
[root@svr5 ~]# iptables -t nat -F [root@svr5 ~]# iptables -t mangle -F [root@svr5 ~]# iptables -t raw -F