FreeBSD Install vnStat – A Console-Based Network Traffic Monitor

How do I install vnStat under FreeBSD version 11/12/13 operating system to keep track of my network traffic?

vnStat is a console-based network traffic monitor for FreeBSD and Unix-like systems, including Linux. It keeps a log of hourly, daily, and monthly network traffic for the selected network interface(s). However, it isn’t a packet sniffer. The traffic information is analyzed from the proc(5) and sys filesystems or kernel depending on availability. That way, vnStat can be used even without root permissions on most systems. These examples only work on FreeBSD operating systems. Linux specific installation instructions are here. Note that virtual and aliased interfaces cannot be monitored because the kernel doesn’t provide traffic information for that type of interface.
Tutorial details
Difficulty Easy (rss)
Root privileges Yes
Requirements FreeBSD Unix
Time 5m

FreeBSD install vnstat – console-based network traffic monitor

The procedure is as follows for installing vnstat on FreeBSD Unix system.

Step 1 – Update ports tree or pkg database

You must update port tree by typing the following portsnap command:
# portsnap fetch update
For binary installation update pkg database:
# pkg update

Step 2 – Installing vnstat on FreeBSD

Type the following command to find out exact port location:
# cd /usr/ports
# whereis vnstat

We will see path:

/usr/ports/net/vnstat

Cd into that path and install the vnstat, run:
# cd /usr/ports/net/vnsta
# make install clean
# rehash

Alternatively, you can install the binary package using the pkg_add command on an older version of FreeBSD, enter:
# pkg_add -r -v vnstat
Use the pkg command on the latest version of FreeBSD version 11/12/13+:
# pkg install vnstat
Make sure you confirm installation when prompted. For instance:

Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 15 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
	expat: 2.2.8
	fontconfig: 2.13.92_2,1
	freetype2: 2.10.4
	gettext-runtime: 0.21
	giflib: 5.2.1
	indexinfo: 0.3.1
	jbigkit: 2.1_1
	jpeg-turbo: 2.0.5
	libedit: 3.1.20191231,1
	libgd: 2.3.0,1
	png: 1.6.37
	sqlite3: 3.33.0,1
	tiff: 4.1.0
	vnstat: 2.6_1
	webp: 1.1.0

Number of packages to be installed: 15

The process will require 29 MiB more space.
6 MiB to be downloaded.

Proceed with this action? [y/N]: y
[1/15] Fetching vnstat-2.6_1.txz: 100%  108 KiB 110.6kB/s    00:01   
....
..
.....
vnstat has been installed.

A sample configuration file has been installed in /usr/local/etc/
Please add your default network interface in the 'Interface' line there
before starting vnstat service.

For more information about vnStat use "man vnstat" or visit:
http://humdi.net/vnstat/

Step 3 – Enabling vnstat service on FreeBSD

Type any one of the following command. Let us use the echo command:
# echo 'vnstat_enable="YES"' >> /etc/rc.conf
OR use the sysrc command:
# sysrc vnstat_enable=YES
vnstat: -> YES

How do I start/stop/restart vnstat service?

You can start/stop/restart the vnstat using the following syntax:
# service vnstat start
# service vnstat stop
# service vnstat restart

Step 4 – Configuring vnStat on your FreeBSD server or router/firewall

Now, vnstat has been installed. However, you need to configure /usr/local/etc/vnstat.conf file, enter:
## [ NOTE: cp only needed on an older version of FreeBSD such as FreeBSD 8.x/9.x ] ##
# cp /usr/local/etc/vnstat.conf.sample /usr/local/etc/vnstat.conf
# vi /usr/local/etc/vnstat.conf

Set default Ethernet interface on FreeBSD:

Interface "em0"

Set maximum bandwidth (Mbit) for all interfaces, use 0 to disable feature. In this example, set to 100Mbit:

MaxBandwidth 100

Save and close the file when using vi or vim.

Step 5 – Start the service to track information about interface(s)

All you have to do is type the following command:
# service vnstat start
And we will see confirmation on our screen:

Starting vnstat.
No interfaces found in database, adding available interfaces...
Interface "em0" added with 1000 Mbit bandwidth limit.
Interface "ue0" added with 1000 Mbit bandwidth limit.
-> 2 new interfaces found.
Limits can be modified using the configuration file. See "man vnstat.conf".
Unwanted interfaces can be removed from monitoring with "vnstat --remove".

