How to Monitor Network Traffic with vnStat and iftop

Understanding your VPS's network traffic patterns — historical usage trends and real-time connection activity — helps with bandwidth planning, troubleshooting, and spotting unusual activity. This guide covers two complementary tools.

vnStat: Historical Bandwidth Tracking

vnStat runs continuously in the background, recording bandwidth usage over time — useful for understanding trends, monthly usage against a data cap, or identifying when a spike occurred.

Step 1 — Install vnStat

sudo apt install vnstat -y

Step 2 — Enable and Start It

sudo systemctl enable --now vnstat

Step 3 — View Traffic Statistics

vnstat

Shows daily, monthly, and all-time totals for the primary network interface.

Viewing Hourly Breakdown

vnstat -h

Viewing a Live Traffic Rate

vnstat -l

Monitoring a Specific Interface

vnstat -i eth0

iftop: Real-Time Connection-Level Monitoring

While vnStat shows historical totals, iftop shows exactly which connections are consuming bandwidth right now — essential for diagnosing an active traffic spike.

Step 1 — Install iftop

sudo apt install iftop -y

Step 2 — Run iftop

sudo iftop

Displays a live, updating list of active connections sorted by bandwidth usage, showing source, destination, and current transfer rate for each.

Running iftop on a Specific Interface

sudo iftop -i eth0

Displaying Port Numbers (Useful for Identifying the Service)

sudo iftop -P

Using iftop to Diagnose an Unexpected Traffic Spike

  1. Run iftop during the spike
  2. Identify the connection(s) consuming the most bandwidth
  3. Cross-reference the source/destination IP and port with your running services to identify the cause
  4. If the traffic is unexpected/unauthorized, investigate further as a potential security incident

Setting Up Alerts Based on vnStat Data

sudo nano /usr/local/bin/check-bandwidth.sh
#!/bin/bash
USAGE=$(vnstat --oneline | cut -d';' -f11)
LIMIT_GB=900

# Parse and compare USAGE against LIMIT_GB, alert if approaching a data cap
# (exact parsing depends on vnstat output format/units)

Useful if your VPS plan has a bandwidth allowance and you want proactive warning before approaching it.

Combining Both Tools Effectively

QuestionTool
"How much bandwidth did I use this month?"vnStat
"What's consuming bandwidth right now?"iftop
"When did this month's usage spike happen?"vnStat (hourly view)

Common Errors

vnStat shows no data — verify the service has been running for some time; vnStat needs to actively observe traffic to build up historical data, it doesn't retroactively calculate past usage.

iftop shows nothing — verify you're monitoring the correct interface (check with ip a first) and that there's actually active traffic during your observation window.

Continue Reading

Browse more articles in Advanced Networking & VPN.

  • vnstat, iftop, network traffic monitoring, bandwidth usage tracking
  • 0 Kasutajad peavad seda kasulikuks
Kas see vastus oli kasulik?

Seotud artiklid

How to Set Up a VPN Server with WireGuard

WireGuard is a modern, fast, and simple VPN protocol — significantly easier to configure...

How to Set Up an OpenVPN Server on a VPS

OpenVPN is a mature, widely-supported VPN protocol — a solid choice when you need broad...

How to Configure a VPS as a Forward Proxy with Squid

A forward proxy routes outbound requests through your VPS, useful for accessing geo-restricted...

How to Set Up IPv6 on Your VPS

IPv6 adoption continues to grow, and many VPS providers now offer IPv6 addresses alongside IPv4....

How to Bond Multiple Network Interfaces for Redundancy

Network interface bonding combines multiple physical/virtual network interfaces into a single...