iptables flush:删除 redhat 和 centos linux 上的所有规则-ag捕鱼王app官网

iptables flush:删除 redhat 和 centos linux 上的所有规则

作者:迹忆客 最近更新:2022/10/23 浏览次数:

在基于 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 防火墙规则。

上一篇:

下一篇:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图