如何列出和删除iptables规则
时间:2020-03-05 15:28:47 来源:igfitidea点击:
iptables是一个防火墙实用程序,它是默认的所有Linux发行版中可用。
通常用于设置,维护和检查Linux内核中的数据包过滤规则的表。
需要删除不再需要或者添加的规则。
完成本教程后,我们将学习不同的方式来列出和删除iptables规则。
要运行iptables命令,我们需要root或者sudo用户权限。
要列出(查看)IPTABLE中的所有规则
要列出所有链中的所有规则(输入,输出和转发),iptables使用- list
或者-l
选项。
检查iptables列表语法:
$sudo iptables --list or $sudo iptables -L
下面的输出示例显示所有链条没有规则:
# 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
让我向我们展示另一个具有"输出"链中规则的示例。
# iptables -L Chain INPUT (policy ACCEPT) Target prot opt source destination Chain FORWARD (policy ACCEPT) Target prot opt source destination Chain INPUT (policy ACCEPT) Target prot opt source destination DROP icmp -- anywhere anywhere icmp echo-request
删除所有iptables规则(所有链条)
当我们要查看链中的所有规则时,可以轻松使用刷新选项。
让我们查看如何在本节中使用-f
或者--flush
。
在删除所有规则时要小心,因为它可能会从终端中删除SSH访问服务器。
如果发生这种情况,那么唯一的选项是通过控制台设置访问。
以下命令将从iptably中刷新所有链条:
$sudo iptables --flush or $sudo iptables -F
执行上述命令后,Iptables将完全清楚。
我们可以使用iptables --list
命令验证输出。
删除特定链条
删除特定链,我们需要使用-f
或者- flush
,然后是链名。
让我们来检查一个例子以完全删除来自iptables的"输出"链。
以下命令将删除输出链中的所有规则:
$sudo iptables -F OUTPUT
如何删除链内的特定规则
可以删除链内的特定规则。
我们有两个选项删除规则匹配或者指定的规则编号。
通过规则匹配删除
以下命令通过匹配删除输入链中的规则。
$sudo iptables -D INPUT -i eth0 -p tcp --dport 22 -j ACCEPT
通过指定的规则编号删除
要按特定号码删除规则,我们必须先列出规则编号(iptables -l输入--lower-numbers
)。
以下是一个示例,显示了"输入"链中规则的行号。
$sudo 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 . . .
所以,现在我们有规则编号。
让我们继续使用"-d""选项删除第二条规则。
$sudo iptables -D INPUT 2
让我们选中要删除所有NAT规则的一个实用示例,然后使用以下命令。
列出所有NAT规则
$sudo iptables -L -t nat -v
删除所有NAT规则
$sudo iptables -F -t nat -v