Use VnStat to Monitor Network Traffic from Linux Command Line

Spread the love

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.

Introduction

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

Download and Install

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.

Usage examples

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

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:

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

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

vnstat -d

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

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

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

Conclusion

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.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Himanshu Arora

Himanshu Arora is a freelance technical writer by profession but a software programmer and Linux researcher at heart. He covers software tutorials, reviews, tips/tricks, and more. Some of his articles have been featured on IBM developerworks, ComputerWorld, and in Linux Journal.

Comments (1)