IPtables 规则管理 增删改查

什么是规则


数据包的过滤基于规则,而规则是由匹配条件+动作组成。那我们对规则的操作无非就是增删查改。

操作规则的语法∶iptables【-t表名】选项【链名】【规则】【动作】

操作规则之前我们需要考量如下两个问题∶

      1)要实现什么功能∶判断添加到哪个表上

      2)报文流经的路线∶判断添加到哪个链上

下面都是选项

 如何查看规则:查看规则属于链当中的第几条,-L 查看 -n 不解析 -v 详细 --line-numbers 编号

[root@localhost ~]# iptables -L  -n --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain FORWARD (policy DROP)
num  target     prot opt source               destination         
1    DOCKER-USER  all  --  0.0.0.0/0            0.0.0.0/0           
2    DOCKER-ISOLATION-STAGE-1  all  --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
4    DOCKER     all  --  0.0.0.0/0            0.0.0.0/0           
5    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
6    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           

添加规则:禁止 ping 

在filter当中,因为是请求本机,所以在input当中去做。 -I insert插入规则,也就是往链中的第一行插入规则。

[root@localhost ~]# iptables -t filter -I INPUT -p icmp -j REJECT 
[root@localhost ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     icmp --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (180.101.49.11) 56(84) bytes of data.
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1009ms

首先去匹配INPUT链中的第一个规则,源地址和目标地址是任意IP,只要协议是ICMP就REJECT拒绝。

修改规则  -R修改规则需要指定number,需要指定规则的编号,

[root@localhost ~]# iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     icmp --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
[root@localhost ~]# iptables -t filter -R INPUT 1 -p icmp -j DROP
[root@localhost ~]# iptables -L -n --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    DROP       icmp --  0.0.0.0/0            0.0.0.0/0  

清空计数器,默认不使用-t则默认为filter表

[root@localhost ~]# iptables -L -n -V
iptables v1.4.21
[root@localhost ~]# iptables -L -n --line-numbers -v
Chain INPUT (policy ACCEPT 134 packets, 9543 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        4   336 DROP       icmp --  *      *       0.0.0.0/0            0.0.0.0/0   

[root@localhost ~]# iptables -t filter -Z

[root@localhost ~]# iptables -t filter -L -n -v
Chain INPUT (policy ACCEPT 40 packets, 2727 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       icmp --  *      *       0.0.0.0/0            0.0.0.0/0    

备份规则

[root@localhost ~]# iptables-save > iptables-rules

清空规则,默认清空的是filter表,如果不指定表

[root@localhost ~]# iptables -F

清空nat表

[root@localhost ~]# iptables -t nat -F

从备份当中恢复规则 

[root@localhost ~]# iptables-restore < iptables-rules 

iptables的操作是临时的,一旦规则重启了规则就失效了,如果想要实现永久生效,可以将这个命令放到开机自启动。

猜你喜欢

转载自blog.csdn.net/qq_34556414/article/details/126381043