Using the vnstat Command (with examples)

Using the vnstat Command (with examples)

The vnstat command is a useful tool for monitoring network traffic on Linux systems. It provides a console-based interface to display traffic statistics for different network interfaces. In this article, we will explore various use cases of the vnstat command and provide code examples for each case.

Display traffic summary for all interfaces

To display the traffic summary for all interfaces, simply run the following command:

vnstat

Motivation: This use case is helpful when you want to quickly view the overall network traffic on your system. It provides an overview of the amount of data transferred in both directions (upload and download) for each network interface.

Explanation: The vnstat command without any arguments returns the traffic summary for all available network interfaces. It displays information such as the total data usage, average data rate, and peak usage for the selected time period.

Example Output:

Database updated: Tue Jan 26 2021 23:36:45

   eth0 since 10/07/2020

          rx: 1.67 TiB      tx: 178.90 GiB      total: 1.83 TiB

   lo since 10/07/2020

          rx: 0.00 B        tx: 0.00 B         total: 0.00 B

   docker0 since 04/17/2021

          rx: 1.00 MiB      tx: 7.24 GiB        total: 7.25 GiB

Display traffic summary for a specific network interface

If you want to view the traffic summary for a specific network interface, you can use the following command:

vnstat -i eth0

Motivation: This use case is useful when you want to focus on a specific network interface instead of all the interfaces. It allows you to monitor the traffic usage of a particular interface, making it easier to troubleshoot or analyze network performance.

Explanation: The -i option is used to specify the network interface for which you want to display the traffic summary. In the example command above, eth0 is the network interface name. Replace it with the desired interface name on your system.

Example Output:

Database updated: Tue Jan 26 2021 23:36:45

   eth0 since 10/07/2020

          rx: 1.67 TiB      tx: 178.90 GiB      total: 1.83 TiB

Display live stats for a specific network interface

To get real-time statistics for a specific network interface, you can use the following command:

vnstat -l -i eth0

Motivation: This use case is helpful when you want to monitor the network traffic in real-time for a specific interface. It provides continuous updates on the amount of data transferred, making it suitable for monitoring network usage during ongoing activities.

Explanation: The -l option puts vnstat in live mode, continuously updating the displayed statistics in the console. The -i option is used to specify the network interface for which you want to display the live stats.

Example Output:

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

   rx:     4.98 kbit/s    13 p/s          tx:     6.78 kbit/s    19 p/s
   rx:     5.42 kbit/s    14 p/s          tx:     7.59 kbit/s    20 p/s
   rx:     5.45 kbit/s    14 p/s          tx:     7.62 kbit/s    20 p/s
   ...

Show traffic statistics on an hourly basis for the last 24 hours using a bar graph

To view traffic statistics on an hourly basis for the last 24 hours, you can use the following command:

vnstat -hg

Motivation: This use case is useful when you want to analyze network traffic patterns over a specific period, such as the last 24 hours. The bar graph representation provides a visual representation of the traffic distribution throughout the day.

Explanation: The -h option is used to display the data in an hourly format, showing traffic statistics for each hour. The -g option enables the display of the bar graph.

Example Output:

 eth0                                                                     09:01
   30 Mbps                                                                    ▂
   25 Mbps                                                                    ▂
   20 Mbps                                                                    ▂
   15 Mbps                                                                    ▂
   10 Mbps                                                                    ▂
    5 Mbps                                                                    ▂
    0 Mbps                                                                    ▂


 eth0                                                                     10:01
    3 Mbps                                                                    ▂
    2 Mbps                                                                    ▂
    1 Mbps                                                                    ▂
    0 Mbps                                                                    ▂

Measure and show average traffic for 30 seconds

To measure and display the average traffic for a specific duration, you can use the following command:

vnstat -tr 30

Motivation: This use case is helpful when you want to measure the average traffic rate over a short period, such as 30 seconds. It allows you to quickly check the network usage during a specific activity or event.

Explanation: The -t option is used to specify the time duration (in seconds) for which you want to measure the average traffic. In the example command above, 30 represents the duration of 30 seconds.

Example Output:

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

   rx:   518.97 kbit/s    tx:     9.64 kbit/s
   rx:   520.15 kbit/s    tx:     7.06 kbit/s
   rx:   514.77 kbit/s    tx:     6.88 kbit/s
   ...

Conclusion

In this article, we explored different use cases of the vnstat command and provided code examples for each case. We learned how to display the traffic summary for all interfaces, a specific network interface, and how to view live statistics. Additionally, we saw how to analyze traffic patterns using a bar graph and measure average traffic for a specified duration. Understanding these various use cases of the vnstat command can help sysadmins and network administrators monitor and manage network traffic effectively.

Related Posts

How to use the command 'odps' (with examples)

How to use the command 'odps' (with examples)

The ‘odps’ command is the command-line tool for Aliyun ODPS (Open Data Processing Service).

Read More
How to use the command 'gcloud components update' (with examples)

How to use the command 'gcloud components update' (with examples)

This article provides examples of how to use the ‘gcloud components update’ command to update Google Cloud CLI components.

Read More
How to use the command gnome-screenshot (with examples)

How to use the command gnome-screenshot (with examples)

This article provides several use cases of the gnome-screenshot command, along with their respective code, motivation, explanation, and example output.

Read More