Mastering iftop: Real-Time Bandwidth Usage Monitoring for Linux & macOS
In a world where network performance can make or break application reliability, having tools to monitor and analyze bandwidth usage is critical. While tools like top or htop excel at tracking system resources, they fall short when you need granular visibility into network traffic flows. Enter iftop—a powerful command-line utility that provides real-time, interactive monitoring of network bandwidth usage per connection.
Unlike aggregate tools such as vnstat, iftop focuses on individual network conversations (source-destination pairs), allowing you to quickly identify bandwidth hogs, diagnose unusual traffic patterns, and troubleshoot slow network links. Whether you’re a system administrator optimizing server performance, a developer debugging application network issues, or a power user monitoring home network activity, iftop is an indispensable addition to your toolkit.
This guide will take you from installing iftop to mastering its advanced features, including filtering, scripting, and troubleshooting common network problems. We’ll cover best practices, real-world examples, and everything you need to leverage iftop effectively.
Table of Contents#
- Installation
- Basic Usage: Getting Started
- Key Features & Interactive Commands
- Advanced Usage
- Common & Best Practices
- Troubleshooting Scenarios
- Conclusion
- References
Installation#
iftop is available for most Linux distributions and macOS. It requires root privileges to capture raw network packets, so you’ll need to use sudo for most commands.
Debian/Ubuntu#
Install directly from official repositories:
sudo apt update
sudo apt install -y iftopRHEL/CentOS/Fedora#
iftop is part of the EPEL repository. First enable EPEL, then install:
# RHEL/CentOS 7
sudo yum install -y epel-release
sudo yum install -y iftop
# RHEL/CentOS 8+ / Fedora
sudo dnf install -y epel-release
sudo dnf install -y iftopmacOS#
Use Homebrew to install:
brew update
brew install iftopNote: macOS may require additional permissions for iftop to access network interfaces.
Basic Usage: Getting Started#
Launching iftop#
Start monitoring traffic with root privileges:
sudo iftopBy default, iftop monitors the first active interface (e.g., eth0, wlan0). To specify a different interface:
sudo iftop -i wlan0Understanding the iftop Interface#
The interface is divided into three core sections:
1. Bandwidth Graph (Top)#
A horizontal bar graph visualizing total bandwidth usage over 2, 10, and 40 seconds. It highlights sudden spikes or sustained high usage.
2. Connection List (Middle)#
Displays active network conversations with real-time rates:
- Source/Destination: Left/right columns show IPs/hostnames. Arrows (
->,<-,<->) indicate traffic direction. - Bandwidth Rates: Three columns show average usage for each conversation over 2s, 10s, and 40s. Color-coded bars represent relative usage.
3. Summary Statistics (Bottom)#
- Cumulative: Total bytes transferred since launch.
- Peak Rates: Highest bandwidth observed during the session.
- Total Transfer: Sum of all incoming/outgoing traffic.
Key Features & Interactive Commands#
Interactive Navigation#
Press these keys while iftop is running to customize output:
| Command | Description |
|---|---|
q | Quit iftop |
n | Toggle IP addresses (disable DNS resolution) |
p | Toggle port number visibility |
s | Sort connections by source address |
d | Sort connections by destination address |
r | Sort by current bandwidth rate (default) |
f | Edit traffic filters (BPF syntax) |
t | Switch to plain text mode (no ncurses) |
Packet Filtering with BPF Syntax#
Use Berkeley Packet Filter (BPF) syntax to narrow traffic to specific flows. Apply filters via the command line (-f) or interactively (f):
# Monitor traffic to a specific IP
sudo iftop -f "dst 192.168.1.100"
# Filter HTTPS traffic (port 443)
sudo iftop -f "dst port 443"
# Show only TCP traffic
sudo iftop -f "tcp"
# Exclude DNS queries (port 53)
sudo iftop -f "not port 53"Sorting Connections#
- By Source: Press
sto group traffic by source IP/hostname. - By Destination: Press
dto focus on destination endpoints. - By Bandwidth: Press
rto sort by current rate (default) orlto sort by total transfer.
Advanced Usage#
Command-Line Flags for Customization#
| Flag | Description |
|---|---|
-n | Disable DNS resolution (reduce overhead) |
-P | Show port numbers by default |
-m <limit> | Set bandwidth graph limit (e.g., -m 10M for 10 Mbps) |
-s <seconds> | Run for a specified duration then exit |
-t | Use plain text mode (ideal for SSH without X11) |
Example: Monitor eth0 with IPs, ports, and a 10 Mbps graph limit:
sudo iftop -i eth0 -n -P -m 10MSaving Output to File#
Save text-mode output for post-analysis:
# Capture 60 seconds of traffic to a file
sudo iftop -t -s 60 -n -P > iftop_traffic_report.txtScripting with iftop#
Integrate into automated monitoring scripts. Example: Alert on high bandwidth usage:
#!/bin/bash
INTERFACE="eth0"
THRESHOLD="10M"
# Capture 10 seconds of traffic and extract current rate
CURRENT_RATE=$(sudo iftop -t -s 10 -n -i $INTERFACE | grep "Cumulative send" | awk '{print $4}')
if [[ "$CURRENT_RATE" > "$THRESHOLD" ]]; then
echo "High bandwidth alert: $CURRENT_RATE on $INTERFACE" | mail -s "Network Alert" [email protected]
fiCommon & Best Practices#
- Always Use Root:
iftopneeds raw packet access—run withsudo. - Disable DNS for Busy Servers: Use
-nto avoid overhead from DNS queries. - Filter Strategically: Focus on specific IPs/ports to isolate issues quickly.
- Combine with Other Tools: Pair
iftopwithssto identify processes behind high-traffic connections:sudo ss -tulpn | grep "<port-number>" - Monitor Specific Interfaces: Servers often have multiple interfaces—specify
-ito avoid missing critical traffic. - Save Output for Post-Mortem: Capture sessions during incidents to review later.
Troubleshooting Scenarios#
1. Identifying Bandwidth Hogs#
Problem: Server latency spikes due to excessive bandwidth usage. Solution:
- Run
sudo iftop -n -Pto see IPs and ports. - Identify the top traffic flow.
- Use
sudo ss -tulpnto find the associated process. - Verify if the process is legitimate (e.g., backup) or malicious.
2. Investigating Unusual Outbound Traffic#
Problem: Unexpected outbound traffic from your server. Solution:
- Filter outbound traffic:
sudo iftop -f "src host <your_ip>" -nto view outgoing connections. - Look up suspicious IPs with
whois <ip-address>. - Block malicious IPs with
iptablesor your firewall.
3. Diagnosing Port-Specific Congestion#
Problem: Web server slowdown on port 443. Solution:
- Monitor HTTPS traffic:
sudo iftop -f "dst port 443". - Identify top clients consuming bandwidth.
- Implement rate limiting (e.g., Nginx
limit_reqmodule) for abusive clients.
Conclusion#
iftop is a lightweight yet powerful tool for real-time network monitoring. Its focus on individual traffic flows makes it ideal for troubleshooting bandwidth issues, identifying anomalies, and optimizing network performance. By mastering its installation, filtering, and scripting capabilities, you’ll be able to resolve network problems faster and keep your systems running smoothly.