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.
VnStat is available in the default Ubuntu repositories but it is not the latest version. To install vnStat latest version, follow the below steps.
First, execute the below command to update the system repository index:
$ sudo apt updateEnter the password for sudo.
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 -yNow 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.gzOnce 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.gzii. Then switch to the resulting extracted directory using the below command:
$ cd vnstat-2.6iii. Now configure it using the command below:
$ ./configure --prefix=/usr --sysconfdir=/etciv. Then, install vnStat as follows:
$ sudo make
$ sudo make installTo verify if vnStat has been successfully installed, execute the below command in Terminal:
$ vnstat -vThe output will show the version of vnStat installed.
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 vnstatiii. Then to start vnStat service, execute the below command
$ sudo systemctl start vnstativ. To verify if vnStat service is running, execute the below command:
$ sudo systemctl status vnstatIf everything is in order, the output will confirm that vnStat service is running properly without any issues.
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 --helpOr execute the following command to view the complete list of options:
$ vnstat --longhelpRunning the vnStat command without any option prints the bandwidth usage statistics for all the available interfaces.
$ vnstatHere 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 ens33You 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 -dYou 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 10To monitor bandwidth usage in real-time, use the -l option as follows:
$ vnstat -lIf 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> --forceFor instance, to remove the interface ens33 from the database and stop monitoring it, the command would be:
$ sudo vnstat --remove -i ens33 --forceTo add this interface again for monitoring, use the below command:
$ sudo vnstat --add -i ens33After adding the interface, restart vnStat service:
$ sudo systemctl restart vnstatThis 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 upgrade2. Search for vnstat package, enter:
$ apt show vnstat3. Execute the following command to set up and install vnstat on Ubuntu server:
$ sudo apt-get install vnstatOR
$ sudo apt install vnstatTo Enable and start the vnstat service on Ubuntu:
Run the command below:
$ sudo systemctl enable vnstat.serviceTo Start the vnstat service
$ sudo systemctl start vnstat.serviceTo Stop the vnstat service
$ sudo systemctl stop vnstat.serviceTo Restart/reload the vnstat service
After editing the /etc/vnstat.conf, we must restart or reload the service:
$ sudo systemctl restart vnstat.serviceOR
$ sudo systemctl reload vnstat.serviceTo Find the status of vnstat service
$ sudo systemctl status vnstat.service