How to Monitor Network Traffic with vnStat on Ubuntu 22.04

vnStat is a free, open-source, and console-based network traffic monitoring tool for Linux operating system. With vnStat, you can monitor network statistics over various time periods. It is simple, lightweight, and consumes a small portion of your system resources. vnStat allow you to generate the network traffic data in an hour, day, month, week, and day. This tutorial will show you how to install and use vnStat to monitor network traffic.

Features

  • Monitor multiple interfaces at the same time.
  • Provides several output options, such as day-wise, week-wise, etc.
  • Sort the data by hour, day, month, or week or get the top 10 days.
  • Allows you to generate output in image format.
  • Monitor multiple network interfaces at the same time.
  • Lightweight and low CPU usage.

This tutorial will show you how to install and use the VnStat tool to monitor network traffic on Ubuntu 22.04.

Prerequisites

  • A server running Ubuntu 20.04.
  • A root password is configured on the server.

Getting Started

Before starting, updating and upgrading all system packages to the latest version is recommended. You can update all of them using the following command:

apt update -y
apt upgrade -y

Once all the packages are updated, you can install the VnStat from the source in the next step.

Install vnStat

By default, the latest version of vnStat is not available in the Ubuntu default repository. So you will need to compile it from the source.

First, install all the dependencies required to compile vnStat with the following command:

apt-get install build-essential gcc make libsqlite3-dev -y

Once all the dependencies are installed, download the latest version of the vnStat source with the following command:

wget https://humdi.net/vnstat/vnstat-2.10.tar.gz

Once the download is completed, extract the downloaded file with the following command:

tar -xvzf vnstat-2.10.tar.gz

Next, change the directory to the extracted directory and configure it with the following command:

cd vnstat-2.10
./configure --prefix=/usr --sysconfdir=/etc

Next, install the vnStat by running the following command:

make
make install

Once vnStat is installed, you can verify the installed version of vnStat with the following command:

vnstat -v

You should get the following output:

vnStat 2.10 by Teemu Toivola 

Once you are finished, you can proceed to the next step.

Use Systemd To Manage VnStat

To manage VnStat with systemd, you will need to copy the vnStat systemd service file from the vnStat source to the /etc/systemd/system/ directory:

cp -v vnstat-2.10/examples/systemd/vnstat.service /etc/systemd/system/

Next, enable the vnStat service and start it with the following command:

systemctl enable vnstat
systemctl start vnstat

You can also check the status of vnStat service using the following command:

systemctl status vnstat

You should get the following output:

