zoukankan      html  css  js  c++  java
  • Linux学习95 Linux防火墙iptables命令管理入门

    一、iptables/netfilter

      1、规则

        a、组成部分:根据规则匹配条件来尝试匹配报文,一旦匹配成功,就由规则定义的处理动作做出处理

          (1)、匹配条件

            1)、基本匹配条件:内建

            2)、扩展匹配条件:扩展模块定义

          (2)、处理动作

            1)、基本处理动作:内建

            2)、扩展处理动作:由扩展模块定义

            3)、自定义处理机制:自定义链

        b、iptables的链:内置链和自定义链

          (1)、内置链:对应于hook function

          (2)、自定义链接:用于内置链的扩展和补充,可实现更灵活的规则管理机制

      2、添加规则时的考量的点

        a、要实现哪种功能:判断添加到哪个表上

        b、报文流经的路径:判断添加到哪个链上

        c、链:链上的规则次序,即为检查的次序;因此,隐含一定的应用法则

          (1)、同类规则(访问同一应用),匹配范围小的放上面

          (2)、不同类的规则(访问不同应用),匹配到报文频率较大的放在上面

          (3)、将那些可由一条规则描述的多个规则合并起来

          (4)、设置默认策略 

    二、iptables命令

      他是高度模块化的,由诸多扩展模块实现其检查条件或处理动作的定义

        /usr/lib64/xtables

          IPv6:libip6t_

          IPv4:libipt_,libxt_

      1、iptables [-t table] {-A|-C|-D} chain rule-specification

        chain表示链的意思

      2、iptables [-t table] -I chain [rulenum] rule-specification

        rulenum表示规则号码

      3、iptables [-t table]  -R chain rulenum rule-spectification

      4、iptables [-t table]  -D chain rulenum

      5、iptables [-t table]  -S [chain [rulenum]]

      6、iptables [-t table]  {-F|-L|-Z} [chain [rulenum]] [options...]

      7、iptables [-t table] -N chain

      8、iptables [-t table]  -X [chain]

      9、iptables [-t table]  -P chain target

      10、iptables [-t table]  -E old-chain-name new-chain-name

      11、rule-specification = [matches...] [target]

      12、match = m matchname [per-match-options]

      13、target = -j targetname [per-target-options]

      14、规则格式:iptables [-t table]  COMMAND chain [-m matchname [per-match-options]] -j targetname [per-target-options]

        a、-t table:指的是表,可以省略,省略的话就相当于是默认的表filiter。

          raw,mangle,nat,[filiter],filiter是默认的

        b、COMMAND:管理命令

          (1)、链管理

            -N:new,自定义一条新的规则链

            -X:delete,删除自定义的规则链

              注意:仅能删除 用户自定义的 引用计数为0的 空的 链

            -P:Policy,设置默认策略,对filter表中的链而言,其默认策略有:

              ACCEPT:接收

              DROP:丢弃

              REJECT:拒绝

            -E:重命名自定义链,引用计数不为0的自定义链不能够被重命名,也不能被删除

          (2)、规则管理

            -A:append,追加

            -I:insert,插入,要指明位置,省略时表示第一条

            -D:delete,删除

              指明规则序号

              指明规则本身

            -R:replace,替换指定链上的指定规则

            -F:flush,清空指定的规则链

            -Z:zero,置零

              iptables的每条规则都有两个计数器

                匹配到的报文的个数

                匹配到的所有报文的大小之和

          (3)、查看:

            -L:list,列出指定链上的所有规则

              -n:numberic,以数字格式显示地址和端口号;如果不加-n,如果是0.0.0.0就会显示anywhere

              -v:verbose,详细信息

                -vv,-vvv

              -x:exacity,显示计数器结果的精确值

              --line-numbers:显示规则的序号

        c、chain:

          PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING

        d、匹配条件:

          (1)、基本匹配条件:无需加载任何模块,由iptables/netfilter自行提供

            1)、[!] -s,--source address[/mask][,...]:检查报文中的源IP地址是否符合此处指定的地址或范围,如果-s不写就表示源地址所有主机。

            2)、[!] -d,--destination address[/mask][,...]:检查报文中的目标IP地址是否符合此处指定的地址或范围。如果不加-d选项就表示所有主机。

            3)、[!] -p,--protocol protocol

                protocol:tcp,udp,udplite,icmp,icmpv6,esp,ah,sctp,mh or "all"

                {tcp | udp | icmp}

                如果我们在添加时不加-p,就相当于是all,即所有协议都匹配

              i:停止firewalld后我们现在来添加一条规则:来自于192.168.10.0网段访问本机地址的tcp服务时我们就允许

    [root@node3 ~]# iptables -t filter -A INPUT -s 192.168.10.0/24 -d 192.168.10.15 -p tcp -j ACCEPT
    [root@node3 ~]# iptables -vnL
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
       21  1516 ACCEPT     tcp  --  *      *       192.168.10.0/24      192.168.10.15       
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 14 packets, 1320 bytes)
     pkts bytes target     prot opt in     out     source               destination

              ii:我们来设置本机访问任何主机的tcp服务我们都允许,或者是本机访问192.168.10.0网络的。我们本机访问别人的主机报文是怎么出去的呢?即OUTPUT和POSTROUTING,因为POSTROUTING上不能做过滤所以我们这条规则要添加在OUTPUT上。

    [root@node3 ~]# iptables -A OUTPUT -s 192.168.10.15 -d 192.168.10.0/24 -p tcp -j ACCEPT 
    [root@node3 ~]# iptables -vnL
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
      177 12896 ACCEPT     tcp  --  *      *       192.168.10.0/24      192.168.10.15       
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
       14  1216 ACCEPT     tcp  --  *      *       192.168.10.15        192.168.10.0/24     
    [root@node3 ~]#

              iii:现在的话我们就可以将INPUT的默认策略设置为DROP了,因为我们放行了tcp的ssh服务,那么他就不会被默认策略所拒绝,所以我们就可以设置INPUT和OUTPUT默认策略都为DROP了,甚至FORWARD也设置为DROP了。这样我们就可以做白名单了。

    [root@node3 ~]# iptables -P INPUT DROP
    [root@node3 ~]# iptables -P OUTPUT DROP
    [root@node3 ~]# iptables -P FORWARD DROP
    [root@node3 ~]# iptables -vnL
    Chain INPUT (policy DROP 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
      302 21976 ACCEPT     tcp  --  *      *       192.168.10.0/24      192.168.10.15       
    
    Chain FORWARD (policy DROP 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy DROP 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
       99  9460 ACCEPT     tcp  --  *      *       192.168.10.15        192.168.10.0/24

              iiii、此时我们会发现我们ping 192.168.10.15已经ping不通了,因为我们只放行了tcp的流量,而ping是icmp协议。现在我们来设置允许任何人ping

    [root@node3 ~]# iptables -A INPUT -d 192.168.10.15 -p icmp -j ACCEPT #放行进来
    [root@node3 ~]# iptables -A OUTPUT -s 192.168.10.15 -p icmp -j ACCEPT #放行出去

            4)、[!] -i,--in-interface name:数据报文流入的接口,只能应用于数据报文流入的环节,只能应有于PREROUTING,INPUT和FORWARD链;即前半场。

            5)、[!] -o,--out-interfaca name:数据报文流出的接口;只能应用于数据报文流出的环境,只能应用于FORWARD,OUTPUT和POSTROUTING链即后半场。

            6)、-m:指明要使用哪个扩展模块的

            7)、-j: 匹配到相应的报文后如何处理

          (2)、扩展匹配条件:需要加载扩展模块,方可生效

            1)、隐式扩展:不需要手动加载扩展模块;因为他们是对协议的扩展,所以,但凡使用-p指明了协议,就表示已经指明了要扩展的模块

            2)、tcp:

              [!] --source-port,--sport port[:port]:匹配报文的源端口;可以是端口范围

              [!] --destination-port,--dport port[:port]:匹配报文的目标端口,可以是端口范围

              [!] --tcp-flages mask comp 

                mask is the flags which we should examine,written as a comma-separated list,例如SYN,ACK,FIN,RST

                comm is a comma-separated list of flags which must be set,例如SYN,例如:"--tcp-flags SYN,ACK,FIN,RST SYN"表示,要检查的标志位为SYN,ACK,FIN,RST四个,其中SYN必须为1,余下的必须为0;

              [!] --syn:用于匹配第一次握手,相当于"--tcp-flags SYN,ACK,FIN,RST SYN";

      15、如果我们关闭iptables那么他的默认规则是允许的。我们来看相应的查看命令

        a、我们来查看关闭后的规则

    [root@www ~]# iptables -vnL
    Chain INPUT (policy ACCEPT 7754 packets, 636K bytes) #policy ACCEPT指默认允许访问
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain FORWARD (policy DROP 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 417 packets, 48594 bytes)
     pkts bytes target     prot opt in     out     source               destination         

        b、-L:我们来查看相应的表

          (1)、我们来看filiter表的规则,可以使用iptables -L或者iptables -t filter -L

    [root@www ~]# iptables -L
    Chain INPUT (policy ACCEPT) #Chain INPUT是用来指明哪个链的,每个链都应该有默认的法则,policy叫策略,即指明这个链的默认策略。
    target     prot opt source               destination         
    
    Chain FORWARD (policy DROP)
    target     prot opt source               destination         
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination         

            1)、Chain INPUT (policy ACCEPT) #Chain INPUT是用来指明哪个链的,每个链都应该有默认的法则,policy叫策略,即指明这个链的默认策略。ACCEPT表示允许,即允许访问的,也就是说我们要做黑名单了,接下来就应该设置拒绝哪些人访问。我们说过做黑名单其实不是一种安全做法,我们一般都使用白名单,即我们要将这个默认策略设置为拒绝,也就是DROP。或者REJECT。这是两种不同的拒绝方式。比如别人向你扔了一块石子, 你走掉了没理他,假如是隔墙扔过去的,在扔之前他并不知道有人,然后你把石子捡起来又扔回来,然后他就知道那边有人了,于是开始发起攻击,一大堆石头扔过去了。于是我们不回应的方式就是DROP,我们扔回去的方式就REJECT,我们一般用DROP就可以 ,用REJECT就是自找麻烦。不过从响应角度来说的话我们的REJECT是比DROP要快的。

              target     prot opt source               destination :下面就是添加一条一条的规则,我们这儿没有添加规则所以他没有一条一条显示,我们将firewalld启动起来以后默认会有一些规则。

    [root@node3 ~]# systemctl start firewalld
    [root@node3 ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED 
    ACCEPT     all  --  anywhere             anywhere            
    INPUT_direct  all  --  anywhere             anywhere            
    INPUT_ZONES_SOURCE  all  --  anywhere             anywhere            
    INPUT_ZONES  all  --  anywhere             anywhere            
    DROP       all  --  anywhere             anywhere             ctstate INVALID
    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
    
    Chain FORWARD (policy ACCEPT)
    target     prot opt source               destination         
    ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
    ACCEPT     all  --  anywhere             anywhere            
    FORWARD_direct  all  --  anywhere             anywhere            
    FORWARD_IN_ZONES_SOURCE  all  --  anywhere             anywhere            
    FORWARD_IN_ZONES  all  --  anywhere             anywhere            
    FORWARD_OUT_ZONES_SOURCE  all  --  anywhere             anywhere            
    FORWARD_OUT_ZONES  all  --  anywhere             anywhere            
    DROP       all  --  anywhere             anywhere             ctstate INVALID
    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
    ...

            Chain INPUT (policy ACCEPT)
            target prot opt source destination
            ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
            ACCEPT all -- anywhere anywhere

            3)、target表示这个规则是拒绝还是允许,允许什么呢?port表示协议,即允许哪一种协议来讲,source表示源地址,即源地址是谁,destination表示目标地址,即目标地址是谁的时候是允许的还是拒绝的。

            4)、ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED :表示来自于任意地址的家伙访问都任意地址的时候无论是什么协议我们都允许。

          我们的anywhere是任意地址,之所以要写成anywhere而不写成IP地址是因为可以反向解析的,默认iptables解析的时候会把我们的地址反解为主机名。把端口反解为服务名。因此我们可以在命令中加上-n选项,即以数值格式显示端口号,不要反解。

            5)、我们可以看到我们INPUT一个链上有很多条规则,哪一条规则和哪一条有什么关系呢?是第几个呢?因此我们有些命令在处理的时候是需要给定规则号码的,叫rulenum,因此我们可以使用一个长选项--line-numbers来显示我们每一个规则在当前链上的规则号码。

    [root@node3 ~]# iptables -L -n --line-numbers |more 
    Chain INPUT (policy ACCEPT)
    num  target     prot opt source               destination         
    1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
    3    INPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           
    4    INPUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
    5    INPUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
    6    DROP       all  --  0.0.0.0/0            0.0.0.0/0            ctstate INVALID
    7    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
    
    Chain FORWARD (policy ACCEPT)
    num  target     prot opt source               destination         
    1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
    3    FORWARD_direct  all  --  0.0.0.0/0            0.0.0.0/0           
    4    FORWARD_IN_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
    5    FORWARD_IN_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
    6    FORWARD_OUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0   
    ...

            6)、我们还可以使用-v显示详细选项

    [root@node3 ~]# iptables -L -n -v --line-numbers |more 
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1      194 14528 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    2        4   204 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    3      582 40294 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    4      582 40294 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    5      582 40294 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    6        0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
    7      582 40294 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

              pkts: 表示包数量,bytes表示字节数。意思是被本条规则匹配到的有多少通信包,这么多通信包加起来他的大小一共有多大。所以叫被本规则匹配到的包的个数以及所有的大小之和。

              我们还可以通过-vv或-vvv来显示更详细的细节。

            7)、不仅默认策略有我们内建的链上的策略也有,即被默认匹配到的包有多少个以及加起来的大小有多少

               

            8)、而且,规则匹配到相应包后是会做单位换算的,即有可能四舍五入,如果我们要想看更精确的值我们可以使用-x参数,即显示计数器结果的精确值。

          (2)、查看mangle表

    [root@www ~]# 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               destination

          (3)、查看nat表

    [root@node3 ~]# iptables -t nat -L
    Chain PREROUTING (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain POSTROUTING (policy ACCEPT)
    target     prot opt source               destination

          (4)、查看raw表

    [root@node3 ~]# iptables -t raw -L
    Chain PREROUTING (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination

          (5)、我们也可以只查看单个链

    [root@node3 ~]# iptables -vn --line-numbers -L INPUT 
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1      376 29301 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    2        4   204 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    3     1125 84489 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    4     1125 84489 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    5     1125 84489 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    6        0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
    7     1124 84437 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

      16、我们来看链管理命令(我们先禁用firewalld)

         a、我们来做入栈web服务控制。即到本机来的。

    [root@node3 ~]# iptables -N in_web_rules
    [root@node3 ~]# iptables -vnL
    Chain INPUT (policy ACCEPT 20 packets, 1424 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 13 packets, 1228 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain in_web_rules (0 references)
     pkts bytes target     prot opt in     out     source               destination

        此处我们没有写-t tables,即会选择默认的filter表。我们可以看到对于内建的链来讲他有policy以及匹配到的报文数,但是对于自定义的链来讲有个引用计数,自定义链是没法直接生效的,只能在内建的链上调用以后才能生效,调用一次就加1,再调用一次就再加一。并且每一个自定义链都会有引用计数,如果引用计数不为0的话你是删不掉的。

        b、我们可以删除一个自定义链

    [root@node3 ~]# iptables -X in_web_rules
    [root@node3 ~]# iptables -vnL
    Chain INPUT (policy ACCEPT 5 packets, 356 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 4 packets, 432 bytes)
     pkts bytes target     prot opt in     out     source               destination

          (1)、如果一个链上有规则的话你是删不掉的

          (2)、一个链如果有引用你也是删不掉的,即引用计数不为0的时候

          (3)、如果这个链不是自定义那么你也删不掉

        c、-P:我们还可以修改相应链的默认策略。使用-P选项

    [root@node3 ~]# iptables -vnL
    Chain INPUT (policy ACCEPT 1058 packets, 85883 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 136 packets, 6852 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    [root@node3 ~]# iptables -P FORWARD DROP
    [root@node3 ~]# iptables -vnL
    Chain INPUT (policy ACCEPT 28 packets, 2034 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain FORWARD (policy DROP 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 12 packets, 1136 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    [root@node3 ~]#

        d、-E:我们还可以可以重命名自定义链;引用计数不为0的自定义链不能够被重命名,也不能被删除。

    [root@node3 ~]# iptables -N in_web
    [root@node3 ~]# iptables -vnL
    Chain INPUT (policy ACCEPT 18 packets, 1292 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 12 packets, 1136 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain in_web (0 references)
     pkts bytes target     prot opt in     out     source               destination         
    [root@node3 ~]# iptables -E in_web in_web_rules
    [root@node3 ~]# iptables -vnL
    Chain INPUT (policy ACCEPT 18 packets, 1292 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 12 packets, 1136 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain in_web_rules (0 references)
     pkts bytes target     prot opt in     out     source               destination         
    [root@node3 ~]#

      17、我们来看看规则管理命令

        a、-A:append,追加,即放在整个链的尾部

        b、-I:insert,插入,要指明位置,省略时表示第一条

        c、-D:delete,删除:

          (1)、指明规则序号

          (2)、指明规则本身

          (3)、示例

            1)、比如我们要清空我们FORWARD链的第七条规则

    [root@node3 ~]# iptables --line-numbers -vnL FORWARD
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    2        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    3        0     0 FORWARD_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    4        0     0 FORWARD_IN_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    5        0     0 FORWARD_IN_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    6        0     0 FORWARD_OUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    7        0     0 FORWARD_OUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    8        0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
    9        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
    [root@node3 ~]# iptables -D FORWARD 7
    [root@node3 ~]# iptables --line-numbers -vnL FORWARD
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    2        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    3        0     0 FORWARD_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    4        0     0 FORWARD_IN_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    5        0     0 FORWARD_IN_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    6        0     0 FORWARD_OUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    7        0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
    8        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
    [root@node3 ~]#

        d、-R:replace,替换指定链上的指定规则

        e、-F:flush,清空指定的规则链

          (1)、比如我们要清空IN_public链上的规则

    [root@node3 ~]# iptables -vnL |grep -wA4 "IN_public"
    Chain IN_public (2 references)
     pkts bytes target     prot opt in     out     source               destination         
      115  9143 IN_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
      115  9143 IN_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
      115  9143 IN_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    [root@node3 ~]# iptables -F IN_public
    [root@node3 ~]# iptables -vnL |grep -wA4 "IN_public"
    Chain IN_public (2 references)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain IN_public_allow (0 references)
     pkts bytes target     prot opt in     out     source               destination         
    [root@node3 ~]# 

          (2)、如果我们后面啥也没写的话那么所有的链就被清除了。不过如果我们清除完后不保存那么我们将firewalld服务重启起来就可以了。因为我们修改的时候是在内存中内核中修改的,他启动的时候是读取某个文件配置的,只要不把文件干掉就没啥问题。

        f、-Z:zero,置零

          iptables的每条规则都有两个计数器

          (1)、匹配到的报文的个数

          (2)、匹配到的所有报文的大小之和

          (3)、示例

            1)、我们启动firewalld发现会有INPUT_ZONES这么一个链,现在我们将其置零。如果我们-Z选项不指定链那么就表示指定表的所有链上的所有规则都被清空。因此我们这儿指定链

    Chain INPUT_ZONES (1 references)
     pkts bytes target     prot opt in     out     source               destination         
        3   248 IN_public  all  --  ens33  *       0.0.0.0/0            0.0.0.0/0           [goto] 
        0     0 IN_public  all  --  +      *       0.0.0.0/0            0.0.0.0/0           [goto] 
    [root@node3 ~]# iptables -Z INPUT_ZONES

            2)、我们还可以指定哪一个链上的哪一个规则重新计数,INPUT_ZONES的第一条计数重新置零计数

    [root@node3 ~]# iptables -Z INPUT_ZONES 1
  • 相关阅读:
    Ajax返回DataSet时
    spring(8)
    spring(12)
    spring(6)
    spring(11)
    spring(10)
    spring(5)
    spring(3)
    spring(9)
    spring(7)
  • 原文地址:https://www.cnblogs.com/Presley-lpc/p/12991554.html
Copyright © 2011-2022 走看看