VnStat is a free, open source, light-weight, and console-based Network Traffic monitor. Using VnStat, we can easily find how much network bandwidth is consumed and display the result hourly, daily, monthly, or for specific time period. It gathers the usage details of a single or multiple Network interfaces and displays the summary in the command line. It also helps to calculate the approximate amount of Internet bandwidth usage per month or per day or for a particular period of time. In this tutorial, we will see how to monitor Network bandwidth in Linux and BSD using vnStat.

Install vnStat On Linux

VnStat is available in the default repositories of many Linux and Unix operating systems. You can install it using your distribution's package manager.

On Arch based systems:

Make sure you have enabled [Community] repository and install it using pacman like below:

$ sudo pacman -S vnstat

On Debian, Uubntu, Linux Mint:

$ sudo apt-get install vnstat

On Ubuntu you may need to enable [Universe] repository to install vnstat.

On Redhat, CentOS:

Add and Enable EPEL repository first:

$ sudo yum install epel-release

And then install vnStat as shown above.

$ sudo yum install vnstat

On Fedora:

$ sudo dnf install vnstat

On openSUSE:

$ sudo zypper install vnstat

Start vnStat service

After installed vnStat, start it using command:

$ sudo systemctl enable --now vnstat

You need not to start vnstat on DEB-based systems, because it will be automatically started during installation time.

Install vnStat On FreeBSD

On BSD systems like freeBSD, update port tree and install vnStat as root user using commands:

# pkg update

# pkg install vnstat

Once installed, enable and start vnStat service like below.

# echo 'vnstat_enable="YES"' >> /etc/rc.conf

# service vnstat start

Sample output:

Starting vnstat. No interfaces found in database, adding available interfaces... Interface "em0" added with 1000 Mbit bandwidth limit. -> 1 new interface found. Limits can be modified using the configuration file. See "man vnstat.conf". Unwanted interfaces can be removed from monitoring with "vnstat --remove".".

Configure default network interface

On Linux:

vnStat uses eth0 as default network interface in its configuration file(/etc/vnstat.conf).

However, in some Linux systems (e.g. Ubuntu), the network interface name might be different, like enp0s3, enp0s8. You can verify the network interface's name using "ip addr" command.

Let us have a look at the contents of vnstat config file.

$ head /etc/vnstat.conf

Sample output:

# vnStat 1.18 config file ## # default interface Interface "eth0" # location of the database directory DatabaseDir "/var/lib/vnstat" # locale (LC_ALL) ("-" = use system locale)

As you can see, the default interface is etho in this file.

Let us check the available network interfaces identified by vnstat.

$ ls /var/lib/vnstat enp0s3 enp0s8

As you see, the available network interfaces are different than the one defined in vnstat.conf file.

If you don't set correct default interface, you might get the following error when viewing network bandwidth traffic hourly or monthly.

Error: Unable to read database "/var/lib/vnstat/eth0": No such file or directory

This happens because, the default interface is incorrectly defined in the /etc/vnstat.conf file.

To configure default network interface, edit /etc/vnstat.conf file and update the correct interface name. You can directly edit /etc/vnstat.conf file and update the interface name globally. However, it is recommended to create your own vnstat config file in the user's HOME directory.

$ cp /etc/vnstat.conf ~/.vnstatrc

$ nano ~/.vnstatrc

I am going to use enp0s3 as my default interface.

Replace enp0s3 with your own. Save and close the file.

On FreeBSD:

Edit vnstat configuration file:

# vi /usr/local/etc/vnstat.conf

If the config file is not available, create a new one using sample configuration file:

# cp /usr/local/etc/vnstat.conf.sample /usr/local/etc/vnstat.conf

Then, update the correct default network interface:

Interface "em0"

Save and close the file.

Monitor Network Bandwidth Using vnStat

If you are new to vnStat, bring up the help section of vnStat and see the list of available options by running this command:

$ vnstat --help

Sample output:

vnStat 1.18 by Teemu Toivola <tst at iki dot fi> -q, --query query database -h, --hours show hours -d, --days show days -m, --months show months -w, --weeks show weeks -t, --top10 show top 10 days -s, --short use short output -u, --update update database -i, --iface select interface (default: eth0) -?, --help short help -v, --version show version -tr, --traffic calculate traffic -ru, --rateunit swap configured rate unit -l, --live show transfer rate in real time See also "--longhelp" for complete options list and "man vnstat

Viewing bandwidth usage of all interfaces

Now, Let us display the Network traffic of all interfaces.

To do so, just run:

$ vnstat

Sample output:

rx / tx / total / estimated enp0s3: Sep '19 120 KiB / 66 KiB / 186 KiB / 0 KiB today 120 KiB / 66 KiB / 186 KiB / -- enp0s8: Sep '19 128 KiB / 88 KiB / 216 KiB / 0 KiB today 128 KiB / 88 KiB / 216 KiB / --

Here,

rx - The received traffic

- The received traffic tx - The transferred traffic

Allow a few minutes for Vnstat to start gathering the network traffic usage. If you start vnstat immediately right after installing it, you may get a result something like below.

enp0s3: Not enough data available yet.

Or, you will get the following output in RPM based distributions such as CentOS.

No database found, nothing to do. Use --help for help. A new database can be created with the following command: vnstat --create -i eth0 Replace 'eth0' with the interface that should be monitored. The following interfaces are currently available: enp0s3 (1000 Mbit) lo

It displays nothing now. It is because, vnStat is not yet started to collect the statistics.

Viewing bandwidth usage of a particular interface

