×


Install vnStat in Linux to monitor network traffic

Are you trying to install vnStat in Linux?

This guide will help you.

Recently, we received a remote support request from one of the customers to install vnStat as he wished to monitor the network traffic.
vnStat is a console-based network traffic monitoring tool design for Linux and BSD. It will keep a log of the network traffic for selected network interfaces.
vnStat won't actually be sniffing any traffic and also ensures light use of system resources regardless of network traffic rate.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Linux related installations tasks.
In this context, we shall look into how to install vnStat on Linux.

Steps to install vnStat in Linux ?

Here, follow the process given below to install the vnStat in Linux.

1. First, we download the vnStat latest version from its official GitHub release page:

https://github.com/vergoh/vnstat/releases

2. After downloading it, we extract the archive and navigate to the location where we have extracted the archive and compile it using the following commands.
Install VnStat in CentOS/RHEL and Fedora:

# yum group install “Development Tools”
# yum install gd gd-devel sqlite-devel
# tar -xvf vnstat-2.6.tar.gz
# cd vnstat-2.6/
# ./configure –prefix=/usr –sysconfdir=/etc
# make
# make install


Install VnStat in Debian and Ubuntu:

$ sudo apt-get install build-essential gd gd-devel libsqlite3-dev
$ tar -xvf vnstat-2.6.tar.gz
$ cd vnstat-2.6/
$ sudo ./configure –prefix=/usr –sysconfdir=/etc
$ sudo make
$ sudo make install


3. Once the installation completes, we copy the vnStat service file.
VnStat Systemd:

# cp -v examples/systemd/vnstat.service /etc/systemd/system/
# systemctl enable vnstat
# systemctl start vnstat

Vnstat Init.d:

# cp -v examples/init.d/redhat/vnstat /etc/init.d/
# chkconfig vnstat on
# service vnstat start


4. Now, we verify that the binaries got installed to a suitable location and are of the correct version.

# vnstat

5. If we wish to customize vnStat configuration, open its configuration file located at:

# vi /etc/vnstat.conf

6. Then we allow some time for the stats to upload to the database.

After having enough information written in the database, we can see the network stats by running the below command.

# vnstat

7. Also, we can check the hourly stats by using the -h option:

# vnstat -h

8. For daily stats, we can use the -d option:

# vnstat -d

9. For more available options, we can use the –help:

# vnstat –help


Resolving common errors relating to vnStat ?

Here, look into common errors in relation to vnStat.

1. No such file or directory

error: linux/errno.h: No such file or directory
make[1]: *** [vnstat.o] Error 1
make[1]: Leaving directory `/home/vnstat-1.10/src’
make: *** [vnstat] Error 2

Cause:
Kernel headers package is missing


Solution:
Install kernel-headers package
To install kernel-headers in Ubuntu/Debian:
i. First, we check the kernel version as well as kernel header package that matches the kernel version using following commands.

$ uname -r
$ apt search linux-headers-$(uname -r)

ii. On Debian/Ubuntu and their derivatives, all kernel header files can be found under /usr/src directory.

By running the below command, we can check if the matching kernel headers for kernel version are already installed on the system:

$ ls -l /usr/src/linux-headers-$(uname -r)

iii. We update the packages before installing the appropriate kernel headers.
We do this to grab information about the latest package releases, using the following command.

$ sudo apt update

iv. Then we run the below command to install the Linux Kernel headers package for our kernel version.

$ sudo apt install linux-headers-$(uname -r)

After that, we check if the matching kernel headers have been installed on the system.

To install kernel-headers in CentOS 7:
i. First, we confirm that the matching kernel headers are already installed under /usr/src/kernels/ location on the system using the following commands:

# cd /usr/src/kernels/
# ls -l

ii. If no matching kernel headers are located in the /usr/src/kernels/ directory, we install kernel headers. For that, we run the below commands.

# yum install kernel-devel [On CentOS/RHEL 7]
# dnf install kernel-devel [On Fedora 22+]

iii. After the installation, we can find all the kernel headers files in /usr/src/kernels directory using following command.

# ls -l /usr/src/kernels/$(uname -r)

iv. In VPS, a kernel may have a customized version name, in such scenario, we have to identify the kernel version manually and check the installed kernel header files using following commands.

# uname -r
# ls -l /usr/src/kernels/3.10.0-862.2.3.el7.x86_64

v. Additionally, if we need header files for the Linux kernel for use by glibc, install the kernel-header package using following command.

# yum install kernel-headers [On CentOS/RHEL 7]
# dnf install kernel-headers [On Fedora 22+]

 

2. vnStat not updating

After installing vnStat, the database never updates.

Causes

* /var/lib/vnstat having wrong ownership
* Database not created


Solution
i. To fix the improper ownership of /var/lib/vnstat, we run:

$ sudo chown -R vnstat:vnstat /var/lib/vnstat

ii. Then we make sure that the following files are present in that folder and have proper ownership:

ibmimedia@server:/var/lib/vnstat$ ls -la
total 16
drwxr-xrwx 2 vnstat vnstat 4096 May 16 01:50 .
drwxr-xr-x 78 root root 4096 Jul 22 15:14 ..
-rw-r–rwx 1 vnstat vnstat 2792 Jul 26 00:26 eth0
-rw-rw-r– 1 vnstat vnstat 2792 Jul 26 00:26 .eth0

.eth0 is temp file, that may disappear sometimes.


iii. After installing vnStat, we need to create a database.
To create the database, we can use the update switch, which creates a new database in the base of no database.

$ sudo vnstat -i eth0 -u

where eth0 is the name of the network interface.
Then we can view the details vnstat -i eth0 or simply vnstat.


iv. In case, if the vnStat is not running then we start the daemon by running the following command and check for the result after 5 minutes.

$ sudo /etc/init.d/vnstat start

Once database have been initialized, its ready to monitor bandwidth usage.
We can view the report using the command vnStat.

[Still, not able to install vnStat in Linux? – We are here to help you. ]


Conclusion

This article covers how to install and configure vnStat in Linux. Also, you will see how to resolve some common errors relating to it.
#vnStat (view network statistics) is a network utility for the #Linux operating system.
It uses a command line interface. vnStat command is a console-based network #traffic #monitor.
It keeps a log of hourly, daily and monthly network traffic for the selected interface(s) but is not a packet sniffer.

Features of vnStat:
1. quick and simple to install and get running
2. gathered statistics persists through system reboots
3. can monitor multiple interfaces at the same time
4. data retention duration is fully user configurable on the fly
5. months can be configured to follow billing period
6. light, minimal resource usage
7. same low cpu usage regardless of traffic
8. can be used without root permissions
9. online color configuration editor