Use VnStat to Monitor Network Traffic from Linux Command Line

Network traffic monitoring not only helps administrators detect root causes of traffic related issues, like network overloading, but also helps them keep a tab on traffic flow to and from the Internet. Overall, it’s an important task that requires a dedicated and an effective software.

For Linux, there are many GUI-based network traffic monitoring tools, but if you are looking for a command line-based utility, vnstat is worth trying out. In this article, we will discuss the basics of the command along with the features it provides.

vnStat is a command line-based network traffic monitoring tool available for Linux as well as BSD. It keeps a log of network traffic for the selected interfaces and uses the network interface statistics provided by the kernel as information source, which effectively means that it is light on system resources as it doesn’t actually sniff any traffic. However, for the command to run properly, at least a 2.2 series kernel is required.

Here are some of the features it provides:

  • The statistics it gathers persists through system reboots
  • It can monitor multiple interfaces at any given time
  • It provides several output options: summary, hourly, daily, monthly, weekly, and top 10 days
  • Months can be configured to follow billing period
  • It provides an option to produce output as a png image
  • It is light on system resources
  • It can be used without root permissions

Users of Debian-based Linux distributions, like Ubuntu, can easily download and install the utility using the following command:

sudo apt-get install vnstat

Alternatively, you can also download the command’s source package from its official website and install it manually.

Here are some examples of how the vnstat command can be used:

Note: all examples presented in this article are tested on Ubuntu 14.04.

Default output

If you run the vnstat command without any command line option, it displays a monthly and daily report of the network activity. Here is the output the command produced on my system:

vnstat

vnstat-default-output-first-time

As clear from the first line in output, the vnstat updated some sort of database before producing any other output. This is the same database (corresponding to a network interface) that the utility created when you installed it:

vnstat-installation

The database is created with the same name as the interface being monitored (eth0 in this case) and is stored as an FPT file inside the /var/lib/vnstat/ directory.

Display hourly, daily, weekly, and monthly traffic

The command provides command line options to display hourly and daily, as well as monthly, network activity. For example, to display hourly traffic, use the -h command line option:

vnstat -h

vnstat-hourly-output

To display daily traffic, use the -d command line option:

vnstat -d

vnstat-daily-traffic

Similarly, to display weekly and monthly traffic, use the -w and -m command line options, respectively:

Sample traffic for a particular number of seconds using the -tr option

The vnstat command lets you calculate how much traffic goes through the selected interface during a given number of seconds. This can be done using the -tr option. For example, I used the following command to calculate the average amount of traffic that’s going through eth0 in 10 seconds:

vnstat -tr 10

vnstat-sampling

Please note that the default time will be 5 seconds if a number parameter isn’t included.

Display the output in one line

You can also force the vnstat command to produce output in a single line, a format specifically suited for parsing the output of the command from within code. This can be done by using the --oneline option. Here is an example:

vnstat --oneline

vnstat-oneline-output

For more information on vnstat, go through its man page.

Vnstat is a handy tool for those looking to monitor network traffic via command line. It not only provides a wide array of options but is also extremely light on system resources. Have you ever used vnstat or any other similar utility? How was your experience? Share your thoughts in comments below.