To view the network bandwidth usage of a particular interface, for example enp0s3, just mention the Network card name like below.

$ vnstat -i enp0s3

Sample output:

Database updated: Mon Sep 9 08:26:47 2019 enp0s3 since 09/09/2019 rx: 136 KiB tx: 74 KiB total: 210 KiB monthly rx | tx | total | avg. rate ------------------------+-------------+-------------+--------------- Sep '19 136 KiB | 74 KiB | 210 KiB | 0.00 kbit/s ------------------------+-------------+-------------+--------------- estimated -- | -- | -- | daily rx | tx | total | avg. rate ------------------------+-------------+-------------+--------------- today 136 KiB | 74 KiB | 210 KiB | 0.06 kbit/s ------------------------+-------------+-------------+--------------- estimated -- | -- | -- |

Here, enp0s3 is my network card name. You can find your network card name using "ip addr" command.

On FreeBSD, the network card name is em0. To view its bandwidth, simply run:

# vnstat -i em0

Sample output:

Database updated: 2019-09-09 16:40:00 em0 since 2019-09-09 rx: 606.84 KiB tx: 56.12 KiB total: 662.96 KiB monthly rx | tx | total | avg. rate ------------------------+-------------+-------------+--------------- 2019-09 606.84 KiB | 56.12 KiB | 662.96 KiB | 7 bit/s ------------------------+-------------+-------------+--------------- estimated -- | -- | -- | daily rx | tx | total | avg. rate ------------------------+-------------+-------------+--------------- today 606.84 KiB | 56.12 KiB | 662.96 KiB | 90 bit/s ------------------------+-------------+-------------+--------------- estimated 873 KiB | 80 KiB | 953 KiB

Viewing bandwidth usage per hour

Let us display the Network traffic results per hour. To do so, run:

$ vnstat -h

Sample output:

enp0s3 09:46 ^ r | r r | r r | r r | r r r | rt rt r | rt rt r | rt rt rt | rt rt rt | r rt rt rt -+---------------------------------------------------------------------------> | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 00 01 02 03 04 05 06 07 08 09 h rx (KiB) tx (KiB) ][ h rx (KiB) tx (KiB) ][ h rx (KiB) tx (KiB) 10 0 0 ][ 18 0 0 ][ 02 0 0 11 0 0 ][ 19 0 0 ][ 03 0 0 12 0 0 ][ 20 0 0 ][ 04 0 0 13 0 0 ][ 21 0 0 ][ 05 0 0 14 0 0 ][ 22 0 0 ][ 06 12 7 15 0 0 ][ 23 0 0 ][ 07 86 48 16 0 0 ][ 00 0 0 ][ 08 88 45 17 0 0 ][ 01 0 0 ][ 09 59 32

If you have multiple interfaces, use -i parameter to view the hourly network traffic of a specific card like below.

$ vnstat -h -i enp0s8

Suggested read:

Viewing daily bandwidth usage

Use '-d' flag to display the Network traffic of the default network interface per day.

$ vnstat -d

Sample output:

enp0s3 / daily day rx | tx | total | avg. rate ------------------------+-------------+-------------+--------------- 09/09/2019 258 KiB | 138 KiB | 396 KiB | 0.09 kbit/s ------------------------+-------------+-------------+--------------- estimated -- | -- | -- |

Viewing weekly bandwidth usage

$ vnstat -w

Sample output:

enp0s3 / weekly rx | tx | total | avg. rate ---------------------------+-------------+-------------+--------------- last 7 days 258 KiB | 138 KiB | 396 KiB | 0.00 kbit/s current week 258 KiB | 138 KiB | 396 KiB | 0.09 kbit/s ---------------------------+-------------+-------------+--------------- estimated -- | -- | -- |

Viewing monthly bandwidth usage

To view the monthly usage of a network interface card, use 'm' switch.

$ vnstat -m

Sample output:

enp0s3 / monthly month rx | tx | total | avg. rate ------------------------+-------------+-------------+--------------- Sep '19 258 KiB | 138 KiB | 396 KiB | 0.00 kbit/s ------------------------+-------------+-------------+--------------- estimated -- | -- | -- |

Displaying bandwidth usage of past top ten days

To display the network usage of past ten days, use 't' flag.

$ vnstat -t

Sample output:

enp0s3 / top 10 # day rx | tx | total | avg. rate -----------------------------+-------------+-------------+--------------- no data available -----------------------------+-------------+-------------+---------------

It doesn't display any data of top ten days, because I installed Vnstat today.

Suggested read:

Viewing bandwidth usage of particular time

We can also view the bandwidth usage of particular time.

To display the bandwidth usage of past ten seconds, run:

$ vnstat -i enp0s3 -tr 10

Sample output:

7 packets sampled in 10 seconds Traffic average for enp0s3 rx 0.28 kbit/s 0 packets/s tx 0.14 kbit/s 0 packets/s

Viewing live bandwidth usage

To start live monitoring usage of a particular interface card, run:

$ vnstat -i enp0s3 -l

Sample output:

Monitoring enp0s3... (press CTRL-C to stop) rx: 0 kbit/s 0 p/s tx: 0 kbit/s 0 p/s

To stop live monitoring, press CTRL-C.

For more details, refer man pages:

$ man vnstat

Conclusion

VnStat is handy and useful tool to monitor either your Internet usage or Network traffic for a single or multiple network interfaces. In case, you're wondering how much bandwidth you're consuming daily/monthly, you can use this tool to find out without the help of your Internet service provider or using any third party tools.

Resource: