• Linux IPTables Flush:删除/删除 RedHat 和 CentOS Linux 上的所有规则(转载)


    【摘要】 在基于 Red Hat 的 Linux 上,iptables 带有某些默认规则。清理它们并从头开始是个好主意。在我们开始学习如何使用 iptables 添加防火墙规则之前,了解如何清理所有现有的默认规则并从头开始一切会很有帮助。IPTables 中的默认规则如下图启动iptables防火墙。# service iptables statusFirewall is stopped.# serv...

    在基于 Red Hat 的 Linux 上,iptables 带有某些默认规则。清理它们并从头开始是个好主意。

    在我们开始学习如何使用 iptables 添加防火墙规则之前,了解如何清理所有现有的默认规则并从头开始一切会很有帮助。

    IPTables 中的默认规则

    如下图启动iptables防火墙。

    1. # service iptables status
    2. Firewall is stopped.
    3. # service iptables start
    4. Applying iptables firewall rules: [ OK ]
    5. Loading additional iptables modules: ip_conntrack_netbios_n[ OK ]

    可以看到下面的默认规则:iptables -> Filter Table -> RH-Firewall-1-INPUT Chain,如下图。您还可以使用 'iptables –list' 查看所有规则。

    1. # service iptables status
    2. Table: filter
    3. Chain INPUT (policy ACCEPT)
    4. num target prot opt source destination
    5. 1 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0
    6. Chain FORWARD (policy ACCEPT)
    7. num target prot opt source destination
    8. 1 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0
    9. Chain OUTPUT (policy ACCEPT)
    10. num target prot opt source destination
    11. Chain RH-Firewall-1-INPUT (2 references)
    12. num target prot opt source destination
    13. 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
    14. 2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 255
    15. 3 ACCEPT esp -- 0.0.0.0/0 0.0.0.0/0
    16. 4 ACCEPT ah -- 0.0.0.0/0 0.0.0.0/0
    17. 5 ACCEPT udp -- 0.0.0.0/0 224.0.0.251 udp dpt:5353
    18. 6 ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:631
    19. 7 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:631
    20. 8 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
    21. 9 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
    22. 10 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

    IPTables 规则存储在 /etc/sysconfig/iptables

    请注意,iptables 规则存储在 /etc/sysconfig/iptables 文件中。如果您查看此文件,您将看到所有默认规则。

    1. # cat /etc/sysconfig/iptables
    2. # Firewall configuration written by system-config-securitylevel
    3. # Manual customization of this file is not recommended.
    4. *filter
    5. :INPUT ACCEPT [0:0]
    6. :FORWARD ACCEPT [0:0]
    7. :OUTPUT ACCEPT [0:0]
    8. :RH-Firewall-1-INPUT - [0:0]
    9. -A INPUT -j RH-Firewall-1-INPUT
    10. -A FORWARD -j RH-Firewall-1-INPUT
    11. -A RH-Firewall-1-INPUT -i lo -j ACCEPT
    12. -A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
    13. -A RH-Firewall-1-INPUT -p 50 -j ACCEPT
    14. -A RH-Firewall-1-INPUT -p 51 -j ACCEPT
    15. -A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
    16. -A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
    17. -A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
    18. -A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    19. -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
    20. -A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
    21. COMMIT

    暂时删除所有防火墙规则

    使用 'iptables –flush' 选项暂时删除所有规则。

    1. # iptables --flush
    2. # iptables --list
    3. Chain INPUT (policy ACCEPT)
    4. target prot opt source destination
    5. Chain FORWARD (policy ACCEPT)
    6. target prot opt source destination
    7. Chain OUTPUT (policy ACCEPT)
    8. target prot opt source destination
    9. Chain RH-Firewall-1-INPUT (0 references)
    10. target prot opt source destination

    在“iptables –flush”之后,如果您重新启动 iptables,您将再次看到所有默认规则。所以,–flush 只是暂时的。

    1. # service iptables stop
    2. # service iptables start
    3. # iptables --list

    永久删除所有默认防火墙规则

    在删除所有防火墙规则之前,您将在 /etc/sysconfig/iptables 文件中看到以下内容。

    1. # cat /etc/sysconfig/iptables
    2. # Firewall configuration written by system-config-securitylevel
    3. # Manual customization of this file is not recommended.
    4. *filter
    5. :INPUT ACCEPT [0:0]
    6. :FORWARD ACCEPT [0:0]
    7. :OUTPUT ACCEPT [0:0]
    8. :RH-Firewall-1-INPUT - [0:0]
    9. -A INPUT -j RH-Firewall-1-INPUT
    10. -A FORWARD -j RH-Firewall-1-INPUT
    11. -A RH-Firewall-1-INPUT -i lo -j ACCEPT
    12. -A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
    13. -A RH-Firewall-1-INPUT -p 50 -j ACCEPT
    14. -A RH-Firewall-1-INPUT -p 51 -j ACCEPT
    15. -A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
    16. -A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
    17. -A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
    18. -A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    19. -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
    20. -A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
    21. COMMIT

    First, flush all these rules temporarily, as we discussed above.

    首先,正如我们上面讨论的那样,暂时刷新所有这些规则。

    # iptables --flush

    接下来,使用“service iptables save”将当前的 iptables(它是空的,因为我们刚刚刷新它)保存到 /etc/sysconfig/iptables 文件以供永久使用

    1. # service iptables save
    2. Saving firewall rules to /etc/sysconfig/iptables: [ OK ]

    最后,查看 /etc/sysconfig/iptables 以确保没有规则。

    1. # cat /etc/sysconfig/iptables
    2. # Generated by iptables-save v1.3.5 on Thu Oct 28 08:44:01 2020
    3. *filter
    4. :INPUT ACCEPT [102:7668]
    5. :FORWARD ACCEPT [0:0]
    6. :OUTPUT ACCEPT [78:8560]
    7. COMMIT
    8. # Completed on Thu Oct 28 08:44:01 2020

    现在,如果您停止并启动 iptables,您将不再看到默认规则。因此,请记住执行“service iptables save”以使“iptables –flush”永久化。

    1. # service iptables stop
    2. # service iptables start
    3. # iptables --list

    现在您了解了iptables的基础知识,以及如何清理所有现有规则以从头开始。

  • 相关阅读:
    C#面:关于集合的几个选择题
    Java 初学者必备核心基础知识有哪些?
    vue3 自定义loading
    IO基础操作和常识
    serialize 序列化原生方法
    【ARMv9 DSU-120 系列 2. -- DSU-120 Cluster 中组件详细介绍】
    Redis经典面试题
    Linux常用命令——bzdiff命令
    JUC学习笔记——并发工具线程池
    【etcd】go etcd实战二:分布式锁
  • 原文地址:https://blog.csdn.net/u013299671/article/details/126165990