Step 1 – Ubuntu install vnstat using the apt/apt-get
Type the following apt command/apt-get command to update the system:
sudo apt update
sudo apt upgrade
Installation
Execute the following command to set up and install vnstat on Ubuntu server:sudo apt-get install vnstat
ORsudo apt install vnstat
Step 2 – Configure vnstat
The default database directory for each interface located at the following location (use the ls command/cd command):cd /var/lib/vnstat/
ls -l /var/lib/vnstat/
Sample outputs:
Use the ip command to find out your interface name:
ip l show
ip a show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 link/ether 40:9f:38:28:f6:b5 brd ff:ff:ff:ff:ff:ff
Note down the interface name eno1. We need edit the /etc/vnstat.conf file using a text editor such as nano or vim:
sudo nano /etc/vnstat.conf
Set default interface name to eno1(or eth0 on older Ubuntu systems):Interface "eno1"
We can customize date output formats as follows:
DayFormat "%Y-%m-%d" MonthFormat "%Y-%m" TopFormat "%Y-%m-%d"
Set data retention duration policy (use -1 for unlimited and 0 to disable it)
5MinuteHours 48 HourlyDays 4 DailyDays 62 MonthlyMonths 25 YearlyYears -1 TopDayEntries 20
Save and close the file.
Step 3 – Enable and start the vnstat service
We are going to use the systemctl command to control the systemd system such as vnstat.service on Ubuntu Linux. The syntax to enable service is as follows:sudo systemctl enable vnstat.service
Start the vnstat service
sudo systemctl start vnstat.service
Stop the vnstat service
sudo systemctl stop vnstat.service
Restart/reload the vnstat service
After editing the /etc/vnstat.conf, we must restart or reload the service:sudo systemctl restart vnstat.service
ORsudo systemctl reload vnstat.service
Find the status of vnstat service
sudo systemctl status vnstat.service
Step 4 – Viewing network traffic graphs
We have two parts here as follows:
- vnstatd daemon – Background service that monitors and updates interface database as per /etc/vnstat.conf file.
- vnstat command – Command line tool for sysadmins/developers for querying the traffic information stored by the vnstatd daemon. In other words, we need to use the vnstat command.
Let us type the following command on Ubuntu box to query vnstat database:
vnstat