Linux 如何检查 iptables 中每个规则的命中计数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17548383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:12:01  来源:igfitidea点击:

How can I check the hit count for each rule in iptables?

linuxfirewalliptables

提问by apps

I want to know how can I find out which rule was accessed and how many times, from the access list I have created using iptables.

我想知道如何从我使用 iptables 创建的访问列表中找出访问了哪个规则以及访问了多少次。

My firewall has over 1000 input and output rules in iptbales; I want to find how many times each of them were accessed.

我的防火墙在 iptbales 中有超过 1000 条输入和输出规则;我想找出他们每个人被访问的次数。

For example, suppose I have the following rules:

例如,假设我有以下规则:

iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT

iptables -A OUTPUT -o eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

iptables -A INPUT -i eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT

iptables -A OUTPUT -o eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT

I want to find out how many times each of the rules 1, 2, 3, 4, 5 and 6 were hit.

我想知道每个规则 1、2、3、4、5 和 6 被击中了多少次。

采纳答案by scai

iptableswill list packet and byte counters if you specify option -vfor verbose, e.g. iptables -vL. Likewise iptables-savewill list all entries including the mentioned counters for each chain, but not for each table entry (on some systems iptables-saverequires option -cto include counters).

iptables如果您-vverbose指定选项,则将列出数据包和字节计数器,例如iptables -vL. 同样iptables-save将列出所有条目,包括每个链的提到的计数器,但不是每个表条目(在某些系统上iptables-save需要选择-c包括计数器)。

回答by dothebart

You can also use collectds iptables module to aggregate the counters:

您还可以使用 collectds iptables 模块来聚合计数器:

https://collectd.org/wiki/index.php/Iptables

https://collectd.org/wiki/index.php/Iptables

回答by Bob Holden

I use the following to check on my iptables rules:

我使用以下命令检查我的 iptables 规则:

iptables -nvL [INPUT|FORWARD|OUTPUT|myCHAINNAME] --line-numbers | less

The -n speeds up the process by not doing hostname lookups

-n 通过不进行主机名查找来加快进程

The line numbers help with deleting rules:

行号有助于删除规则:

iptables -D [INPUT|FORWARD|OUTPUT|myCHAINNAME] [Rule#]

HTH

HTH