iptables 统计特定端口流量
现在需要对某一个公网服务器上的特定端口的上/下行流量进行统计,确定某一时段内该端口的总的流量使用量。
添加规则
例如要统计 10080 端口的流量,添加 入站
规则:
# iptables -I INPUT -p tcp --dport 10080
添加 出站
规则:
# iptables -I OUTPUT -p tcp --sport 10080
同时设定多个端口
# iptables -I OUTPUT -p tcp -m multiport --sports 10022,10080
端口流量限额
例如要给 10080 端口指定 50GB 的下行流量配额:
先插入 DROP 规则
# iptables -I OUTPUT -p tcp --sport 10080 -j DROP
再插入限额规则
# iptables -I OUTPUT -p tcp --sport 10080 -m quota --quota 50000000000 -j ACCPECT
查看流量统计
# iptable -L -v -n
查看某个链的数据
# iptables -L OUTPUT -nv
输出样本:
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
690 5946K tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport sports 10022,10080
35 11459 tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp spt:18008
重置统计
重置入站
# iptables -Z INPUT
重置出站
# iptables -Z OUTPUT
可以使用
-L
指定链,仅重置特定链的统计数据。
移除规则
按规则移除
移除入站规则
# iptables -Z INPUT -p tcp --dport 10080
移除出站规则
# iptables -Z OUTPUT -p tcp --sport 10080
通过编号移除
查看规则编号
# iptables -L OUTPUT --line-number
# iptables -D OUTPUT 1
Discovery
- https://blog.csdn.net/u014015972/article/details/50647039
- https://www.liangzl.com/get-article-detail-15945.html
- https://www.jb51.net/article/114693.htm
- https://www.v2ex.com/t/280507
- https://www.digitalocean.com/community/tutorials/how-to-list-and-delete-iptables-firewall-rules
- https://linux.die.net/man/8/iptables