How to install vnstat on Alpine Linux

VnStat is a free and open-source app for servers and routers. It is a console-based network traffic monitor. It keeps a log of the 5-minute interval, hourly, daily, monthly, and yearly network traffic for the selected interface(s). Let us see how to install vnStat on the Alpine Linux server to keep a tab on bandwidth usage.

Open the Terminal App and then log in using the ssh command to install vnStat on the Alpine Linux server:
ssh ec2-user@alpine-ec2-server
# My Linode server
ssh root@linode-server-vm1

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Alpine Linux
Est. reading time 2 minutes
We have two parts here:

  1. vnstatd – daemon based database updating for vnStat
  2. vnstat – a console-based network traffic monitor that query database created by the vnstatd

Installing vnstat on Alpine Linux

Make sure the community repository is enabled. Edit the /etc/apk/repositories:
# vim /etc/apk/repositories

http://dl-cdn.alpinelinux.org/alpine/v3.13/main
http://dl-cdn.alpinelinux.org/alpine/v3.13/community

Update repo database

The following apk command forces updating of the indexes from all configured package repositories, run:
# apk update
Apply any pending upgrades to installed packages to the latest version available from configured package repositories, enter:
# apk upgrade

Search for vnstat package

Let us see available vnstat packages on your Alpine Linux box:
# apk search vnstat
List:

vnstat-openrc-2.6-r2
vnstat-2.6-r2
vnstat-doc-2.6-r2

Installation

Run the following apk command:
# apk add vnstat
We can also add/install man pages in Alpine Linux for vnstat:
# apk add vnstat-doc
How to install vnstat on Alpine Linux

Configuration

Edit the /etc/vnstat.conf using the vim command or any other editor of your choice:
# vi /etc/vnstat.conf
Want to find out interfaces name on Linux? Use the ip command as follows:
ip link show
The following output indicates that I have loopback (lo0) and Ethernet (eth0) NICs:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether c4:54:44:1f:f7:35 brd ff:ff:ff:ff:ff:ff

First we need to set default network interface name. You can leave empty for automatic selection but I recommend that you set up your main Eth name here:
Interface "eth0"
Set date output formats:

DayFormat    "%Y-%m-%d"
MonthFormat  "%Y-%m"
TopFormat    "%Y-%m-%d"

Set IEC standard prefixes when traffic is shown. In other words, display traffic in KiB/MiB/GiB format:
UnitMode 0
Save and close the file.

Enable vnstatd service on Alpine Linux

Use the rc-update command as follows to enable vnstatd service at boot time:
# rc-update add vnstatd

How to start/stop/restart vnstatd service

The syntax is as follows for the service command to control vnStat:
# service vnstatd start
# service vnstatd stop
# service vnstatd restart
# service vnstatd status

Starting and stopping vnstatd service on Alpine Linux

Viewing stats

Root access is not needed to view bandwidth statistics.

Open the terminal and then run:
vnstat
Here are stats from eth0 and Wireguard wg0 stats on Alpine Linux:

 
                      rx      /      tx      /     total    /   estimated
 eth0:
       2021-02    120.62 GiB  /  122.09 GiB  /  242.71 GiB
       2021-03      5.45 GiB  /    5.48 GiB  /   10.93 GiB  /   68.22 GiB
     2021-03-04   917.71 MiB  /  920.74 MiB  /    1.80 GiB
     2021-03-05     1.08 GiB  /    1.09 GiB  /    2.17 GiB  /    2.24 GiB
 
 wg0:
       2021-02      5.32 GiB  /  114.57 GiB  /  119.88 GiB
       2021-03    308.75 MiB  /    5.09 GiB  /    5.39 GiB  /   33.62 GiB
     2021-03-04    64.83 MiB  /  840.80 MiB  /  905.63 MiB
     2021-03-05    64.92 MiB  /    1.00 GiB  /    1.07 GiB  /    1.10 GiB

