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 available through the EPEL repository on RHEL-based distributions:
# RHEL 8+ / CentOS Stream / Rocky / Alma
sudo dnf install -y epel-release
sudo dnf install -y iftop
# Fedora
sudo dnf install -y iftopmacOS#
Use Homebrew to install:
brew update
brew install iftopNote: On macOS, iftop requires root privileges (sudo) to access network interfaces. You may also need to grant Full Disk Access to your terminal application in System Settings > Privacy & Security.
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 hostname resolution (show/hide IP addresses) |
N | Toggle port-to-service-name resolution |
p | Toggle port number visibility |
S | Toggle source port display |
D | Toggle destination port display |
s | Aggregate traffic by source address |
d | Aggregate traffic by destination address |
< | Sort by source address |
> | Sort by destination address |
1 | Sort by first column (2-second average) |
2 | Sort by second column (10-second average) |
3 | Sort by third column (40-second average) |
f | Edit traffic filters (BPF syntax) |
l | Filter hostnames by POSIX regex |
t | Cycle through display modes (two-line, one-line sent/received/total) |
P | Pause/unpause the display |
L | Toggle logarithmic scale for bar graphs |
T | Show cumulative totals |
j / k | Scroll display up/down (useful with o to freeze order) |
o | Freeze current screen order |
b | Toggle bar graph display |
h | Show help |
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
<to sort by source address, orsto aggregate traffic by source. - By Destination: Press
>to sort by destination address, ordto aggregate traffic by destination. - By Column: Press
1,2, or3to sort by the 2-second, 10-second, or 40-second bandwidth column (default is 10-second).
Advanced Usage#
Command-Line Flags for Customization#
| Flag | Description |
|---|---|
-n | Disable DNS resolution (reduce overhead) |
-N | Disable port-to-service-name resolution |
-P | Show port numbers by default |
-b | Disable bar graph display |
-B | Display bandwidth in bytes/sec instead of bits/sec |
-m <limit> | Set maximum bandwidth for graph scale (e.g., -m 10M for 10 Mbps) |
-F <net/mask> | Show traffic entering/leaving a specific network (e.g., -F 192.168.1.0/24) |
-t | Use plain text mode (ideal for SSH without X11 or for piping to files) |
-c <file> | Use a custom configuration file (default: ~/.iftoprc) |
Example: Monitor eth0 with IPs, ports, and a 10 Mbps graph limit:
sudo iftop -i eth0 -n -P -m 10MSaving Output to File#
Use text mode (-t) to pipe output to a file. You can combine it with timeout to capture for a fixed duration:
# Capture 60 seconds of traffic to a file
timeout 60 sudo iftop -t -n -P > iftop_traffic_report.txtScripting with iftop#
Integrate into automated monitoring scripts. Example: Alert on high bandwidth usage:
#!/bin/bash
INTERFACE="eth0"
THRESHOLD_BYTES=12500000 # 100 Mbps in bytes/sec
# Capture 10 seconds of traffic in text mode and parse total TX rate
CURRENT_RATE=$(timeout 10 sudo iftop -t -n -i "$INTERFACE" 2>/dev/null | grep "TOTAL" | tail -1 | awk '{print $4}')
# Convert and compare (iftop -B shows bytes/sec)
if [[ -n "$CURRENT_RATE" ]] && [[ "$CURRENT_RATE" -gt "$THRESHOLD_BYTES" ]]; then
echo "High bandwidth alert: ${CURRENT_RATE} bytes/sec 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-server-ip>" -n. - 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.