×


Monitor Network Traffic with vnStat on Ubuntu 20.04 - How to do it ?

vnStat is an open-source and lightweight network traffic monitor for Linux operating systems. 

vnStat quietly monitors the network traffic for specific interfaces and stores the monitoring data in a database. 

With vnStat, you can view the bandwidth usage per hour, day, month, or for a particular period.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Server Monitoring queries on Ubuntu Linux Servers.

In this context, we shall look into how to install vnStat and monitor network traffic on Ubuntu OS.


How to install vnStat on Ubuntu 20.04 LTS ?

VnStat is available in the default Ubuntu repositories but it is not the latest version. To install vnStat latest version, follow the below steps.


1. Update repository index

First, execute the below command to update the system repository index:

$ sudo apt update

Enter the password for sudo.


2. Install prerequisites

Next, you will have to install some prerequisites that can build software from source. We will need it for the compilation of vnStat.

Execute this command to install the prerequisites:

$ sudo apt install build-essential gcc make libsqlite3-dev -y


3. Download vnStat tar.gz package

Now from the vnStat official website, download the latest version of vnStat tar.gz package. Currently, the latest version of vnStat is 2.6, so we can download it as follows:

$ wget https://humdi.net/vnstat/vnstat-2.6.tar.gz


4. Install vnStat

Once the tar.gz package is downloaded, we can install it as follows:

i. First, extract the tar.gz using the below command in Terminal:

$ tar -xvzf vnstat-2.6.tar.gz

ii. Then switch to the resulting extracted directory using the below command:

$ cd vnstat-2.6

iii. Now configure it using the command below:

$ ./configure --prefix=/usr --sysconfdir=/etc

iv. Then, install vnStat as follows:

$ sudo make
$ sudo make install


5. Verify Installation

To verify if vnStat has been successfully installed, execute the below command in Terminal:

$ vnstat -v

The output will show the version of vnStat installed.


6. Enable and start vnStat service

i. To enable and start vnStat service, first you will need to copy vnStat service file from the vnStat extracted directory to the /etc/systemd/system/ using the below command:

$ sudo cp -v vnstat-2.6/examples/systemd/vnstat.service /etc/systemd/system/

ii. Now enable vnStat service using the below command:

$ sudo systemctl enable vnstat

iii. Then to start vnStat service, execute the below command

$ sudo systemctl start vnstat

iv. To verify if vnStat service is running, execute the below command:

$ sudo systemctl status vnstat

If everything is in order, the output will confirm that vnStat service is running properly without any issues.


How to Monitor Network Traffic with vnStat on Ubuntu?

vnStat supports various options that help you to monitor traffic in different ways. You can view some of the available options by executing the below command in Terminal:

$ vnstat --help

Or execute the following command to view the complete list of options:

$ vnstat --longhelp

Running the vnStat command without any option prints the bandwidth usage statistics for all the available interfaces.

$ vnstat

Here is the output of vnStat command on our system which is showing statistics of one interface as this is the only available interface on our system.


You can also monitor a specific interface by using the -i option followed by the interface name.

$ vnstat -i <interface_name>

For instance, to monitor an interface ens33, the command would be:

$ vnstat -i ens33

You can also monitor bandwidth usage as per hourly, daily, monthly, and yearly usage using the -h, -d, -m, and -y options respectively. 

For example, to find the daily bandwidth usage, the command would be:

$ vnstat -d

You can also get the bandwidth usage for the top traffic days.

For instance, to find the top 10 bandwidth usage days, use -t option followed by the number of days:

vnstat -t 10

To monitor bandwidth usage in real-time, use the -l option as follows:

$ vnstat -l

If you want to remove all the statistics for a specific interface from the database and stop monitoring it, use the below command syntax:

$ sudo vnstat --remove -i <interface_name> --force

For instance, to remove the interface ens33 from the database and stop monitoring it, the command would be:

$ sudo vnstat --remove -i ens33 --force

To add this interface again for monitoring, use the below command:

$ sudo vnstat --add -i ens33

After adding the interface, restart vnStat service:

$ sudo systemctl restart vnstat


[Need urgent assistance in fixing missing Ubuntu Linux Servers Packages ? We are available to help you. ]


Conclusion

This article covers how to install and use this tool on Ubuntu 20.04 LTS for monitoring the traffic on network interfaces.

vnStat is a handy tool to keep an eye on the bandwidth usage on your Linux OS.


To Install vnstat using the apt/apt-get on Ubuntu:

1. Type the following apt command/apt-get command to update the system:

$ sudo apt update
$ sudo apt upgrade

2. Search for vnstat package, enter:

$ apt show vnstat

3. Execute the following command to set up and install vnstat on Ubuntu server:

$ sudo apt-get install vnstat

OR

$ sudo apt install vnstat


To Enable and start the vnstat service on Ubuntu:

Run the command below:

$ sudo systemctl enable vnstat.service

To Start the vnstat service

$ sudo systemctl start vnstat.service

To Stop the vnstat service

$ sudo systemctl stop vnstat.service

To Restart/reload the vnstat service

After editing the /etc/vnstat.conf, we must restart or reload the service:

$ sudo systemctl restart vnstat.service

OR

$ sudo systemctl reload vnstat.service

To Find the status of vnstat service

$ sudo systemctl status vnstat.service