iptables-参数-A和-I

iptables -L -n --line-number 列出链所有的规则

[root@GDY-TEST07 etc]# iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  192.168.216.55       0.0.0.0/0           tcp dpt:22 
2    ACCEPT     tcp  --  192.168.228.0/24     0.0.0.0/0           tcp dpt:22 
3    ACCEPT     tcp  --  10.153.97.0/24       0.0.0.0/0           tcp dpt:22 
4    DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination      

 删除指定的第4行规则:iptables -D INPUT 4

[root@tp ~]# iptables -F        清除预设表filter中的所有规则链的规则
[root@tp ~]# iptables -X        清除预设表filter中使用者自定链中的规则

-A与-I 参数的区别

-A参数

iptables是由上往下进行匹配

iptables -A INPUT -s 192.168.228.0/24 -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -s 10.153.97.0/24 -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j DROP

执行以上三条命令后,防火墙由上往下匹配,既从num 1开始匹配到num 3,则允许192.168.228.0/24和10.153.97.0/24这两个网段访问,其他不允许访问

[root@GDY-TEST07 etc]# iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  192.168.228.0/24     0.0.0.0/0           tcp dpt:22 
2    ACCEPT     tcp  --  10.153.97.0/24       0.0.0.0/0           tcp dpt:22 
3    DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination     

对比

-I 参数

iptables -I INPUT -s 192.168.228.0/24 -p tcp --dport 22 -j ACCEPT
iptables -I INPUT -s 10.153.97.0/24 -p tcp --dport 22 -j ACCEPT
iptables -I INPUT -p tcp --dport 22 -j DROP

执行以上三条命令后,防火墙由上往下匹配,既从num 1开始匹配,此时num 1的防火墙策略已将访问拒绝,num 2和num 3即使accept也不会生效

[root@GDY-TEST07 etc]# iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22         
2    ACCEPT     tcp  --  192.168.228.0/24     0.0.0.0/0           tcp dpt:22 
3    ACCEPT     tcp  --  10.153.97.0/24       0.0.0.0/0           tcp dpt:22 
 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination    

从以上的-A 和 -I参数可以看出,-A 会将后执行的策略添加到已有策略后,而-I 则会插入到已有策略的前(既成为第一条策略)。

按照上述所说,如果想加一条策略为允许192.168.216.55这台机器访问,此时应采用-I 参数将策略添加到已有策略前,如果采用-A 参数将策略添加到已有策略后,

当前场景则是添加到drop策略后,则“允许192.168.216.55这台机器访问”策略则不会生效。

正确命令:

iptables -I INPUT -s 192.168.216.55/32 -p tcp --dport 22 -j ACCEPT

[root@GDY-TEST07 etc]# iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  192.168.216.55       0.0.0.0/0           tcp dpt:22 
2    ACCEPT     tcp  --  192.168.228.0/24     0.0.0.0/0           tcp dpt:22 
3    ACCEPT     tcp  --  10.153.97.0/24       0.0.0.0/0           tcp dpt:22 
4    DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination      

以上所述,只适合上述所说场景。

因为上述场景drop策略是禁止所有从22端口进行访问。

原文地址:https://www.cnblogs.com/mustark/p/11189883.html