We can limit stats to the wg0 interface as follows:
vnstat -i {name}
vnstat -i wg0
vnstat -i tun0
vnstat -i wan1
vnstat -i lan

We can also display traffic summary for a merge of interfaces eth0, eth1 and eth2.
vnstat -i eth0+eth1+eth3
Please note that when -i skipped vnstat will use default interface set up in your /etc/vnstat.conf file. Let us find out monthly bandwidth usage, run:
vnstat -h
vnstat -i wg0 -h

 eth0  /  hourly
 
         hour        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     2021-03-05
         00:00    215.45 KiB |   86.55 KiB |  301.99 KiB |      687 bit/s
         01:00    193.94 KiB |   32.62 KiB |  226.56 KiB |      515 bit/s
         02:00    118.09 MiB |  119.53 MiB |  237.62 MiB |  553.70 kbit/s
         03:00    195.14 KiB |   32.43 KiB |  227.57 KiB |      517 bit/s
         04:00    218.15 KiB |   90.95 KiB |  309.10 KiB |      703 bit/s
         05:00    198.29 KiB |   32.77 KiB |  231.07 KiB |      525 bit/s
         06:00    116.24 MiB |  117.24 MiB |  233.48 MiB |  544.05 kbit/s
         07:00    199.29 KiB |   34.15 KiB |  233.44 KiB |      531 bit/s
         08:00    220.96 KiB |   88.86 KiB |  309.83 KiB |      705 bit/s
         09:00    199.49 KiB |   31.86 KiB |  231.34 KiB |      526 bit/s
         10:00    296.73 MiB |  298.69 MiB |  595.42 MiB |    1.39 Mbit/s
         11:00    275.92 KiB |   33.13 KiB |  309.04 KiB |      703 bit/s
         12:00    242.26 KiB |  108.64 KiB |  350.90 KiB |      798 bit/s
         13:00    198.14 KiB |   31.68 KiB |  229.82 KiB |      522 bit/s
         14:00    114.90 MiB |  116.25 MiB |  231.15 MiB |  538.61 kbit/s
         15:00    193.16 KiB |   28.96 KiB |  222.13 KiB |      505 bit/s
         16:00     74.23 MiB |   75.48 MiB |  149.71 MiB |  348.84 kbit/s
         17:00    146.12 MiB |  145.49 MiB |  291.62 MiB |  679.51 kbit/s
         18:00    116.93 MiB |  118.25 MiB |  235.18 MiB |  548.02 kbit/s
         19:00    192.78 KiB |   28.61 KiB |  221.39 KiB |      503 bit/s
         20:00    226.13 KiB |   85.76 KiB |  311.89 KiB |      709 bit/s
         21:00    202.47 KiB |   32.92 KiB |  235.38 KiB |      535 bit/s
         22:00    116.09 MiB |  117.42 MiB |  233.51 MiB |  544.11 kbit/s
         23:00      2.89 MiB |    2.91 MiB |    5.80 MiB |   65.73 kbit/s
     ------------------------+-------------+-------------+---------------

Display 5 minutes bandwidth stats

vnstat -5
vnstat -5 -i wg0
vnstat -5 [limit] -i wg0
vnstat -5 10 -i wg0

See hourly stats graph

