假设某服务器有一个接口 eth0
,并配置了三个 IP 地址 10.0.0.4
, 10.0.0.5
, 10.0.0.6
。需求就是,如何知道这一台服务器每个 IP 的历史出网流量。
思路由 @a350063 提供,很简单,活用 iptables
的计数器就 OK 了。这种方法非常灵活,可以对任何能用 iptables 规则描述的流量数据进行统计,比如统计某个源地址指定端口的进出流量。首先我们使用 iptables
增加一些规则:
# iptables -I OUTPUT -s 10.0.0.4/32 -j ACCEPT # iptables -I OUTPUT -s 10.0.0.5/32 -j ACCEPT # iptables -I OUTPUT -s 10.0.0.6/32 -j ACCEPT
执行完成之后,就可以直接使用命令 iptables -nvxL OUTPUT
看到每个 IP 的出网流量了:
# iptables -nvxL OUTPUT Chain OUTPUT (policy ACCEPT 3856 packets, 1914416 bytes) pkts bytes target prot opt in out source destination 12880 11794276 ACCEPT all -- * * 10.0.0.4 0.0.0.0/0 6850 5801012 ACCEPT all -- * * 10.0.0.5 0.0.0.0/0 110 9315 ACCEPT all -- * * 10.0.0.6 0.0.0.0/0