Step 6 – How do I view network traffic?

Open the terminal and then simply type the vnstat command, run:
# vnstat
Here is what we see:

                      rx      /      tx      /     total    /   estimated
 em1:
       Nov '09      2.97 GiB  /   56.38 GiB  /   59.35 GiB
       Dec '09     35.29 MiB  /  604.47 MiB  /  639.76 MiB  /   20.71 GiB
     yesterday     39.43 MiB  /  418.21 MiB  /  457.64 MiB
         today     35.29 MiB  /  604.47 MiB  /  639.76 MiB  /     683 MiB

 em0:
       Nov '09    205.19 MiB  /  134.89 MiB  /  340.07 MiB
       Dec '09      1.53 MiB  /     938 KiB  /    2.45 MiB  /       0 KiB
     yesterday      1.56 MiB  /     769 KiB  /    2.31 MiB
         today      1.53 MiB  /     938 KiB  /    2.45 MiB  /      --    

Here is another outputs:

Fig.01: vnstat command in action on my FreeBSD box

We can select interface too:
$ vnstat -i {interface}
$ vnstat -i em1

See Daily Traffic, Enter:

$ vnstat -d
$ vnstat -d -i em1

For em1 Ethernet:

 em1  /  daily

         day         rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
      11/20/09     44.69 MiB |  654.44 MiB |  699.13 MiB |   66.29 kbit/s
      11/21/09     30.52 MiB |  322.34 MiB |  352.86 MiB |   33.46 kbit/s
      11/22/09     31.04 MiB |  313.65 MiB |  344.70 MiB |   32.68 kbit/s
      11/23/09    208.52 MiB |    7.04 GiB |    7.24 GiB |  703.35 kbit/s
      11/24/09     57.55 MiB |    1.00 GiB |    1.06 GiB |  102.95 kbit/s
      11/25/09    710.73 MiB |  705.73 MiB |    1.38 GiB |  134.30 kbit/s
      11/26/09     65.78 MiB |    1.52 GiB |    1.59 GiB |  154.01 kbit/s
      11/27/09     45.24 MiB |  641.35 MiB |  686.59 MiB |   65.10 kbit/s
      11/28/09     95.72 MiB |  280.59 MiB |  376.32 MiB |   35.68 kbit/s
      11/29/09     80.17 MiB |    1.77 GiB |    1.85 GiB |  179.37 kbit/s
      11/30/09    271.64 MiB |   10.62 GiB |   10.89 GiB |    1.06 Mbit/s
      12/01/09     44.13 MiB |  368.22 MiB |  412.35 MiB |   39.10 kbit/s
      12/02/09     45.46 MiB |  699.37 MiB |  744.83 MiB |   70.62 kbit/s
      12/03/09     54.18 MiB |    1.01 GiB |    1.06 GiB |  103.05 kbit/s
      12/04/09     44.90 MiB |  668.54 MiB |  713.43 MiB |   67.64 kbit/s
      12/05/09     28.79 MiB |  325.02 MiB |  353.81 MiB |   33.55 kbit/s
      12/06/09     55.02 MiB |    1.35 GiB |    1.41 GiB |  136.60 kbit/s
      12/07/09    276.27 MiB |   10.73 GiB |   11.00 GiB |    1.07 Mbit/s
      12/08/09     38.15 MiB |  343.64 MiB |  381.79 MiB |   36.20 kbit/s
      12/09/09     43.77 MiB |  625.01 MiB |  668.78 MiB |   63.41 kbit/s
      12/10/09     36.95 MiB |  348.90 MiB |  385.84 MiB |   36.58 kbit/s
      12/11/09     54.36 MiB |  320.11 MiB |  374.47 MiB |   35.51 kbit/s
      12/12/09    138.14 MiB |    1.41 GiB |    1.55 GiB |  150.33 kbit/s
      12/13/09     32.60 MiB |  410.92 MiB |  443.52 MiB |   42.05 kbit/s
      12/14/09    280.86 MiB |   10.51 GiB |   10.79 GiB |    1.05 Mbit/s
      12/15/09     49.07 MiB |  717.77 MiB |  766.83 MiB |   72.71 kbit/s
      12/16/09     39.99 MiB |  356.22 MiB |  396.22 MiB |   37.57 kbit/s
      12/17/09     54.12 MiB |  743.57 MiB |  797.69 MiB |   75.63 kbit/s
      12/18/09     39.43 MiB |  418.21 MiB |  457.64 MiB |   43.39 kbit/s
      12/19/09     35.33 MiB |  604.59 MiB |  639.92 MiB |   64.72 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated        37 MiB |     644 MiB |     681 MiB |

