Blog

03/06/2019

CentOS / RHEL: Install vnStat Network Traffic Monitor To Keep a Log Of Daily Traffic



How do I install vnstat software – a console-based network traffic monitor under CentOS or RHEL or Fedora Linux operating system to keep a log of hourly, daily and monthly network traffic?


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). However, it isn’t a packet sniffer. The traffic information is analyzed from the proc filesystem. That way vnStat can be used even without root permissions.

Installation

First, turn on EPEL repo on CentOS 6/RHEL 6 or on RHEL 7./CentOS 7.x. Type the the following yum command to install vnstat:
# yum install vnstat

Loaded plugins: auto-update-debuginfo, product-id, protectbase, rhnplugin,
              : subscription-manager
Updating certificate-based repositories.
Unable to read consumer identity
0 packages excluded due to repository protections
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package vnstat.x86_64 0:1.11-1.el6 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
================================================================================
 Package          Arch             Version                 Repository      Size
================================================================================
Installing:
 vnstat           x86_64           1.11-1.el6              epel           102 k
 
Transaction Summary
================================================================================
Install       1 Package(s)
 
Total download size: 102 k
Installed size: 283 k
Is this ok [y/N]: y
Downloading Packages:
vnstat-1.11-1.el6.x86_64.rpm                             | 102 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : vnstat-1.11-1.el6.x86_64                                     1/1 
Installed products updated.
  Verifying  : vnstat-1.11-1.el6.x86_64                                     1/1 
 
Installed:
  vnstat.x86_64 0:1.11-1.el6                                                    
 
Complete!

If you are using Fedora Linux, run the following dnf command:
# dnf update
# dnf install vnstat

Configuration

Edit /etc/vnstat.conf, enter:
# vi /etc/sysconfig/vnstat
Sample outputs:

VNSTAT_OPTIONS="-u -i eth0"

Edit /etc/vnstat.conf, enter:
# vi /etc/vnstat.conf
Set default interface to eth1:

Interface "eth1"

The file is self documented so set the options as per your local setup. Save and close the file.

Create the database

You need to run the following command to create the database. But, first set vnstat shell to /bin/bash using the chsh commad
# chsh -s /bin/bash vnstat
# grep --color vnstat /etc/passwd
# id vnstat

This is usually the first command used after a fresh install. Use the runuser command to create the database for eth0 interface:
# runuser -l vnstat -g vnstat -c '/usr/bin/vnstat -u -i eth0'
To create the database for eth1 interface, run:
# runuser -l vnstat -g vnstat -c '/usr/bin/vnstat -u -i eth1'
Make sure vnstat user can not login i.e. refuse a login for vnstat user:
# chsh -s /sbin/nologin vnstat
# grep --color vnstat /etc/passwd
# id vnstat

Start vnstat service

Type the following chkconfig command to turn on vnstat service at boot time:
# chkconfig vnstat on
To start / stop / restart and control vnstart service, run:
# service vnstat start
# service vnstat stop
# service vnstat restart
# service vnstat status

OR systemd based distro such as CentOS/RHEL version 7.x:
$ sudo systemctl start vnstat
$ sudo systemctl stop vnstat
$ sudo systemctl restart vnstat
$ sudo systemctl status vnstat

How do I view stats?

The syntax is:

vnstat
vnstat -i {interface} 
vnstat -i {interface} -option
vnstat -i {interface} -option1 -option2

$ vnstat
Sample outputs:

                      rx      /      tx      /     total    /   estimated
 eth1:
       Oct '12     76.68 GiB  /  635.07 GiB  /  711.74 GiB
       Nov '12     31.14 GiB  /  186.98 GiB  /  218.12 GiB  /  352.45 GiB
     yesterday      1.35 GiB  /   11.32 GiB  /   12.67 GiB
         today      1.34 GiB  /    8.82 GiB  /   10.16 GiB  /   19.37 GiB
 
 eth0:
       Oct '12    629.87 GiB  /   91.10 GiB  /  720.97 GiB
       Nov '12    181.99 GiB  /   35.38 GiB  /  217.37 GiB  /  351.24 GiB
     yesterday     11.25 GiB  /    1.70 GiB  /   12.95 GiB
         today      8.76 GiB  /    1.60 GiB  /   10.36 GiB  /   19.76 GiB

To see eth0 stats, enter:
$ vnstat -i eth0
To see daily stats for eth0, enter:
$ vnstat -i eth0 -d
Sample outputs:

 eth0  /  daily
 
         day         rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
      10/21/12     13.48 GiB |    1.80 GiB |   15.28 GiB |    1.48 Mbit/s
      10/22/12     24.06 GiB |    3.44 GiB |   27.51 GiB |    2.67 Mbit/s
      10/23/12     23.95 GiB |    3.45 GiB |   27.40 GiB |    2.66 Mbit/s
      10/24/12     22.81 GiB |    3.30 GiB |   26.11 GiB |    2.53 Mbit/s
      10/25/12     25.11 GiB |    3.48 GiB |   28.59 GiB |    2.78 Mbit/s
      10/26/12     22.24 GiB |    3.01 GiB |   25.25 GiB |    2.45 Mbit/s
      10/27/12     12.13 GiB |    1.67 GiB |   13.80 GiB |    1.34 Mbit/s
      10/28/12      9.65 GiB |    1.67 GiB |   11.33 GiB |    1.10 Mbit/s
      10/29/12     17.04 GiB |    3.13 GiB |   20.17 GiB |    1.96 Mbit/s
      10/30/12     18.14 GiB |    3.18 GiB |   21.32 GiB |    2.07 Mbit/s
      10/31/12     17.45 GiB |    3.05 GiB |   20.51 GiB |    1.99 Mbit/s
      11/01/12     14.64 GiB |    2.74 GiB |   17.38 GiB |    1.69 Mbit/s
      11/02/12     13.66 GiB |    2.51 GiB |   16.17 GiB |    1.57 Mbit/s
      11/03/12      7.62 GiB |    1.34 GiB |    8.96 GiB |  870.25 kbit/s
      11/04/12      5.63 GiB |    1.14 GiB |    6.77 GiB |  656.98 kbit/s
      11/05/12      9.34 GiB |    2.08 GiB |   11.42 GiB |    1.11 Mbit/s
      11/06/12      9.85 GiB |    2.13 GiB |   11.98 GiB |    1.16 Mbit/s
      11/07/12      9.91 GiB |    2.14 GiB |   12.05 GiB |    1.17 Mbit/s
      11/08/12      9.76 GiB |    2.12 GiB |   11.88 GiB |    1.15 Mbit/s
      11/09/12      8.47 GiB |    1.81 GiB |   10.28 GiB |  998.23 kbit/s
      11/10/12      5.54 GiB |    1.05 GiB |    6.58 GiB |  639.28 kbit/s
      11/11/12      5.92 GiB |    1.12 GiB |    7.03 GiB |  682.98 kbit/s
      11/12/12      8.74 GiB |    1.93 GiB |   10.67 GiB |    1.04 Mbit/s
      11/13/12      8.77 GiB |    1.95 GiB |   10.72 GiB |    1.04 Mbit/s
      11/14/12      8.93 GiB |    2.00 GiB |   10.93 GiB |    1.06 Mbit/s
      11/15/12     10.53 GiB |    2.13 GiB |   12.65 GiB |    1.23 Mbit/s
      11/16/12     13.58 GiB |    2.31 GiB |   15.89 GiB |    1.54 Mbit/s
      11/17/12     11.09 GiB |    1.60 GiB |   12.70 GiB |    1.23 Mbit/s
      11/18/12     11.25 GiB |    1.70 GiB |   12.95 GiB |    1.26 Mbit/s
      11/19/12      8.81 GiB |    1.61 GiB |   10.42 GiB |    1.92 Mbit/s
     ------------------------+-------------+-------------+---------------
     estimated     16.69 GiB |    3.06 GiB |   19.74 GiB |

To see hourly network stats, enter:
$ vnstat -i eth0 -h
To see monthly network stats, enter:
$ vnstat -i eth0 -m
To see weekly network stats, enter:
$ vnstat -i eth0 -w
To see top 10 network, stats:
$ vnstat -t
$ vnstat -i eth0 -t

To see short output:
$ vnstat -i eth0 -s
To calculate traffic, enter:
$ vnstat -i eth0 -tr
Sample outputs:

2038 packets sampled in 5 seconds   
Traffic average for eth0
 
      rx         1.08 Mbit/s           202 packets/s
      tx       273.60 kbit/s           204 packets/s

To see transfer rate in real time for eth0, enter:
$ vnstat -i eth0 -l

Check out related media

This tutorial is also available in a video format:

This entry is 1 of 3 in the vnStat Monitor and Log Network Traffic Tutorial series. Keep reading the rest of the series:
  1. How to install vnstat on a CentOS/RHEL/Fedora Linux
  2. View Vnstat Graphs Using PHP Web Interface Frontend
  3. How to install vnstat on FreeBSD Unix server

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...