iftop Command in Linux

Introduction

Unlock the power of real-time network monitoring with the iftop command in Linux. This tutorial will guide you through installing and utilizing iftop, a crucial tool for any systemadmin. Learn to analyze network traffic and gain insights into your system's bandwidth usage. We'll cover basic installation, usage, and advanced scenarios to maximize your network monitoring capabilities.

This hands-on lab is structured into three key sections:

  1. Understanding the iftop Command
  2. Practical Network Traffic Monitoring with iftop
  3. Advanced Techniques for iftop Usage

Master the iftop command and enhance your ability to monitor and troubleshoot network performance in your Linux environment.

Understanding the iftop Command

Discover iftop, a dynamic network monitoring tool designed for Linux systemadmin. This section introduces iftop, highlighting its real-time network traffic analysis capabilities and its ability to display bandwidth consumption across your system.

First, let's set up iftop on your system:

sudo apt-get update
sudo apt-get install -y iftop

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfprint-2-tod1
Use 'sudo apt autoremove' to remove it.
The following NEW packages will be installed:
  iftop
0 to upgrade, 1 to newly install, 0 to remove and 0 not to upgrade.
Need to get 59.5 kB of archives.
After this operation, 188 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy/universe amd64 iftop amd64 1.0~pre4-5 [59.5 kB]
Fetched 59.5 kB in 0s (0 B/s)
Selecting previously unselected package iftop.
(Reading database ... 123594 files and directories currently installed.)
Preparing to unpack .../iftop_1.0~pre4-5_amd64.deb ...
Unpacking iftop (1.0~pre4-5) ...
Setting up iftop (1.0~pre4-5) ...
Processing triggers for man-db (2.10.2-1) ...

Now, execute iftop to observe network activity:

sudo iftop

Example output:

@@@@ IFTOP 1.0~pre4 @@@@
 192.168.1.100 : 192.168.1.101
                 192.168.1.102
                 192.168.1.103
                 192.168.1.104
                 192.168.1.105
                 192.168.1.106
                 192.168.1.107
                 192.168.1.108
                 192.168.1.109
                 192.168.1.110

            In:  0.00 Kb/s   0.00 Kb/s   0.00 Kb/s
           Out:  0.00 Kb/s   0.00 Kb/s   0.00 Kb/s
         Total:  0.00 Kb/s   0.00 Kb/s   0.00 Kb/s

iftop presents live network data, breaking down bandwidth usage by connection. The display includes source and destination IP addresses, alongside incoming, outgoing, and total bandwidth metrics.

Practical Network Traffic Monitoring with iftop

This section dives into the practical application of iftop for monitoring network traffic on your Linux system. Learn how to use iftop effectively to understand your network's behavior.

Begin by launching iftop in interactive mode:

sudo iftop

This command initiates real-time monitoring of your system's network traffic, showing source and destination IPs alongside bandwidth statistics.

To effectively interpret the output, consider the following key sections:

  • The top section shows active hostnames or IP addresses involved in network communications.
  • "In:" provides incoming bandwidth figures for each connection.
  • "Out:" displays the outgoing bandwidth for each connection.
  • "Total:" aggregates the total bandwidth usage across all connections.

Utilize these interactive keys to control iftop:

  • p: Pause the display to examine data closely
  • t: Switch between cumulative and interval time display modes
  • n: Toggle between showing hostnames or IP addresses
  • i: Cycle through available network interfaces
  • m: Change the display mode to bandwidth, packets, or bits
  • s: Sort connections by source address
  • d: Sort connections by destination address
  • r: Invert the sorting order
  • q: Exit the iftop utility

Example output:

@@@@ IFTOP 1.0~pre4 @@@@
 192.168.1.100 : 192.168.1.101
                 192.168.1.102
                 192.168.1.103
                 192.168.1.104
                 192.168.1.105
                 192.168.1.106
                 192.168.1.107
                 192.168.1.108
                 192.168.1.109
                 192.168.1.110

            In:  0.00 Kb/s   0.00 Kb/s   0.00 Kb/s
           Out:  0.00 Kb/s   0.00 Kb/s   0.00 Kb/s
         Total:  0.00 Kb/s   0.00 Kb/s   0.00 Kb/s

Advanced Techniques for iftop Usage

Elevate your iftop skills with these advanced techniques for specific monitoring scenarios.

  1. Target a specific network interface:
sudo iftop -i eth0

This command narrows the focus to only display traffic flowing through the eth0 interface.

  1. Filter traffic by host or port for focused analysis:
sudo iftop -F 192.168.1.100

This command filters the output to show traffic exclusively to and from the IP address 192.168.1.100.

sudo iftop -p 80

Monitor HTTP traffic specifically by focusing on port 80.

  1. Archive your iftop sessions by saving the output to a file:
sudo iftop -B -f iftop.txt

This command saves the running iftop data to a file named iftop.txt in the current directory.

  1. Customize the output format for better readability:
sudo iftop -o "%-18s %-18s %10.2f %10.2f %10.2f"

Control the displayed columns, showing source, destination, incoming, outgoing, and total bandwidth as defined by the format string.

  1. Visualize iftop data in a web browser for remote monitoring:
sudo iftop -w

Start an iftop web server to view real-time network information in your browser.

Example output:

@@@@ IFTOP 1.0~pre4 @@@@
 192.168.1.100 : 192.168.1.101
                 192.168.1.102
                 192.168.1.103
                 192.168.1.104
                 192.168.1.105
                 192.168.1.106
                 192.168.1.107
                 192.168.1.108
                 192.168.1.109
                 192.168.1.110

            In:  0.00 Kb/s   0.00 Kb/s   0.00 Kb/s
           Out:  0.00 Kb/s   0.00 Kb/s   0.00 Kb/s
         Total:  0.00 Kb/s   0.00 Kb/s   0.00 Kb/s

Summary

This lab provided a comprehensive guide to using iftop, a critical network monitoring tool for Linux. You learned how to install iftop and interpret its real-time bandwidth usage display. We covered basic operation and advanced techniques, including interface targeting and traffic filtering. With these skills, you can effectively monitor and troubleshoot network issues, ensuring optimal performance on your Linux system. The iftop command is essential for any systemadmin responsible for maintaining network health.

400+ Linux Commands