See Traffic For Months, Enter:

vnstat -m
Sample outputs:

 em1  /  monthly

       month        rx      |     tx      |    total    |   avg. rate
    ------------------------+-------------+-------------+---------------
      Aug '09      2.24 GiB |   42.50 GiB |   44.74 GiB |  140.13 kbit/s
      Sep '09      1.68 GiB |   37.86 GiB |   39.54 GiB |  127.96 kbit/s
      Oct '09      2.12 GiB |   48.09 GiB |   50.21 GiB |  157.27 kbit/s
      Nov '09      2.97 GiB |   56.38 GiB |   59.35 GiB |  192.06 kbit/s
      Dec '09     35.33 MiB |  604.59 MiB |  639.92 MiB |   64.72 kbit/s
    ------------------------+-------------+-------------+---------------
    estimated      1.13 GiB |   19.50 GiB |   20.63 GiB |

Other Options:

Show traffic for the last 24 hours, enter:
$ vnstat -h
Display all time top10 traffic days, run:
$ vnstat -t
Want to display traffic for 7 days, current and previous week? Run:
$ vnstat -w
To see current transfer rate for the selected interface in real time until interrupted. Statistics will be shown after interruption if runtime was more than 10 seconds, run:
$ vnstat -l
Here is what wee see:

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

   rx:       28 kbit/s    25 p/s          tx:      108 kbit/s    22 p/s


 em1   /  traffic statistics

                           rx         |       tx
--------------------------------------+------------------------
  bytes                      729 KiB  |        3.95 MiB
--------------------------------------+------------------------
          max             236 kbit/s  |     1.84 Mbit/s
      average           72.00 kbit/s  |   399.80 kbit/s
          min               8 kbit/s  |        8 kbit/s
--------------------------------------+------------------------
  packets                       4254  |            4424
--------------------------------------+------------------------
          max                130 p/s  |         185 p/s
      average                 52 p/s  |          54 p/s
          min                  8 p/s  |           8 p/s
--------------------------------------+------------------------
  time                  1.35 minutes

Adding a new interface to the database

Pass the --add option along with -i as follows:
# vnstat --add -i wg0lnsg1
Confirmation from vnstat:

Adding interface "wg0lnsg1" for monitoring to database...

Restart the vnStat daemon if it is currently running in order to start monitoring "wg0lnsg1".

I added my FreeBSD wireguard client interface named wg0lnsg1. Now all we have to do is restart the service. Hence, run:
# service vnstat restart
Outputs:

Stopping vnstat.
Waiting for PIDS: 54546.
Starting vnstat.

How to delete or remove interface when using vnstat

The syntax is:
# vnstat --remove -i interface
# vnstat --remove -i em2

Summing up

You learned how to install, set up, and configure vnStat on FreeBSD to monitor various network traffic logs from the CLI. The vnstat is a client, and the vnstatd is a daemon for data retrieval, caching, and storage on FreeBSD.

This entry is 3 of 5 in the vnStat Monitor and Log Network Traffic Tutorial series. Keep reading the rest of the series:
  1. How to install vnstat on a CentOS/RHEL/Fedora Linux
  2. View Vnstat Graphs Using PHP Web Interface Frontend
  3. How to install vnstat on FreeBSD Unix server
  4. Fedora Linux install vnstat network traffic monitor
  5. Ubuntu install vnstat console network traffic monitor

🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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
3 comments… add one
  • jaysunn Dec 20, 2009 @ 7:51

    Sweet Cron Job Entry as well as tutorial.

    As always,

    Thanks for growing my knowledge,,,,,,,,

    JAYSUNN

  • chatzki Apr 10, 2014 @ 11:29

    Thanks for the post, it made my day easier)

  • bsd4life May 26, 2017 @ 20:52

    no need to run from cron, just start the daemon!

    sysrc vnstat=YES
    service vnstat start

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz