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#

  1. Installation
    1. Debian/Ubuntu
    2. RHEL/CentOS/Fedora
    3. macOS
  2. Basic Usage: Getting Started
    1. Launching iftop
    2. Understanding the iftop Interface
  3. Key Features & Interactive Commands
    1. Interactive Navigation
    2. Packet Filtering with BPF Syntax
    3. Sorting Connections
  4. Advanced Usage
    1. Command-Line Flags for Customization
    2. Saving Output to File
    3. Scripting with iftop
  5. Common & Best Practices
  6. Troubleshooting Scenarios
    1. Identifying Bandwidth Hogs
    2. Investigating Unusual Outbound Traffic
    3. Diagnosing Port-Specific Congestion
  7. Conclusion
  8. 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 iftop

RHEL/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 iftop

macOS#

Use Homebrew to install:

brew update
brew install iftop

Note: 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 iftop

By default, iftop monitors the first active interface (e.g., eth0, wlan0). To specify a different interface:

sudo iftop -i wlan0

Understanding 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:

CommandDescription
qQuit iftop
nToggle hostname resolution (show/hide IP addresses)
NToggle port-to-service-name resolution
pToggle port number visibility
SToggle source port display
DToggle destination port display
sAggregate traffic by source address
dAggregate traffic by destination address
<Sort by source address
>Sort by destination address
1Sort by first column (2-second average)
2Sort by second column (10-second average)
3Sort by third column (40-second average)
fEdit traffic filters (BPF syntax)
lFilter hostnames by POSIX regex
tCycle through display modes (two-line, one-line sent/received/total)
PPause/unpause the display
LToggle logarithmic scale for bar graphs
TShow cumulative totals
j / kScroll display up/down (useful with o to freeze order)
oFreeze current screen order
bToggle bar graph display
hShow 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, or s to aggregate traffic by source.
  • By Destination: Press > to sort by destination address, or d to aggregate traffic by destination.
  • By Column: Press 1, 2, or 3 to sort by the 2-second, 10-second, or 40-second bandwidth column (default is 10-second).

Advanced Usage#

Command-Line Flags for Customization#

FlagDescription
-nDisable DNS resolution (reduce overhead)
-NDisable port-to-service-name resolution
-PShow port numbers by default
-bDisable bar graph display
-BDisplay 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)
-tUse 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 10M

Saving 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.txt

Scripting 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]
fi

Common & Best Practices#

  1. Always Use Root: iftop needs raw packet access—run with sudo.
  2. Disable DNS for Busy Servers: Use -n to avoid overhead from DNS queries.
  3. Filter Strategically: Focus on specific IPs/ports to isolate issues quickly.
  4. Combine with Other Tools: Pair iftop with ss to identify processes behind high-traffic connections:
    sudo ss -tulpn | grep "<port-number>"
  5. Monitor Specific Interfaces: Servers often have multiple interfaces—specify -i to avoid missing critical traffic.
  6. 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:

  1. Run sudo iftop -n -P to see IPs and ports.
  2. Identify the top traffic flow.
  3. Use sudo ss -tulpn to find the associated process.
  4. Verify if the process is legitimate (e.g., backup) or malicious.

2. Investigating Unusual Outbound Traffic#

Problem: Unexpected outbound traffic from your server. Solution:

  1. Filter outbound traffic: sudo iftop -f "src host <your-server-ip>" -n.
  2. Look up suspicious IPs with whois <ip-address>.
  3. Block malicious IPs with iptables or your firewall.

3. Diagnosing Port-Specific Congestion#

Problem: Web server slowdown on port 443. Solution:

  1. Monitor HTTPS traffic: sudo iftop -f "dst port 443".
  2. Identify top clients consuming bandwidth.
  3. Implement rate limiting (e.g., Nginx limit_req module) 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.


References#

  1. iftop Official Documentation
  2. iftop Man Page
  3. BPF Syntax Guide
  4. EPEL Repository Guide
  5. Homebrew for macOS