首页 文章

如何从iptables中删除特定规则?

提问于
浏览
283

我分别在端口8006和8007上托管特殊的HTTP和HTTPS服务 . 我用iptables来“激活”服务器;即路由传入的HTTP和HTTPS端口:

iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 8006 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 8007 -j ACCEPT
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8006 
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8007  
iptables -A OUTPUT -t nat -d 127.0.0.1 -p tcp --dport 80 -j REDIRECT --to-ports 8006
iptables -A OUTPUT -t nat -d 127.0.0.1 -p tcp --dport 443 -j REDIRECT --to-ports 8007

这就像一个魅力 . 但是,我想创建另一个脚本,再次禁用我的服务器;即在运行上面的行之前将iptables恢复到它所处的状态 . 但是,我很难找出删除这些规则的语法 . 似乎唯一有效的是完全冲洗:

iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

但这也将删除其他不受欢迎的iptables规则 .

6 回答

  • 1

    对我来说没有任何问题的最佳解决方案就是这样的:
    1.添加一些评论的临时规则:

    comment=$(cat /proc/sys/kernel/random/uuid | sed 's/\-//g')
    iptables -A ..... -m comment --comment "${comment}" -j REQUIRED_ACTION
    

    2.当规则添加并且您希望删除它(或带有此注释的所有内容)时,请执行以下操作:

    iptables-save | grep -v "${comment}" | iptables-restore
    

    因此,您将100%删除与$ comment匹配的所有规则,并保持其他行不变 . 这个解决方案在过去的2个月里工作,每天约100次规则变更 - 没有问题 . 希望,这有帮助

  • 407

    首先使用以下命令列出所有iptables规则:

    iptables -S
    

    它列出如下:

    -A XYZ -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
    

    然后复制所需的行,只需将 -A 替换为 -D 即可删除:

    iptables -D XYZ -p ...
    
  • 2

    执行相同的命令,但将“-A”替换为“-D” . 例如:

    iptables -A ...
    

    iptables -D ...
    
  • 29

    您也可以使用规则编号( --line-numbers ):

    iptables -L INPUT --line-numbers
    

    示例输出:

    Chain INPUT (policy ACCEPT) 
        num  target prot opt source destination
        1    ACCEPT     udp  --  anywhere  anywhere             udp dpt:domain 
        2    ACCEPT     tcp  --  anywhere  anywhere             tcp dpt:domain 
        3    ACCEPT     udp  --  anywhere  anywhere             udp dpt:bootps 
        4    ACCEPT     tcp  --  anywhere  anywhere             tcp dpt:bootps
    

    所以,如果你想删除第二条规则:

    iptables -D INPUT 2
    

    更新

    如果你使用(d)一个特定的表(例如nat),你必须将它添加到删除命令(thx到@ThorSummoner的评论)

    sudo iptables -t nat -D PREROUTING 1
    
  • 4

    使用 -D 命令,这是 man 页面解释的方式:

    -D, --delete chain rule-specification
    -D, --delete chain rulenum
        Delete  one  or more rules from the selected chain.  
        There are two versions of this command: 
        the rule can be specified as a number in the chain (starting at 1 for the first rule) or a rule to match.
    

    Do realize 此命令与所有其他命令( -A-I )一样适用于某些表 . 如果您没有使用默认表( filter 表),请使用 -t TABLENAME 指定该目标表 .

    删除要匹配的规则

    iptables -D INPUT -i eth0 -p tcp --dport 443 -j ACCEPT
    

    注意:这仅删除匹配的第一个规则 . 如果您有许多匹配的规则(这可能发生在iptables中),请多次运行 .

    删除指定为数字的规则

    iptables -D INPUT 2
    

    除了计算数字,您可以使用 --line-number 参数列出行号,例如:

    iptables -t nat -nL --line-number
    
  • 395

    假设,如果要删除NAT规则,

    使用以下命令列出附加的IPtables,

    # sudo iptables -L -t nat -v
    
    Chain PREROUTING (policy ACCEPT 18 packets, 1382 bytes)
     pkts bytes target     prot opt in     out     source               destination         
        7   420 DNAT       tcp  --  any    any     anywhere             saltmaster           tcp dpt:http to:172.31.5.207:80
        0     0 DNAT       tcp  --  eth0   any     anywhere             anywhere             tcp dpt:http to:172.31.5.207:8080
    

    如果您想从IPtables中删除nat规则,只需执行命令,

    # sudo iptables -F -t nat -v
    
    Flushing chain `PREROUTING'
    Flushing chain `INPUT'
    Flushing chain `OUTPUT'
    Flushing chain `POSTROUTING'
    

    然后,你可以验证,

    # sudo iptables -L -t nat -v
    

相关问题