さこ街浪漫

GO ON A HIGHWAY

Linux(CentOS) の通信トラフィック(ポート別)

概要

CentOS でポート別に通信量を知る方法を記録しておく。iptables コマンドを使用する場合。

環境
準備

iptables を使用してポートを開けておくこと。INPUT チェインでは REJECT よりも手前(=上)に挿入しなければならないことに注意する。

# service iptables status
# iptables --insert INPUT 位置 --proto tcp --destination-port 80 --jump ACCEPT
# iptables --insert OUTPUT 位置 --proto tcp --source-port 80 --jump ACCEPT
# service iptables save
手順

bytes(左から二列目) に着目。

# iptables --list -nvx
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    pkts      bytes target     prot opt in     out     source               destination
    4043   585653 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
       0        0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
       1       60 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
       2      104 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
     291    15132 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:80
      39     3042 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:137
       9     2106 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:138
       0        0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:139
       1       52 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:445
       0        0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:10050
       0        0 LOG        all  --  *      *       0.0.0.0/0            0.0.0.0/0           LOG flags 0 level 4 prefix `[Netfilter rejected]: '
       0        0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    pkts      bytes target     prot opt in     out     source               destination
       0        0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 596 packets, 50070 bytes)
    pkts      bytes target     prot opt in     out     source               destination
    1561   425898 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp spt:22
    3999  5738870 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp spt:80
       0        0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp spt:137
       0        0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp spt:138
       0        0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp spt:139
      13     2075 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp spt:445
       0        0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp spt:10050
メモ
  • iptables を restart すると pkts や bytes がリセットされてしまうことに注意。
  • MySQL の 3306 は unix ドメインソケットを使用しているため、iptables ではうまく計測できない。