? vnstat.service - vnStat network traffic monitor
   Loaded: loaded (/lib/systemd/system/vnstat.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2022-11-29 08:34:08 IST; 2h 49min ago
     Docs: man:vnstatd(1)
           man:vnstat(1)
           man:vnstat.conf(5)
 Main PID: 1730 (vnstatd)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/vnstat.service
           ??1730 /usr/sbin/vnstatd -n

Nov 29 08:34:08 vyompc systemd[1]: Started vnStat network traffic monitor.
Nov 29 08:34:08 vyompc vnstatd[1730]: Info: vnStat daemon 1.18 started. (pid:1730 uid:122 gid:128)
Nov 29 08:34:08 vyompc vnstatd[1730]: Info: Monitoring: enp20s0 (1000 Mbit) wlp22s0 (1000 Mbit)

Once you are finished, you can proceed to the next step.

How to Monitor Network Traffic with vnStat

vnStat provides several options that allow you to monitor the network traffic. You can list all available options with the following command:

vnstat --help

You should get the following output:

vnStat 2.10 by Teemu Toivola 

      -5,  --fiveminutes [limit]   show 5 minutes
      -h,  --hours [limit]         show hours
      -hg, --hoursgraph            show hours graph
      -d,  --days [limit]          show days
      -m,  --months [limit]        show months
      -y,  --years [limit]         show years
      -t,  --top [limit]           show top days

      -b, --begin            set list begin date
      -e, --end              set list end date

      --oneline [mode]             show simple parsable format
      --json [mode] [limit]        show database in json format
      --xml [mode] [limit]         show database in xml format

      -tr, --traffic [time]        calculate traffic
      -l,  --live [mode]           show transfer rate in real time
      -i,  --iface      select interface

Use "--longhelp" or "man vnstat" for complete list of options.

Before using vnStat, you will need to wait for some time to update the vnStat database. After updating the database, you can run vnStat without any option as shown below:

vnstat

You should see the following output:

                      rx      /      tx      /     total    /   estimated
 wlp22s0:
       Oct '22     17.80 GiB  /    4.35 GiB  /   22.15 GiB
       Nov '22     16.61 GiB  /    3.62 GiB  /   20.23 GiB  /   21.48 GiB
     yesterday    332.22 MiB  /  114.17 MiB  /  446.39 MiB
         today    710.25 MiB  /   44.85 MiB  /  755.11 MiB  /    1.55 GiB

To use the specific network interface, use the -i option as shown below:

vnstat -i ens3

You should get the daily and monthly statistics in the following output:

Database updated: 2020-11-12 06:30:00

   ens3 since Saturday 29 January 2022

          rx:  185.89 GiB      tx:  38.82 GiB      total:  224.70 GiB

   monthly
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       Oct '22     17.80 GiB |    4.35 GiB |   22.15 GiB |   71.04 kbit/s
       Nov '22     16.61 GiB |    3.62 GiB |   20.23 GiB |   71.20 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated     17.64 GiB |    3.85 GiB |   21.48 GiB |

   daily
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     yesterday    332.22 MiB |  114.17 MiB |  446.39 MiB |   43.34 kbit/s
         today    710.25 MiB |   44.85 MiB |  755.11 MiB |  154.31 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated      1.46 GiB |      92 MiB |    1.55 GiB |

To display the hourly statistics, run the following command:

vnstat -h

You should get the following output:

 ens3  /  hourly

 h  rx (MiB)   tx (MiB)  ][  h  rx (MiB)   tx (MiB)  ][  h  rx (MiB)   tx (MiB) 
12        5.4        0.2 ][ 20       30.4       14.8 ][ 04        0.0        0.0
13       72.7       15.0 ][ 21       24.8        6.4 ][ 05        0.0        0.0
14        1.9        0.4 ][ 22       20.1        9.8 ][ 06        0.0        0.0
15        0.0        0.0 ][ 23        0.0        0.0 ][ 07        0.0        0.0
16        0.0        0.0 ][ 00        0.0        0.0 ][ 08      147.0       10.2
17       36.4        9.6 ][ 01        0.0        0.0 ][ 09       64.1        9.4
18       54.0       23.4 ][ 02        0.0        0.0 ][ 10      496.9       23.9
19       86.5       34.5 ][ 03        0.0        0.0 ][ 11        2.3        1.3

To display the daily statistics, run the following command:

vnstat -d

You should get the following output:

 ens3  /  daily
        day         rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     Sunday 30 October 2022  266.75 MiB |   47.81 MiB |  314.56 MiB |   30.54 kbit/s
     Monday 31 October 2022  701.49 MiB |  225.89 MiB |  927.38 MiB |   90.04 kbit/s
     Tuesday 01 November 2022  506.20 MiB |  184.50 MiB |  690.70 MiB |   67.06 kbit/s
     Wednesday 02 November 2022  631.15 MiB |  182.43 MiB |  813.59 MiB |   78.99 kbit/s
     Thursday 03 November 2022  189.21 MiB |   41.18 MiB |  230.39 MiB |   22.37 kbit/s
     Friday 04 November 2022  703.67 MiB |  175.49 MiB |  879.16 MiB |   85.36 kbit/s
     Saturday 05 November 2022  518.96 MiB |  130.47 MiB |  649.43 MiB |   63.05 kbit/s
     Sunday 06 November 2022  301.74 MiB |   81.95 MiB |  383.69 MiB |   37.25 kbit/s

You can use the option -t to display the top traffic days:

vnstat -t

You should get the following output:

 ens3  /  top 1

    #      day          rx      |     tx      |    total    |   avg. rate
   -----------------------------+-------------+-------------+---------------
    1   Thursday 03 February 2022    2.58 GiB |  214.64 MiB |    2.79 GiB |  277.76 kbit/s
    2   Saturday 05 February 2022    2.11 GiB |  229.32 MiB |    2.33 GiB |  231.67 kbit/s
    3   Tuesday 01 February 2022    1.81 GiB |  190.56 MiB |    2.00 GiB |  198.71 kbit/s
    4   Wednesday 02 February 2022    1.80 GiB |  179.75 MiB |    1.98 GiB |  196.72 kbit/s
    5   Sunday 10 July 2022    1.69 GiB |  248.34 MiB |    1.93 GiB |  192.21 kbit/s
    6   Saturday 19 February 2022    1.56 GiB |  172.67 MiB |    1.73 GiB |  171.56 kbit/s
    7   Friday 16 September 2022    1.44 GiB |  201.21 MiB |    1.64 GiB |  163.16 kbit/s
    8   Friday 17 June 2022    1.34 GiB |  278.89 MiB |    1.61 GiB |  160.33 kbit/s
    9   Friday 25 March 2022    1.06 GiB |  504.19 MiB |    1.55 GiB |  154.51 kbit/s
   10   Monday 07 February 2022    1.30 GiB |  160.68 MiB |    1.46 GiB |  145.30 kbit/s
   -----------------------------+-------------+-------------+---------------

To display the network traffic statistics in real time, run the following command:

vnstat -l

You should get the following output:

Monitoring ens3...    (press CTRL-C to stop)

   rx:       11 kbit/s     7 p/s          tx:        9 kbit/s     6 p/s^C


 ens3  /  traffic statistics

                           rx         |       tx
--------------------------------------+------------------
  bytes                       17 KiB  |          18 KiB
--------------------------------------+------------------
          max              24 kbit/s  |       33 kbit/s
      average            6.78 kbit/s  |     7.39 kbit/s
          min               0 kbit/s  |        0 kbit/s
--------------------------------------+------------------
  packets                         78  |              82
--------------------------------------+------------------
          max                 10 p/s  |          11 p/s
      average                  3 p/s  |           4 p/s
          min                  0 p/s  |           0 p/s
--------------------------------------+------------------
  time                    20 seconds

You can also clear the database entries for interface ens3 and stop monitoring using the following command:

vnstat -i ens3 --remove --force

You should get the following output:

Interface "ens3" removed from database.
The interface will no longer be monitored. Use --add
if monitoring the interface is again needed.

You can also add the removed interface ens3 again by running the following command:

vnstat -i ens3 --add

You should get the following output:

Adding interface "ens3" for monitoring to database...

Restart the vnStat daemon if it is currently running in order to start monitoring "ens3".

Next, restart the vnStat service to apply the changes:

systemctl restart vnstat

Conclusion

In this post, we explained how to install vnStat on Ubuntu 22.04. We also show you how to use it to monitor network traffic with several examples. You can now use this tool to analyze and troubleshoot network-related problems. Feel free to ask me if you have any questions.

Share this page:

Suggested articles

1 Comment(s)

Add comment

Comments

By: Steve at: 2022-12-13 19:11:28

People shouldn't be grabbing source code unless the version in the repos doesn't meet some critical need.  There are many, many, reasons for this, but mainly that repos-installed versions get security updates with the regular patching we should all be doing weekly anway.

Please don't make this harder than it needs to be by installing from source.

 

$ apt search vnstatSorting... DoneFull Text Search... Donevnstat/focal 2.6-1 amd64  console-based network traffic monitorvnstati/focal 2.6-1 amd64  image output support for vnStat