vnStat – Monitor Network Bandwidth in Linux and BSD

In this tutorial, we are going to see how to monitor Network bandwidth or traffic usage in Linux and BSD. VnStat is a console-based, free, open source, and light-weight Network Traffic monitor. Using this tool, we can easily find how much bandwidth that we consume and display the result hourly, daily, monthly, or for specif time period. It gathers the usage details of a single or multiple Network interfaces and displays the summary in the command line. We can use this tool to calculate the approximate amount of Internet bandwidth usage per month or per day or for a particular period of time.

Install vnStat

VnStat is available in the default repositories of modern Linux and Unix operating systems. You can install it using your distribution’s package manager.

On RPM based systems:

Add and Enable EPEL repository first:

sudo yum install epel-release

And then install vnStat as shown above.

sudo yum install vnstat

On DEB based systems:

sudo apt-get install vnstat

On Arch based systems:

sudo pacman -S vnstat

You can install and use it either using root or non-root user.

Monitor Network Bandwidth using vnStat

Run the following command to display the help section of VnStat:

vnstat --help

Sample output:

vnStat 1.15 by Teemu Toivola <tst at iki dot fi>

 -q, --query query database
 -h, --hours show hours
 -d, --days show days
 -m, --months show months
 -w, --weeks show weeks
 -t, --top10 show top 10 days
 -s, --short use short output
 -u, --update update database
 -i, --iface select interface (default: eth0)
 -?, --help short help
 -v, --version show version
 -tr, --traffic calculate traffic
 -ru, --rateunit swap configured rate unit
 -l, --live show transfer rate in real time

See also "--longhelp" for complete options list and "man vnstat".

ostechnixubuntuserver-_003

Viewing bandwidth usage of all interfaces

Now, Let us display the Network traffic of all interfaces.

To do so, just run:

vnstat

Sample output:

enp0s3: Not enough data available yet.

Or, you will get the following output in RPM based distributions such as CentOS.

No database found, nothing to do. Use --help for help.

A new database can be created with the following command:
 vnstat --create -i eth0

Replace 'eth0' with the interface that should be monitored.

The following interfaces are currently available:
 enp0s3 (1000 Mbit) lo

ostechnixubuntuserver-_004

It displays nothing now. It is because, vnStat is not yet started to collect the statistics.

Wait for few minutes, and check again the bandwidth of all Network interfaces using command:

vnstat

Sample output:

Database updated: Tue Sep 20 17:15:12 2016

enp0s3 since 

rx: 83.93 MiB tx: 2.32 MiB total: 86.25 MiB

monthly
 rx | tx | total | avg. rate
 ------------------------+-------------+-------------+---------------
 Sep '16 83.93 MiB | 2.32 MiB | 86.25 MiB | 0.41 kbit/s
 ------------------------+-------------+-------------+---------------
 estimated 126 MiB | 3 MiB | 129 MiB |

daily
 rx | tx | total | avg. rate
 ------------------------+-------------+-------------+---------------
 83.93 MiB | 2.32 MiB | 86.25 MiB | 11.38 kbit/s
 ------------------------+-------------+-------------+---------------
 estimated 115 MiB | 2 MiB | 117 MiB |

ostechnixubuntuserver-_005

Here,

  • rx – The received traffic
  • tx – The transferred traffic

Viewing bandwidth usage of a particular interface

To view the network bandwidth usage of a particular interface, just mention the Network card name like below.

vnstat -i enp0s3

Sample output:

Database updated: Tue Sep 20 17:25:12 2016

enp0s3 since 

rx: 83.98 MiB tx: 2.33 MiB total: 86.31 MiB

monthly
 rx | tx | total | avg. rate
 ------------------------+-------------+-------------+---------------
 Sep '16 83.98 MiB | 2.33 MiB | 86.31 MiB | 0.41 kbit/s
 ------------------------+-------------+-------------+---------------
 estimated 126 MiB | 3 MiB | 129 MiB |

daily
 rx | tx | total | avg. rate
 ------------------------+-------------+-------------+---------------
 83.98 MiB | 2.33 MiB | 86.31 MiB | 11.27 kbit/s
 ------------------------+-------------+-------------+---------------
 estimated 114 MiB | 2 MiB | 116 MiB |

ostechnixubuntuserver-_007

