vnstat – keeping an eye on your network bandwidth

Once we leave the haloed portals of our alma mater, accessing the internet ceases to be a luxury it once used to be. Keeping an eye on network bandwidth usage in order to stay within data pack limits becomes utmost important. In such a scenario, a simple and extremely elegant command line tool called vnstat comes to our rescue.

  • To begin using vnstat, install it:

$ sudo yum install vnstat

  • Now, we need to tell vnstat which interface to monitor. First check the list of interfaces available on your system:

$ ip link show

This will print something like the following :

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: p2p1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN mode DEFAULT qlen 1000
link/ether 00:16:d3:26:73:dc brd ff:ff:ff:ff:ff:ff
3: wlp3s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
link/ether 00:16:cf:18:57:6c brd ff:ff:ff:ff:ff:ff
4: wwp0s29f7u2i1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
link/ether 58:2c:80:13:92:63 brd ff:ff:ff:ff:ff:ff
5: ppp0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN mode DEFAULT qlen 3
link/ppp

  • On my system, am interested in the mobile broadband connection (ppp0). Hence, we point vnstat to monitor it :

$ sudo vnstat -u -i ppp0

  • Now, make vnstat daemon auto start at boot :

$ sudo systemctl enable vnstat.service

  • Unfortunately, vnstat log file gets created as a root and not as a normal user. Hence, we need to change its permissions to allow vnstat to write data to it. This step will remove the frustrating

ppp0: Not enough data available yet.

issue. Simple type the following to change owner and group:

$ sudo chown <owner_name> -R /var/lib/vnstat/*

$ sudo chgrp <group_name> -R /var/lib/vnstat/*

(CREDIT : http://realtechtalk.com/vnstat_setup_guidetutorial-1181-articles)

  • As pointed out by Jim, on a multiuser system, it may make more sense to allow everyone to have read-write permissions, that is if the admin so desires. In such a scenario we need to run the following:

$ sudo chmod a+rw -R /var/lib/vnstat/*

  • And we are done! From next boot, vnstat will start logging your network bandwidth. To see realtime / live usage statistics, type:

$ vnstat -l

For non-realtime data:

$ vnstat

There is an excellent article on various vnstat options on Linuxaria. Happy internet-ing without burning a hole into your pocket.