vnstat -hg

 eth0                                                                     23:12 
  ^                                 t                                           
  |                                rt                                           
  |                                rt                                           
  |                                rt                                           
  |                                rt                                           
  |                                rt                                           
  |         t                      rt                   rt                      
  |        rt          rt          rt          rt       rt rt          rt       
  |        rt          rt          rt          rt    rt rt rt          rt       
  |        rt          rt          rt          rt    rt rt rt          rt       
 -+---------------------------------------------------------------------------> 
  |  00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23    
 
 h  rx (MiB)   tx (MiB)  ][  h  rx (MiB)   tx (MiB)  ][  h  rx (MiB)   tx (MiB) 
00        0.2        0.1 ][ 08        0.2        0.1 ][ 16       74.2       75.5
01        0.2        0.0 ][ 09        0.2        0.0 ][ 17      146.1      145.5
02      118.1      119.5 ][ 10      296.7      298.7 ][ 18      116.9      118.3
03        0.2        0.0 ][ 11        0.3        0.0 ][ 19        0.2        0.0
04        0.2        0.1 ][ 12        0.2        0.1 ][ 20        0.2        0.1
05        0.2        0.0 ][ 13        0.2        0.0 ][ 21        0.2        0.0
06      116.2      117.2 ][ 14      114.9      116.2 ][ 22      116.1      117.4
07        0.2        0.0 ][ 15        0.2        0.0 ][ 23        2.9        2.9

Want to see daily stats?

vnstat -d

 eth0  /  daily
 
          day        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     2021-02-04     2.09 GiB |    2.09 GiB |    4.18 GiB |  415.43 kbit/s
     2021-02-05     1.59 GiB |    1.60 GiB |    3.19 GiB |  317.47 kbit/s
     2021-02-06     1.18 GiB |    1.19 GiB |    2.38 GiB |  236.16 kbit/s
     2021-02-07    21.58 GiB |   22.02 GiB |   43.60 GiB |    4.33 Mbit/s
     2021-02-08     6.15 GiB |    6.35 GiB |   12.50 GiB |    1.24 Mbit/s
     2021-02-09     1.30 GiB |    1.31 GiB |    2.61 GiB |  259.23 kbit/s
     2021-02-10     6.21 GiB |    6.31 GiB |   12.52 GiB |    1.24 Mbit/s
     2021-02-11     1.14 GiB |    1.15 GiB |    2.30 GiB |  228.22 kbit/s
     2021-02-12     1.29 GiB |    1.30 GiB |    2.59 GiB |  257.14 kbit/s
     2021-02-13     1.35 GiB |    1.36 GiB |    2.71 GiB |  269.69 kbit/s
     2021-02-14    18.77 GiB |   18.96 GiB |   37.73 GiB |    3.75 Mbit/s
     2021-02-15     2.00 GiB |    2.02 GiB |    4.01 GiB |  398.97 kbit/s
     2021-02-16     1.08 GiB |    1.09 GiB |    2.17 GiB |  215.83 kbit/s
     2021-02-17   868.25 MiB |  871.92 MiB |    1.70 GiB |  168.95 kbit/s
     2021-02-18     1.86 GiB |    1.87 GiB |    3.73 GiB |  370.69 kbit/s
     2021-02-19     1.14 GiB |    1.14 GiB |    2.28 GiB |  226.84 kbit/s
     2021-02-20     1.15 GiB |    1.14 GiB |    2.29 GiB |  227.70 kbit/s
     2021-02-21    18.12 GiB |   18.22 GiB |   36.34 GiB |    3.61 Mbit/s
     2021-02-22     1.03 GiB |    1.04 GiB |    2.07 GiB |  205.38 kbit/s
     2021-02-23     1.79 GiB |    1.79 GiB |    3.58 GiB |  355.92 kbit/s
     2021-02-24     2.09 GiB |    2.09 GiB |    4.18 GiB |  415.16 kbit/s
     2021-02-25     1.22 GiB |    1.24 GiB |    2.46 GiB |  244.27 kbit/s
     2021-02-26   971.47 MiB |  977.38 MiB |    1.90 GiB |  189.22 kbit/s
     2021-02-27   829.45 MiB |  833.88 MiB |    1.62 GiB |  161.49 kbit/s
     2021-02-28    17.20 GiB |   17.45 GiB |   34.66 GiB |    3.45 Mbit/s
     2021-03-01     1.66 GiB |    1.69 GiB |    3.35 GiB |  333.08 kbit/s
     2021-03-02   908.53 MiB |  910.37 MiB |    1.78 GiB |  176.60 kbit/s
     2021-03-03   941.24 MiB |  946.52 MiB |    1.84 GiB |  183.28 kbit/s
     2021-03-04   917.71 MiB |  920.74 MiB |    1.80 GiB |  178.50 kbit/s
     2021-03-05     1.08 GiB |    1.09 GiB |    2.17 GiB |  222.65 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated      1.12 GiB |    1.12 GiB |    2.24 GiB |

View monthly stats

vnstat -m

 eth0  /  monthly
 
        month        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       2020-09     41.57 MiB |    4.64 MiB |   46.21 MiB |      149 bit/s
       2020-10     99.96 GiB |  100.55 GiB |  200.51 GiB |  643.05 kbit/s
       2020-11    114.78 GiB |  116.36 GiB |  231.14 GiB |  766.00 kbit/s
       2020-12    102.51 GiB |  103.67 GiB |  206.18 GiB |  661.25 kbit/s
       2021-01    272.13 GiB |  275.48 GiB |  547.61 GiB |    1.76 Mbit/s
       2021-02    120.62 GiB |  122.09 GiB |  242.71 GiB |  861.79 kbit/s
       2021-03      5.45 GiB |    5.48 GiB |   10.93 GiB |  218.80 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated     33.99 GiB |   34.23 GiB |   68.22 GiB |

Want to see yearly data

vnstat -y

 eth0  /  yearly
 
         year        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
          2020    317.29 GiB |  320.58 GiB |  637.87 GiB |  173.27 kbit/s
          2021    398.19 GiB |  403.06 GiB |  801.25 GiB |    1.25 Mbit/s
     ------------------------+-------------+-------------+---------------
     estimated      2.22 TiB |    2.25 TiB |    4.46 TiB |

Get list of top days

vnstat -t

 eth0  /  top 10
 
    #      day          rx      |     tx      |    total    |   avg. rate
   -----------------------------+-------------+-------------+---------------
    1   2021-01-08    77.41 GiB |   78.92 GiB |  156.33 GiB |   15.54 Mbit/s
    2   2021-01-27    52.90 GiB |   53.38 GiB |  106.28 GiB |   10.57 Mbit/s
    3   2021-01-03    49.36 GiB |   50.03 GiB |   99.39 GiB |    9.88 Mbit/s
    4   2021-01-05    30.69 GiB |   31.18 GiB |   61.87 GiB |    6.15 Mbit/s
    5   2020-10-11    23.98 GiB |   24.31 GiB |   48.29 GiB |    4.80 Mbit/s
    6   2020-10-25    23.22 GiB |   23.60 GiB |   46.82 GiB |    4.65 Mbit/s
    7   2021-02-07    21.58 GiB |   22.02 GiB |   43.60 GiB |    4.33 Mbit/s
    8   2021-02-14    18.77 GiB |   18.96 GiB |   37.73 GiB |    3.75 Mbit/s
    9   2020-11-15    18.43 GiB |   18.73 GiB |   37.16 GiB |    3.69 Mbit/s
   10   2020-12-13    18.15 GiB |   18.26 GiB |   36.41 GiB |    3.62 Mbit/s
   -----------------------------+-------------+-------------+---------------

How to show database in json and xml format

vnstat --json
vnstat --xml

Want to see transfer rate in real time?

vnstat -l

How to add a new interface to database

We can create database entry for interface specified with -i as follows. For instance add a new interface called tun0:
vnstat -i tun0 --add

How to delete database entries for interface eth1 and stop monitoring it

vnstat -i eth1 --remove
Getting help is easy too:
man vnstat
man vnstat.conf
man vnstatd

Summing up

You learned how to install vnstat on Alpine Linux. The vnstat command’s purpose is to provide an interface for querying the traffic information stored in the database. Whereas the daemon vnstatd is responsible for data retrieval, caching, and storage. Although the daemon process is constantly running as a service, it is spending most of its time sleeping between data updates. See vnstat project home page.


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum