hana_shinのLinux技術ブログ

Linuxの技術情報を掲載しています。特にネットワークをメインに掲載していきます。

iftopコマンドの使い方



1 iftopコマンドとは?

iftopコマンドは、帯域を測定するツールです。TCPコネクション単位で帯域を確認することができます。ここでは、iperf3コマンドを使ってiftopの動作確認をしてみます。なお、iperf3コマンドの使い方は、iperf3コマンドの使い方 - hana_shinのLinux技術ブログを参照ください。

2 検証環境

2.1 ネットワーク構成

サーバとクライアントの2台構成です。図中のeth0はNICの名前です。

                               192.168.2.0/24
client(eth0) ------------------------------------------(eth0) server
        .105                                           .120

2.2 版数

CentOS版数は、サーバ、クライアントともに下記版数です。

[root@server ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

カーネル版数は以下のとおりです。

[root@server ~]# uname  -r
3.10.0-1160.el7.x86_64

3 インストール方法

iftopパッケージはepelリポジトリにあるので、まず、epel-releaseパッケージをインストールします。

[root@client ~]# yum -y install epel-release

クライアントにiftopパッケージをインストールします。

[root@client ~]# yum -y install iftop

iftopコマンドの版数を確認します。

[root@client ~]# iftop -h
-snip-
iftop, version 1.0pre4

4 事前準備

サーバでポート番号を開放します。iperf3は,TCP/UDPの5201番ポートを使うので、5201番ポートへのアクセスを許可しておきます。まず、TCPの5201番ポートを開放します。firewall-cmdの使い方は、firewall-cmdの使い方 - hana_shinのLinux技術ブログを参照してください。

[root@server ~]# firewall-cmd --add-port=5201/tcp
success

ポートの状態を確認します。TCPの5201番ポートが開放されたことがわかります。

[root@server ~]# firewall-cmd --list-ports
5201/tcp

次に、UDPの5201番ポートを開放します。

[root@server ~]# firewall-cmd --add-port=5201/udp
success

ポートの状態を確認します。UDPの5201番ポートが開放されたことがわかります。

[root@server ~]# firewall-cmd --list-ports
5201/tcp 5201/udp

5 オプション一覧

オプションは以下のとおりです。

[root@client ~]#  iftop -h
iftop: display bandwidth usage on an interface by host

Synopsis: iftop -h | [-npblNBP] [-i interface] [-f filter code]
                               [-F net/mask] [-G net6/mask6]

   -h                  display this message
   -n                  don't do hostname lookups
   -N                  don't convert port numbers to services
   -p                  run in promiscuous mode (show traffic between other
                       hosts on the same network segment)
   -b                  don't display a bar graph of traffic
   -B                  display bandwidth in bytes
   -a                  display bandwidth in packets
   -i interface        listen on named interface
   -f filter code      use filter code to select packets to count
                       (default: none, but only IP packets are counted)
   -F net/mask         show traffic flows in/out of IPv4 network
   -G net6/mask6       show traffic flows in/out of IPv6 network
   -l                  display and count link-local IPv6 traffic (default: off)
   -P                  show ports as well as hosts
   -m limit            sets the upper limit for the bandwidth scale
   -c config file      specifies an alternative configuration file
   -t                  use text interface without ncurses

   Sorting orders:
   -o 2s                Sort by first column (2s traffic average)
   -o 10s               Sort by second column (10s traffic average) [default]
   -o 40s               Sort by third column (40s traffic average)
   -o source            Sort by source address
   -o destination       Sort by destination address

   The following options are only available in combination with -t
   -s num              print one single text output afer num seconds, then quit
   -L num              number of lines to print

iftop, version 1.0pre4
copyright (c) 2002 Paul Warren <pdw@ex-parrot.com> and contributors

6 基本的な使い方

iperf3コマンドを使って、クライアントからサーバに10(Mbit/s)、30(Mbit/s)でパケットを送信してみます。このとき、iftopコマンドでクライアント/サーバ間の帯域を確認してみます。

6.1 10(Mbit/s)の場合

サーバでiperf3コマンドを実行します。

[root@server ~]# iperf3 -s
-----------------------------------------------------------
Server	listening	on	5201
-----------------------------------------------------------

クライアントでiperf3を実行します。テスト時間は600秒、帯域は10(Mbit/s)を指定します。

[root@client ~]# iperf3 -c server -t 600 -b 10M

クライアントでターミナルをもう1つ開き、iftopコマンドを実行します。
直近2秒の帯域は、クライアントからサーバは9.52Mb(★)、サーバからクライアンは7.72Kb(●)であることがわかります。

[root@client ~]# iftop -i eth0 -t
-snip-
============================================================================================

   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
   1 client                                   =>   ★9.52Mb     9.52Mb     9.27Mb     37.1MB
     server                                   <=   ●7.72Kb     10.3Kb     8.53Kb     34.1KB
--------------------------------------------------------------------------------------------
Total send rate:                                     9.52Mb     9.53Mb     9.27Mb
Total receive rate:                                  9.38Kb     11.0Kb     9.21Kb
Total send and receive rate:                         9.53Mb     9.54Mb     9.28Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     10.0Mb     12.1Kb     10.0Mb
Cumulative (sent/received/total):                    37.1MB     36.8KB     37.1MB
============================================================================================
-snip-

6.2 30(Mbit/s)の場合

サーバでiperf3コマンドを実行します。

[root@server ~]# iperf3 -s
-----------------------------------------------------------
Server	listening	on	5201
-----------------------------------------------------------

クライアントでiperf3を実行します。テスト時間は600秒、帯域は30(Mbit/s)を指定します。

[root@client ~]# iperf3 -c server -t 600 -b 30M

クライアントでターミナルをもう1つ開き、iftopコマンドを実行します。
直近2秒の帯域は、クライアントからサーバは28.5Mb(★)、サーバからクライアンは15.8Kb(●)であることがわかります。

[root@client ~]# iftop -i eth0 -t
-snip-
============================================================================================

   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
   1 client                                   =>   ★28.5Mb     28.9Mb     28.9Mb      159MB
     server                                   <=   ●15.8Kb     16.9Kb     17.3Kb     98.7KB
--------------------------------------------------------------------------------------------
Total send rate:                                     28.5Mb     28.9Mb     28.9Mb
Total receive rate:                                  16.4Kb     17.6Kb     18.1Kb
Total send and receive rate:                         28.6Mb     29.0Mb     28.9Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     30.0Mb     22.7Kb     30.1Mb
Cumulative (sent/received/total):                     159MB      106KB      159MB
============================================================================================
-snip-

7 ホスト名をIPアドレスで表示する方法(-n)

-nは、ホスト名をIPアドレスで表示するオプションです。

サーバでiperf3コマンドを実行します。

[root@server ~]# iperf3 -s
-----------------------------------------------------------
Server	listening	on	5201
-----------------------------------------------------------

クライアントでiperf3を実行します。テスト時間は600秒、帯域は10(Mbit/s)を指定します。

[root@client ~]# iperf3 -c server -t 600 -b 10M

クライアントでターミナルをもう1つ開き、iftopコマンドを実行します。ホスト名(server,client)ではなくIPアドレスが表示されていることがわかります。

[root@client ~]# iftop -i eth0 -t -n
-snip-
============================================================================================

   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
   1 192.168.2.105                            =>     9.52Mb     9.52Mb     8.23Mb     41.2MB
     192.168.2.100                            <=     7.72Kb     8.08Kb     7.86Kb     39.3KB
--------------------------------------------------------------------------------------------
Total send rate:                                     9.52Mb     9.52Mb     8.24Mb
Total receive rate:                                  8.26Kb     8.62Kb     8.71Kb
Total send and receive rate:                         9.53Mb     9.53Mb     8.25Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     10.0Mb     13.7Kb     10.0Mb
Cumulative (sent/received/total):                    2.98GB     1.24MB     2.98GB
============================================================================================
-snip-

8 ポート番号を表示する方法(-P)

-Pは、ポート番号を表示するオプションです。

サーバでiperf3コマンドを実行します。

[root@server ~]# iperf3 -s
-----------------------------------------------------------
Server	listening	on	5201
-----------------------------------------------------------

クライアントでiperf3を実行します。テスト時間は600秒、帯域は10(Mbit/s)を指定します。

[root@client ~]# iperf3 -c server -t 600 -b 10M

クライアントでターミナルをもう1つ開き、iftopコマンドを実行します。ポート番号(54122)、サービス名(targus-getdata1)が表示されていることがわかります。サービス名は/etc/servicesで定義されていて、5201番であることがわかります。

[root@client ~]# iftop -i eth0 -t -P
-snip-
============================================================================================

   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
   1 client:54122                             =>     9.52Mb     8.02Mb     8.02Mb     6.01MB
     server:targus-getdata1                   <=     7.72Kb     8.19Kb     8.19Kb     6.14KB
--------------------------------------------------------------------------------------------
Total send rate:                                     9.52Mb     8.02Mb     8.02Mb
Total receive rate:                                  8.26Kb     8.93Kb     8.93Kb
Total send and receive rate:                         9.53Mb     8.03Mb     8.03Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     10.0Mb     12.5Kb     10.0Mb
Cumulative (sent/received/total):                    6.02MB     6.69KB     6.02MB
============================================================================================

9 実行時間を指定する方法(-s)

-sは、iftopコマンドを実行する時間(秒)を指定するオプションです。指定した時間が経過すると、iftopコマンドが終了します。

今までと同じようにiperf3を実行します。今回はiperf3実行の様子は記載省略します。このとき、-sに10秒を指定してiftopコマンドを実行してみます。20:49:57に開始して20:50:07に終了していることがわかります。

[root@client ~]# date;iftop -i eth0 -t -s 10 ; date
2022年  1月 16日 日曜日 20:49:57 JST
interface: eth0
IP address is: 192.168.2.37
MAC address is: xx:xx:xx:yy:yy:yy
Listening on eth0
   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
   1 client                                   =>     9.52Mb     8.61Mb     8.61Mb     10.8MB
     server                                   <=     7.72Kb     7.64Kb     7.64Kb     9.55KB
   2 224.0.0.251                              =>         0b         0b         0b         0B
     hananoiPad                               <=     2.78Kb     3.35Kb     3.35Kb     4.19KB
   3 client                                   =>     1.22Kb     1.18Kb     1.18Kb     1.48KB
     DESKTOP-MB6G4KI                          <=       368b       368b       368b       460B
   4 client                                   =>         0b       226b       226b       282B
     WRC-2533GS2                              <=         0b       334b       334b       417B
--------------------------------------------------------------------------------------------
Total send rate:                                     9.52Mb     8.62Mb     8.62Mb
Total receive rate:                                  10.9Kb     11.7Kb     11.7Kb
Total send and receive rate:                         9.53Mb     8.63Mb     8.63Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     9.52Mb     17.0Kb     9.54Mb
Cumulative (sent/received/total):                    10.8MB     14.6KB     10.8MB
============================================================================================

2022年  1月 16日 日曜日 20:50:07 JST

10 filter codeの使い方(-f)

filter codeを使うと、特定の通信の帯域だけを表示することができます。たとえば、ポート番号やIPアドレスを指定することで、特定の通信だけに絞り込むことが可能です。

10.1 ポート番号で絞り込む方法(dst port/src port)

サーバでiperf3コマンドを実行します。

[root@server ~]# iperf3 -s
-----------------------------------------------------------
Server	listening	on	5201
-----------------------------------------------------------

クライアントでiperf3を実行します。テスト時間は600秒、帯域は10(Mbit/s)を指定します。

[root@client ~]# iperf3 -c server -t 600 -b 10M

クライアントでターミナルをもう1つ開きます。"dst port 5201"と指定してクライアントからサーバに向けての通信に絞り込んでみます。クライアントからサーバへの通信の帯域は約9.0Mbであることがわかります。一方、サーバからクライアントへの通信の帯域は0であることがわかります。

[root@client ~]# iftop -i eth0 -t -nP -f "dst port 5201"
-snip-
============================================================================================

   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
   1 192.168.2.105:48552                      =>     9.52Mb     9.62Mb     8.95Mb     17.9MB
     192.168.2.100:targus-getdata1            <=         0b         0b         0b         0B
--------------------------------------------------------------------------------------------
Total send rate:                                     9.52Mb     9.62Mb     8.95Mb
Total receive rate:                                      0b         0b         0b
Total send and receive rate:                         9.52Mb     9.62Mb     8.95Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     10.0Mb         0b     10.0Mb
Cumulative (sent/received/total):                    17.9MB         0B     17.9MB
============================================================================================
-snip-

今度は、サーバからクライアントに向けての通信に絞り込んでみます。”src port 5201"と指定してサーバからクライアントに向けての通信に絞り込んでみます。クライアントからサーバへの通信の帯域は0であることがわかります。一方、サーバからクライアントへの通信の帯域は約7Kbであることがわかります。この帯域は、サーバからクライアントに送信されるTCPのACKによるものです。

[root@client ~]# iftop -i eth0 -t -nP -f "src port 5201"
-snip-
============================================================================================

   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
   1 192.168.2.105:48552                      =>         0b         0b         0b         0B
     192.168.2.100:targus-getdata1            <=     7.72Kb     7.80Kb     7.11Kb     10.7KB
--------------------------------------------------------------------------------------------
Total send rate:                                         0b         0b         0b
Total receive rate:                                  7.72Kb     7.80Kb     7.11Kb
Total send and receive rate:                         7.72Kb     7.80Kb     7.11Kb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                         0b     8.12Kb     8.12Kb
Cumulative (sent/received/total):                        0B     10.7KB     10.7KB
============================================================================================
-snip-

10.2 IPアドレスで絞り込む方法(dst host/src host)

考え方はポート番号の時と同じですので、詳細の説明は割愛します。
送信先Pアドレスが192.168.2.10の通信を絞り込んでみます。

[root@client ~]# iftop -i eth0 -t -nP -f "dst host 192.168.2.100"
-snip-
============================================================================================

   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
   1 192.168.2.105:48552                      =>     9.52Mb     8.56Mb     8.56Mb     10.7MB
     192.168.2.100:targus-getdata1            <=         0b         0b         0b         0B
--------------------------------------------------------------------------------------------
Total send rate:                                     9.52Mb     8.56Mb     8.56Mb
Total receive rate:                                      0b         0b         0b
Total send and receive rate:                         9.52Mb     8.56Mb     8.56Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     10.0Mb         0b     10.0Mb
Cumulative (sent/received/total):                    10.7MB         0B     10.7MB
============================================================================================

今度は、送信元IPアドレスが192.168.2.10の通信を絞り込んでみます。

[root@client ~]# iftop -i eth0 -t -nP -f "src host 192.168.2.100"
-snip-
============================================================================================

   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
   1 192.168.2.105:48552                      =>         0b         0b         0b         0B
     192.168.2.100:targus-getdata1            <=     7.72Kb     7.24Kb     7.24Kb     5.43KB
--------------------------------------------------------------------------------------------
Total send rate:                                         0b         0b         0b
Total receive rate:                                  7.72Kb     7.24Kb     7.24Kb
Total send and receive rate:                         7.72Kb     7.24Kb     7.24Kb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                         0b     8.53Kb     8.53Kb
Cumulative (sent/received/total):                        0B     5.43KB     5.43KB
============================================================================================

Z 参考情報

私が業務や記事執筆で参考にした書籍を以下のページに記載します。
Linux技術のスキルアップをしよう! - hana_shinのLinux技術ブログ