Here, enp0s3 is my network card name. You can find your network card name using “ip addr” command.

Viewing bandwidth usage of a particular interface per hour

Let us display the Network traffic results per hour. To do so, run:

vnstat -h

Sample output:

enp0s3 17:25 
 ^ r 
 | r 
 | r 
 | r 
 | r 
 | r 
 | r 
 | r 
 | r 
 | r 
 -+---------------------------------------------------------------------------> 
 | 18 19 20 21 22 23 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 
 
 h rx (KiB) tx (KiB) h rx (KiB) tx (KiB) h rx (KiB) tx (KiB) 
18 0 0 02 0 0 10 0 0
19 0 0 03 0 0 11 0 0
20 0 0 04 0 0 12 0 0
21 0 0 05 0 0 13 0 0
22 0 0 06 0 0 14 0 0
23 0 0 07 0 0 15 0 0
00 0 0 08 0 0 16 85,873 2,376
01 0 0 09 0 0 17 120 8

ostechnixubuntuserver-_008

Viewing bandwidth usage of a particular interface per day

Use ‘-d’ flag to display the Network traffic per day.

vnstat -d

Sample output:

enp0s3 / daily

day rx | tx | total | avg. rate
 ------------------------+-------------+-------------+---------------
 �F��� 84.01 MiB | 2.34 MiB | 86.35 MiB | 11.23 kbit/s
 ------------------------+-------------+-------------+---------------
 estimated 115 MiB | 2 MiB | 117 MiB |

ostechnixubuntuserver-_009

Viewing bandwidth usage of a week

vnstat -w

Sample output:

enp0s3 / weekly

rx | tx | total | avg. rate
 ---------------------------+-------------+-------------+---------------
 last 7 days 84.06 MiB | 2.35 MiB | 86.41 MiB | 1.22 kbit/s
 current week 84.06 MiB | 2.35 MiB | 86.41 MiB | 4.72 kbit/s
 ---------------------------+-------------+-------------+---------------
 estimated 344 MiB | 8 MiB | 352 MiB |

ostechnixubuntuserver-_010

Viewing bandwidth usage of a week

To view the monthly usage of a network interface card, use ‘m’ switch.

vnstat -m

Sample output:

enp0s3 / monthly

 month rx | tx | total | avg. rate
 ------------------------+-------------+-------------+---------------
 Sep '16 84.06 MiB | 2.35 MiB | 86.41 MiB | 0.42 kbit/s
 ------------------------+-------------+-------------+---------------
 estimated 127 MiB | 3 MiB | 130 MiB |

ostechnixubuntuserver-_011

Displaying bandwidth usage of past top ten days

To display the network usage of past ten days, use ‘t’ flag.

vnstat -t

Sample output:

enp0s3 / top 10

 # day rx | tx | total | avg. rate
 -----------------------------+-------------+-------------+---------------
 no data available
 -----------------------------+-------------+-------------+---------------

ostechnixubuntuserver-_012

Viewing bandwidth usage of particular time

We can also view the bandwidth usage of particular time.

To display the bandwidth usage of past ten seconds, run:

vnstat -i enp0s3 -tr 10

Sample output:

12 packets sampled in 10 seconds 
Traffic average for enp0s3

rx 0.88 kbit/s 0 packets/s
 tx 0.35 kbit/s 0 packets/s

ostechnixubuntuserver-_013

Viewing live bandwidth usage

To start live monitoring usage of a particular interface card, run:

vnstat -i enp0s3 -l

Sample output:

Monitoring enp0s3... (press CTRL-C to stop)

rx: 1 kbit/s 1 p/s tx: 2 kbit/s 0 p/s

ostechnixubuntuserver-_015

You can stop this at anytime by pressing CTRL-C.

For more details, I suggest you to read the man pages:

man vnstat

Conclusion

To summing up, VnStat is handy and useful tool to monitor either your Internet usage or Network traffic for a single or multiple network interfaces. In case, you’re wondering how much bandwidth you’re consuming per month, you can use this tool to find out without the help of your Internet service provider or using any third party tools.

That’s all for now folks. I will be here with another interesting guide soon. If you find this guide useful, please share it on your social networks and support OSTechNix.

Cheers!

Reference link:

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...

  • I think iftop is great too. You can see which to which interfaces and IPs the traffic is flowing to the most.