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

by on November 20, 2012 · 6 comments· LAST UPDATED November 20, 2012

in ,

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?

Tutorial details
DifficultyIntermediate (rss)
Root privilegesYes
RequirementsRHEL / SL / CentOS
Fedora Linux
Estimated completion timeN/A

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. 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!
 

Configuration

Edit /etc/sysconfig/vnstat, 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

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:



(Video.01: Demo: Setup and use vnStat )

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!
This entry is 1 of 2 in the vnStat Monitor and Log Network Traffic Tutorial series. Keep reading the rest of the series:
  1. Install vnStat Network Traffic Monitor To Keep a Log Of Daily Traffic
  2. View Vnstat Graphs Using PHP Web Interface Frontend

{ 6 comments… read them below or add one }

1 Jalal Hajigholamali November 21, 2012 at 4:43 am

Hi,

Thanks, very useful material

Reply

2 Vegard November 21, 2012 at 1:51 pm

Using Centos 6.3 and it uses em1 and lo.

I’ve entered em1 instead of eth0 and em2 instead of eth1. But vnstat says that there isn’t enough data available yet. It’s on a busy server.. Have I mixed the networkcards up?

Reply

3 Glyn November 23, 2012 at 8:49 pm

Lo is the loopback interface. In otherwords, localhost, which points back to your machine. I dony know what em1 would be, but that’s not it either. Check ifconfig and iwconfig and see what your options are.

Reply

4 ITN John November 23, 2012 at 8:55 pm

em1 is common name on BSD like operating system such as FreeBSD / OpenBSD and so on. I think newer Linux kernel are also started use the em1, em0 and so on.

Reply

5 Asri June 4, 2013 at 4:59 am

very useful tutorial. it works perfect on my server. thanks.

Reply

6 Sujit January 20, 2014 at 11:44 am

It is giving me output in TB but i am not using such bandwidth.

Reply

Leave a Comment

Tagged as: , , , ,

Previous Faq:

Next Faq: