Monitor Network traffic & Live up/down speed in linux terminal with vnstat

Vnstat is an Open Source, command-line based network traffic monitoring tool for Linux and BSD. It keeps track of network traffic for different network interfaces. It always runs in the background to do so. It can also show current/live Internet  download/upload speed. It uses network interface information from the Kernal and is not a packet sniffer therefore it ensures light use of system resources.

Install vnstat

Open up your terminal (ctrl+alt+t) and enter
(for Ubuntu/Debian/Linux Mint)

sudo apt-get install vnstat

After installation it will automatically start and it will auto start at each system boot. you can check this using systemctl

aman@vostro:~$ systemctl status vnstat
● vnstat.service - vnStat network traffic monitor
Loaded: loaded (/lib/systemd/system/vnstat.service; enabled; vendor preset: e
Active: active (running) since Mon 2017-01-09 11:46:21 IST; 25s ago
Docs: man:vnstatd(1)
man:vnstat(1)
man:vnstat.conf(5)
Main PID: 8511 (vnstatd)
CGroup: /system.slice/vnstat.service
└─8511 /usr/sbin/vnstatd -n

Jan 09 11:46:21 vostro systemd[1]: Started vnStat network traffic monitor.

Configure vnstat

open the configuration file and make changes to it as you wish.

Tip: you might wanna change the default interface from eth0 to wlan0, in case you primarly use a wifi connection.

sudo nano /etc/vnstat.conf

Basic Usage

Simply entering vnstat without any arguments will show traffic statistics for default or multiple interfaces (like eth0 and wlan0) for last two months and last two days.

NOTE: vnstat collects data from the day/time it was installed. Since you have just install vnstat, it will show “eth0: Not enough data available yet.”wait for some time then try again. 

Note: rx is for download and tx is for upload (see below)

 aman@vostro:~$ vnstat

rx / tx / total / estimated
eth0: Not enough data available yet.
wlan0:
Oct '15 589.29 MiB / 90.44 MiB / 679.73 MiB
Nov '15 998.20 MiB / 107.32 MiB / 1.08 GiB / 1.34 GiB
yesterday 502.15 MiB / 44.49 MiB / 546.63 MiB
today 376.35 MiB / 45.12 MiB / 421.47 MiB / 3.87 GiB

However, if you want to monitor only specific interface (like wlan0 in this case) , give -i argument followed by the interface name.

aman@vostro:~$ vnstat -i wlan0
Database updated: Wed Nov 25 02:53:24 2015

wlan0 since 10/23/2015

rx: 1.56 GiB tx: 199.25 MiB total: 1.75 GiB

monthly
rx | tx | total | avg. rate
------------------------+-------------+-------------+---------------
Oct '15 589.29 MiB | 90.44 MiB | 679.73 MiB | 2.08 kbit/s
Nov '15 0.98 GiB | 108.81 MiB | 1.09 GiB | 4.38 kbit/s
------------------------+-------------+-------------+---------------
estimated 1.22 GiB | 134 MiB | 1.35 GiB |

daily
rx | tx | total | avg. rate
------------------------+-------------+-------------+---------------
yesterday 502.15 MiB | 44.49 MiB | 546.63 MiB | 51.83 kbit/s
today 382.86 MiB | 46.60 MiB | 429.46 MiB | 338.15 kbit/s
------------------------+-------------+-------------+---------------
estimated 3.10 GiB | 382 MiB | 3.48 GiB |

Get Live Network Speed!

Use the -l option to display the live download/upload speed

NOTE: Since I use a wifi connection, I have set wlan0 as my default interface by making change to the config file (like shown above) so I am getting stats for wlan0 instead of eht0.

aman@vostro:~$ vnstat -l 
Monitoring wlan0... (press CTRL-C to stop)

rx: 2.36 Mbit/s 230 p/s tx: 141 kbit/s 228 p/s

When you stop it by pressing CTRL-C it gives you detailed traffic summary for that live session.

 wlan0  /  traffic statistics

rx | tx
--------------------------------------+------------------
bytes 11.85 MiB | 926 KiB
--------------------------------------+------------------
max 4.35 Mbit/s | 278 kbit/s
average 1.31 Mbit/s | 100.07 kbit/s
min 0 kbit/s | 0 kbit/s
--------------------------------------+------------------
packets 10183 | 10195
--------------------------------------+------------------
max 431 p/s | 431 p/s
average 137 p/s | 137 p/s
min 0 p/s | 0 p/s
--------------------------------------+------------------
time 1.23 minutes

Monitor Data breakdown by Hour, Day, Week or Month.

use options like -h , -d, -m

aman@vostro:~$ vnstat -h
wlan0 03:08
^ r
| r
| r
| r r
| r r
| r r
| r r r
| r r r
| r r r
| r rt r
-+--------------------------------------------------------------------------->
| 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 00 01 02 03

h rx (KiB) tx (KiB) h rx (KiB) tx (KiB) h rx (KiB) tx (KiB)
04 0 0 12 0 0 20 0 0
05 0 0 13 0 0 21 0 0
06 0 0 14 0 0 22 0 0
07 0 0 15 0 0 23 17,601 2,341
08 0 0 16 0 0 00 78,096 15,928
09 0 0 17 0 0 01 187,110 18,874
10 0 0 18 0 0 02 133,648 14,055
11 0 0 19 0 0 03 1,950 520
aman@vostro:~$ vnstat -d 

wlan0 / daily

day rx | tx | total | avg. rate
------------------------+-------------+-------------+---------------
10/23/2015 18.14 MiB | 5.41 MiB | 23.55 MiB | 2.23 kbit/s
10/24/2015 387.71 MiB | 48.29 MiB | 436.00 MiB | 41.34 kbit/s
10/25/2015 183.44 MiB | 36.73 MiB | 220.18 MiB | 20.88 kbit/s
11/23/2015 119.71 MiB | 17.72 MiB | 137.43 MiB | 13.03 kbit/s
11/24/2015 502.15 MiB | 44.49 MiB | 546.63 MiB | 51.83 kbit/s
11/25/2015 391.41 MiB | 48.22 MiB | 439.63 MiB | 318.60 kbit/s
------------------------+-------------+-------------+---------------
estimated 2.92 GiB | 367 MiB | 3.28 GiB |
aman@vostro:~$ vnstat -w 

wlan0 / weekly

rx | tx | total | avg. rate
---------------------------+-------------+-------------+---------------
last 7 days 0.99 GiB | 110.66 MiB | 1.10 GiB | 17.38 kbit/s
current week 0.99 GiB | 110.66 MiB | 1.10 GiB | 49.95 kbit/s
---------------------------+-------------+-------------+---------------
estimated 3.26 GiB | 362 MiB | 3.61 GiB |

Leave a Reply

Your email address will not be published.