Linux vnstat Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux vnstat command, a powerful network monitoring tool, and learn how to install it on Ubuntu 22.04, monitor network traffic, and generate reports to visualize the data. We will start by installing vnstat, then use it to monitor network usage, and finally, generate reports to analyze the collected data. This lab is designed to provide practical examples and insights into network traffic analysis using the vnstat command.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/RemoteAccessandNetworkingGroup -.-> linux/ifconfig("`Network Configuring`") linux/RemoteAccessandNetworkingGroup -.-> linux/netstat("`Network Monitoring`") subgraph Lab Skills linux/apt -.-> lab-422999{{"`Linux vnstat Command with Practical Examples`"}} linux/sudo -.-> lab-422999{{"`Linux vnstat Command with Practical Examples`"}} linux/ifconfig -.-> lab-422999{{"`Linux vnstat Command with Practical Examples`"}} linux/netstat -.-> lab-422999{{"`Linux vnstat Command with Practical Examples`"}} end

Install vnstat on Ubuntu 22.04

In this step, we will install the vnstat network monitoring tool on the Ubuntu 22.04 Docker container.

First, let's update the package index and install the vnstat package:

sudo apt-get update
sudo apt-get install -y vnstat

Example output:

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Fetched 324 kB in 1s (324 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libgd3 libonig5 libpcre2-8-0 libsqlite3-0 libxml2
Suggested packages:
  vnstat-cgi vnstati
The following NEW packages will be installed:
  libgd3 libonig5 libpcre2-8-0 libsqlite3-0 libxml2 vnstat
0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,093 kB of archives.
After this operation, 3,800 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...

Now, let's verify that vnstat is installed correctly:

sudo vnstat --version

Example output:

vnstat 2.6 by Teemu Toivonen <[email protected]>

Great! We have successfully installed the vnstat network monitoring tool on our Ubuntu 22.04 Docker container.

Monitor Network Traffic Using vnstat

In this step, we will use the vnstat command to monitor network traffic on our Ubuntu 22.04 Docker container.

First, let's check the network interfaces on our system:

sudo vnstat --iface

Example output:

Interface:
  eth0

The output shows that the network interface we will be monitoring is eth0.

Now, let's start monitoring the network traffic using vnstat:

sudo vnstat -u -i eth0

This command will start the vnstat daemon and begin monitoring the eth0 interface. The -u option tells vnstat to update the database in the background.

To view the current network traffic statistics, we can use the following commands:

sudo vnstat

Example output:

Database updated: 2023-04-10 12:34:56

eth0:
  rx:  12.45 MiB      tx:  4.32 MiB      total:  16.77 MiB

Monthly Report for eth0
------------------------
Apr '23      rx:  12.45 MiB      tx:  4.32 MiB      total:  16.77 MiB

This shows the total received (rx), transmitted (tx), and total traffic for the eth0 interface.

You can also view more detailed network traffic statistics using the following commands:

sudo vnstat -d
sudo vnstat -h
sudo vnstat -m

These commands will show the daily, hourly, and monthly network traffic statistics, respectively.

Generate Reports and Visualize Network Data with vnstat

In this step, we will learn how to generate reports and visualize network data using the vnstat tool.

First, let's generate a text-based report of the network traffic:

sudo vnstat -i eth0 -d

Example output:

eth0 daily traffic statistics (eth0)

          rx      |     tx      |    total    |   date
-----------------+-------------+-------------+----------
          12.45 M|       4.32 M|      16.77 M| 2023-04-10

This command shows the daily network traffic statistics for the eth0 interface.

To generate a more detailed report, we can use the vnstat -m command:

sudo vnstat -i eth0 -m

Example output:

eth0 monthly traffic statistics (eth0)

        rx      |     tx      |    total    |   month
---------------+-------------+-------------+----------
        12.45 M|       4.32 M|      16.77 M| Apr '23

This command shows the monthly network traffic statistics for the eth0 interface.

To visualize the network data, we can use the vnstati tool, which is part of the vnstat package. Let's generate an image-based report:

sudo vnstati -i eth0 -o eth0_report.png

This command will generate a PNG image file named eth0_report.png in the current directory, which contains a graphical representation of the network traffic data.

You can view the generated report image using an image viewer or by uploading it to a file sharing service.

Summary

In this lab, we first installed the vnstat network monitoring tool on the Ubuntu 22.04 Docker container. We updated the package index, installed the vnstat package, and verified the installation. Then, we used the vnstat command to monitor network traffic on the system, checking the available network interfaces and starting the vnstat daemon to monitor the eth0 interface. Finally, we explored generating reports and visualizing network data with vnstat, using various commands to display detailed information about network usage and traffic patterns.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like