iptables Flush:删除 RedHat 和 CentOS Linux 上的所有规则
在基于 Red Hat 的 Linux 上,iptables 带有某些默认规则。 清理它们并从头开始是个好主意。
本文是正在进行的 iptables 教程系列的一部分。 这是该系列的第 2 篇文章。 在我们的第一部分中,我们讨论了 IPTables 表、链、规则基础。
在我们开始学习如何使用 iptables 添加防火墙规则之前,了解如何清理所有现有的默认规则并从头开始一切都会很有帮助。
iptables 中的默认规则
启动 iptables 防火墙,如下所示。
$ service iptables status
Firewall is stopped.
$ service iptables start
Applying iptables firewall rules: [ OK ]
Loading additional iptables modules: ip_conntrack_netbios_n[ OK ]
可以在下面看到默认规则:iptables -> Filter Table -> RH-Firewall-1-INPUT Chain,如下所示。 我们还可以使用 iptables –list
查看所有规则。
$ service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
Chain RH-Firewall-1-INPUT (2 references)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 255
3 ACCEPT esp -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT ah -- 0.0.0.0/0 0.0.0.0/0
5 ACCEPT udp -- 0.0.0.0/0 224.0.0.251 udp dpt:5353
6 ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:631
7 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:631
8 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
9 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt: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 文件中。 如果我们查看此文件,将看到所有默认规则。
$ cat /etc/sysconfig/iptables
$ Firewall configuration written by system-config-securitylevel
$ Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
暂时删除所有防火墙规则
使用 iptables --flush
选项临时删除所有规则。
$ iptables --flush
$ iptables --list
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 RH-Firewall-1-INPUT (0 references)
target prot opt source destination
在iptables --flush
之后,如果你重新启动 iptables,你会再次看到所有的默认规则。 所以,--flush
只是暂时的。
$ service iptables stop
$ service iptables start
$ iptables --list
永久删除所有默认防火墙规则
在删除所有防火墙规则之前,我们将在 /etc/sysconfig/iptables 文件中看到以下内容。
$ cat /etc/sysconfig/iptables
# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
首先,如上所述,暂时清除所有这些规则。
$ iptables --flush
接下来,使用 service iptables save
将当前的 iptables(它是空的,因为我们刚刚刷新它)保存到 /etc/sysconfig/iptables 文件以供永久使用
$ service iptables save
Saving firewall rules to /etc/sysconfig/iptables: [ OK ]
最后,查看 /etc/sysconfig/iptables 以确保没有规则。
$ cat /etc/sysconfig/iptables
# Generated by iptables-save v1.3.5 on Thu Oct 28 08:44:01 2010
*filter
:INPUT ACCEPT [102:7668]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [78:8560]
COMMIT
# Completed on Thu Oct 21 08:44:01 2022
现在,如果我们停止并启动 iptables,我们将不会再看到默认规则。 因此,请记住执行 service iptables save
以使 iptables –flush
永久化。
$ service iptables stop
$ service iptables start
$ iptables --list
现在我们了解了 iptables 的基础知识,以及如何清理所有现有规则以从头开始。 在我们的下一篇文章中,我们将通过几个实际示例了解如何开始添加新的 iptables 防火墙规则。
相关文章
解决 Linux Bash 中的 Nodemon 命令未找到错误
发布时间:2024/03/14 浏览次数:223 分类:操作系统
-
本文介绍如何解决 Linux Bash 中的 nodemon command not found 错误。
解决 Linux Bash 中的 Make Command Not Found 错误
发布时间:2024/03/14 浏览次数:246 分类:操作系统
-
本文介绍如何解决 Linux Bash 中的 make command not found 错误。
解决 Linux Bash 中 syntax error near unexpected token newline 错误
发布时间:2024/03/14 浏览次数:408 分类:操作系统
-
本文介绍如何解决 Linux Bash 中 syntax error near unexpected token newline 错误。
使用 PowerShell 将文件从 Windows 复制到 Linux
发布时间:2024/02/08 浏览次数:571 分类:编程语言
-
本教程将教你使用 PowerShell 将文件从 Windows 复制到 Linux。
等效于 Linux ls 的 PowerShell 命令
发布时间:2024/02/07 浏览次数:206 分类:编程语言
-
本教程将为 Linux ls 命令介绍不同的 PowerShell 等效命令。