Ubuntu 22.04 iftop 安装与命令参数详解

在 Ubuntu 22.04 上安装 iftop 1.0pre4 并逐参数实测带宽监控命令输入与回显,涵盖 -t/-s/-n/-N/-B/-P/-F/-f/-m/-p/-b/-l/-c 文本模式、交互键位与离线安装。

Linux 网络诊断与调试iftopLinux命令行网络诊断2026/10/06

Ubuntu 22.04 iftop 安装与命令参数详解

目标:在 Ubuntu 22.04 上安装 iftop 1.0pre4,并逐参数实测带宽监控命令的输入与输出,涵盖文本模式(-t)下的 -s、-n、-N、-B、-P、-F、-f、-m、-p、-b、-l、-c,交互模式按键参考,以及在线/离线安装。本文命令与输出均为 Ubuntu 22.04(iftop 1.0pre4,Debian 包 1.0~pre4-7)实测回显。

一、环境信息

  • 操作系统:Ubuntu 22.04 LTS
  • iftop 版本:1.0pre4(Debian/Ubuntu 包 iftop 1.0~pre4-7 amd64)
  • 网卡:eth0,IP 172.22.0.2/16,MAC 72:18:8c:51:b4:75
  • 对端主机:172.22.0.3(CentOS 8,同属 172.22.0.0/16)
  • 采集视角:本机为 HTTP 服务端(监听 8000 端口,python3 进程 PID 3219),采集期间用 curl 循环下载请求制造约 4~32Mb/s 短连接流量
  • 说明:本环境无 TERM 环境变量;/etc/iftoprc 不存在

二、工具概述

iftop 实时显示指定接口的带宽用量并按主机对聚合统计,是排查「谁在占用带宽」「本机连接了哪些主机」的常用命令。它基于 libpcap 抓包,依赖 ncurses 实现交互界面;无终端环境则必须使用 -t 文本模式输出纯文本统计。本文针对 Ubuntu 22.04 上的 1.0pre4 实测。

三、安装

3.1 在线安装(apt)

sudo apt update
sudo apt install -y iftop

💡 Ubuntu 22.04 仓库中的 iftop 包版本标识为 1.0~pre4-7,二进制运行版本为 1.0pre4(见 iftop -h 尾部)。

安装后验证:

dpkg -l iftop
ii  iftop  1.0~pre4-7  amd64  displays bandwidth usage information on an network interface

3.2 离线安装(.deb 包)

# 在可联网的 Ubuntu 机器上下载 iftop 及其依赖的 .deb 包
cd /opt/netdiag-offline
apt-get download iftop libpcap0.8 libncurses6

# 拷贝到离线机器后一键安装
dpkg -i /opt/netdiag-offline/*.deb

安装后验证:

iftop -h 2>&1 | tail -3
iftop, version 1.0pre4
copyright (c) 2002 Paul Warren <pdw@ex-parrot.com> and contributors

💡 iftop 1.0pre4 没有 -v/-V 选项,调用会报 iftop: unknown option -v 并回显帮助(RC=1),因此版本号需通过 iftop -h 尾部自带的 iftop, version 1.0pre4 或包管理器(dpkg -l iftop)获取。

测试期间每条实测命令的运行环境:有对 8000 端口的 HTTP 下载流量(1MB 文件循环拉取,短连接为主),除标注「无流量」的条目外均有流量。

四、命令实测

4.1 基础文本模式 4 秒采样 —— iftop -t -s 4 -n -N -i eth0

用途:以纯文本形式输出一屏带宽统计后退出,适合脚本/无终端环境批量采集。

参数说明:-t 不使用 ncurses、结果打印到 STDOUT;-s 4 运行 4 秒后输出一次并退出(帮助原文:print one single text output afer num seconds, then quit);-n 不做主机名反查;-N 端口不解析为服务名;-i eth0 指定接口。

iftop -t -s 4 -n -N -i eth0
interface: eth0
IP address is: 172.22.0.2
MAC address is: 72:18:8c:51:b4:75
Listening on eth0
   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
   1 172.22.0.2                               =>     32.1Mb     22.1Mb     22.1Mb     11.0MB
     172.22.0.3                               <=     63.0Kb     39.8Kb     39.8Kb     19.9KB
--------------------------------------------------------------------------------------------
Total send rate:                                     32.1Mb     22.1Mb     22.1Mb
Total receive rate:                                  63.0Kb     39.8Kb     39.8Kb
Total send and receive rate:                         32.1Mb     22.1Mb     22.1Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     32.1Mb     63.0Kb     32.1Mb
Cumulative (sent/received/total):                    11.0MB     19.9KB     11.0MB
============================================================================================

解读:=> 为发送方向、<= 为接收方向;主机行按主机对聚合,给出 last 2s/10s/40s 三列速率与累计量;底部为总计/峰值/累计三组。本例本机(172.22.0.2,HTTP 服务端)发送 32.1Mb/s,对端(172.22.0.3,客户端)仅有约 63Kb/s 的请求上行。

4.2 按字节显示 —— iftop -t -s 4 -n -N -B -i eth0

用途:把速率/累计单位由 bit 换成 byte,便于与传输字节数对照。

参数说明:-B 显示带宽以字节计(帮助原文:Display bandwidth in bytes);man 手册亦说明可配合 -u bits|bytes|packets 选择单位(本环境二进制仅实现帮助所列参数)。

iftop -t -s 4 -n -N -B -i eth0
interface: eth0
IP address is: 172.22.0.2
MAC address is: 72:18:8c:51:b4:75
Listening on eth0
   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
   1 172.22.0.2                               =>     4.01MB     3.01MB     3.01MB     12.0MB
     172.22.0.3                               <=     7.62KB     5.77KB     5.77KB     23.1KB
--------------------------------------------------------------------------------------------
Total send rate:                                     4.01MB     3.01MB     3.01MB
Total receive rate:                                  7.62KB     5.77KB     5.77KB
Total send and receive rate:                         4.02MB     3.01MB     3.01MB
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     4.01MB     7.62KB     4.02MB
Cumulative (sent/received/total):                    12.0MB     23.1KB     12.1MB
============================================================================================

解读:同一流量在基础模式下为 32.1Mb/s,此处为 4.01MB/s,单位即 8 倍关系。

4.3 显示端口明细 —— iftop -t -s 4 -n -N -P -i eth0

用途:把聚合格式细化为「主机:端口」逐连接行,定位具体业务端口。

参数说明:-P 连同端口一起显示(帮助原文:show ports as well as hosts)。

iftop -t -s 4 -n -N -P -i eth0
interface: eth0
IP address is: 172.22.0.2
MAC address is: 72:18:8c:51:b4:75
Listening on eth0
   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
   1 172.22.0.2:8000                          =>     4.02Mb     2.01Mb     2.01Mb     1.00MB
     172.22.0.3:59710                         <=     5.16Kb     2.58Kb     2.58Kb     1.29KB
   2 172.22.0.2:8000                          =>         0b     2.01Mb     2.01Mb     1.00MB
     172.22.0.3:56358                         <=         0b     3.67Kb     3.67Kb     1.84KB
   3 172.22.0.2:8000                          =>     4.01Mb     2.01Mb     2.01Mb     1.00MB
     172.22.0.3:59680                         <=     7.96Kb     3.98Kb     3.98Kb     1.99KB
   4 172.22.0.2:8000                          =>         0b     2.00Mb     2.00Mb     1.00MB
     172.22.0.3:56344                         <=         0b     4.46Kb     4.46Kb     2.23KB
   5 172.22.0.2:8000                          =>     4.01Mb     2.00Mb     2.00Mb     1.00MB
     172.22.0.3:59696                         <=     8.72Kb     4.36Kb     4.36Kb     2.18KB
   6 172.22.0.2:8000                          =>         0b     2.01Mb     2.01Mb     1.00MB
     172.22.0.3:56350                         <=         0b     2.56Kb     2.56Kb     1.28KB
   7 172.22.0.2:8000                          =>     4.01Mb     2.00Mb     2.00Mb     1.00MB
     172.22.0.3:59654                         <=     8.32Kb     4.16Kb     4.16Kb     2.08KB
   8 172.22.0.2:8000                          =>     4.01Mb     2.00Mb     2.00Mb     1.00MB
     172.22.0.3:59698                         <=     8.52Kb     4.26Kb     4.26Kb     2.13KB
   9 172.22.0.2:8000                          =>     4.01Mb     2.01Mb     2.01Mb     1.00MB
     172.22.0.3:59664                         <=     6.33Kb     3.17Kb     3.17Kb     1.58KB
  10 172.22.0.2:8000                          =>     4.01Mb     2.00Mb     2.00Mb     1.00MB
     172.22.0.3:59666                         <=     8.32Kb     4.16Kb     4.16Kb     2.08KB
--------------------------------------------------------------------------------------------
Total send rate:                                     32.1Mb     24.1Mb     24.1Mb
Total receive rate:                                  61.2Kb     45.0Kb     45.0Kb
Total send and receive rate:                         32.1Mb     24.1Mb     24.1Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     32.1Mb     61.2Kb     32.1Mb
Cumulative (sent/received/total):                    12.0MB     22.5KB     12.1MB
============================================================================================

解读:HTTP 客户端为短连接,每次请求换一个临时端口(56344~59710),故每个端口对一行、各传输约 1MB;本轮输出显示前 10 行(文本模式的行数可用 -L num 控制,帮助原文:number of lines to print)。

4.4 按网段边界过滤 —— iftop -t -s 4 -n -N -F 172.22.0.0/16 -i eth0

用途:只统计「进出」指定网段的流量,并以网段边界判定收发方向。

参数说明:-F net/mask 帮助原文:show traffic flows in/out of IPv4 network;man 手册原文:iftop will only include packets flowing in to or out of the given network, and packet direction is determined relative to the network boundary。掩码可写前缀长度(/16、/24)或点分形式(/255.255.0.0),man 均有说明。

iftop -t -s 4 -n -N -F 172.22.0.0/16 -i eth0
interface: eth0
IP address is: 172.22.0.2
MAC address is: 72:18:8c:51:b4:75
Listening on eth0
   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
Total send rate:                                         0b         0b         0b
Total receive rate:                                      0b         0b         0b
Total send and receive rate:                             0b         0b         0b
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                         0b         0b         0b
Cumulative (sent/received/total):                        0B         0B         0B
============================================================================================

重要实测结论:两端主机(172.22.0.2 与 172.22.0.3)同在 172.22.0.0/16 内,流量未跨越网段边界,因此输出为空。改用只覆盖单侧主机的 /32 网段后流量立即出现:

iftop -t -s 4 -n -N -F 172.22.0.3/32 -i eth0
   1 172.22.0.3                               =>     52.5Kb     37.5Kb     37.5Kb     18.8KB
     172.22.0.2                               <=     32.1Mb     24.1Mb     24.1Mb     12.0MB
--------------------------------------------------------------------------------------------
Total send rate:                                     52.5Kb     37.5Kb     37.5Kb
Total receive rate:                                  32.1Mb     24.1Mb     24.1Mb
Total send and receive rate:                         32.1Mb     24.1Mb     24.1Mb

(-F 172.22.0.2/32 同样有输出,方向随之对调;-F 172.22.0.0/255.255.0.0 与 /16 一样为空,说明两种掩码写法均被正确解析、空输出的原因是网段内互访不跨越边界。)

4.5 pcap 过滤表达式 —— iftop -t -s 4 -n -N -f 'port 8000' -i eth0

用途:用 tcpdump 风格的 BPF 表达式只统计指定流量。

参数说明:-f filter code 帮助原文:use filter code to select packets to count;man 补充:Only IP packets are ever counted, so the specified code is evaluated as (filter code) and ip。

iftop -t -s 4 -n -N -f 'port 8000' -i eth0
   1 172.22.0.2                               =>     32.1Mb     24.1Mb     24.1Mb     12.0MB
     172.22.0.3                               <=     51.8Kb     41.4Kb     41.4Kb     20.7KB
--------------------------------------------------------------------------------------------
Total send rate:                                     32.1Mb     24.1Mb     24.1Mb
Total receive rate:                                  51.8Kb     41.4Kb     41.4Kb
Total send and receive rate:                         32.2Mb     24.1Mb     24.1Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     32.1Mb     51.8Kb     32.2Mb
Cumulative (sent/received/total):                    12.0MB     20.7KB     12.1MB
============================================================================================

解读:port 8000 精确命中 HTTP 下载流量,收发两个方向(源端口或目的端口为 8000)都被计入,主机对聚合成一行。

4.6 固定流量刻度上限 —— iftop -t -s 4 -n -N -m 1M -i eth0

用途:把条形图刻度的上限固定住,避免刻度自适应跳动。

参数说明:-m limit 帮助原文:sets the upper limit for the bandwidth scale;man:Specified as a number with a 'K', 'M' or 'G' suffix(也接受纯数字,实测 -m 1000000 与 -m 1M 行为一致)。

iftop -t -s 4 -n -N -m 1M -i eth0
   1 172.22.0.2                               =>     32.1Mb     24.1Mb     24.1Mb     12.0MB
     172.22.0.3                               <=     56.6Kb     44.6Kb     44.6Kb     22.3KB
--------------------------------------------------------------------------------------------
Total send rate:                                     32.1Mb     24.1Mb     24.1Mb
Total receive rate:                                  56.6Kb     44.6Kb     44.6Kb
Total send and receive rate:                         32.1Mb     24.1Mb     24.1Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     32.1Mb     56.6Kb     32.1Mb
Cumulative (sent/received/total):                    12.0MB     22.3KB     12.1MB
============================================================================================

实测结论:文本模式不输出条形图,-m 仅作用于图形界面的刻度,故文本输出与基础模式无可见差异(两种写法均被接受、无报错)。

4.7 混杂模式 —— iftop -t -s 4 -n -N -p -i eth0

用途:统计不经过本机接口直连的第三方流量(如同网段其他主机间的通信)。

参数说明:-p 帮助原文:run in promiscuous mode (show traffic between other hosts on the same network segment)。需要 root/CAP_NET_RAW,本环境为特权模式可用。

iftop -t -s 4 -n -N -p -i eth0
   1 172.22.0.2                               =>     32.1Mb     24.1Mb     24.1Mb     12.0MB
     172.22.0.3                               <=     58.0Kb     44.1Kb     44.1Kb     22.1KB
--------------------------------------------------------------------------------------------
Total send rate:                                     32.1Mb     24.1Mb     24.1Mb
Total receive rate:                                  58.0Kb     44.1Kb     44.1Kb
Total send and receive rate:                         32.1Mb     24.1Mb     24.1Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     32.1Mb     58.0Kb     32.1Mb
Cumulative (sent/received/total):                    12.0MB     22.1KB     12.1MB
============================================================================================

解读:本例没有第三方流量可嗅,输出与基础模式一致;man 的 QUIRKS 节说明:混杂模式下若不指定 -F,第三方流量的方向判定是任意的,合计中一律按入向计账(There is no easy way to assign the direction of traffic between two third parties… all traffic between other hosts is accounted as incoming)。

4.8 关闭条形图 —— iftop -t -s 4 -n -N -b -i eth0

用途:图形界面下不画条形图。

参数说明:-b 帮助原文:don't display a bar graph of traffic。文本模式本就无条形图,实测输出与基础模式一致(如实记录,无可见差异)。

iftop -t -s 4 -n -N -b -i eth0
   1 172.22.0.2                               =>     32.1Mb     24.1Mb     24.1Mb     12.0MB
     172.22.0.3                               <=     49.8Kb     40.0Kb     40.0Kb     20.0KB
--------------------------------------------------------------------------------------------
Total send rate:                                     32.1Mb     24.1Mb     24.1Mb
Total receive rate:                                  49.8Kb     40.0Kb     40.0Kb
Total send and receive rate:                         32.1Mb     24.1Mb     24.1Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     32.1Mb     49.8Kb     32.1Mb
Cumulative (sent/received/total):                    12.0MB     20.0KB     12.1MB
============================================================================================

4.9 IPv6 模式 —— iftop -t -s 4 -6 -n -N -i eth0(本版本不支持,如实记录)

用途(预期):只统计 IPv6 流量。

实测结果:iftop 1.0pre4 没有 -6 选项,报错并回显帮助,RC=1:

iftop -t -s 4 -6 -n -N -i eth0
iftop: unknown option -6
iftop: display bandwidth usage on an interface by host

Synopsis: iftop -h | [-npblNBP] [-i interface] [-f filter code]
                               [-F net/mask] [-G net6/mask6]

   -h                  display this message
   ...(帮助全文同下文「帮助输出」)...

替代能力(帮助原文):IPv6 网段过滤由 -G net6/mask6(show traffic flows in/out of IPv6 network)提供,链路本地 IPv6 由 -l(display and count link-local IPv6 traffic (default: off))提供。实测 iftop -t -s 4 -n -N -l -i eth0 可正常运行,输出结构与基础模式一致(本环境 eth0 无 inet6 地址、无 v6 流量,无额外行):

   1 172.22.0.2                               =>     32.1Mb     16.0Mb     16.0Mb     8.02MB
     172.22.0.3                               <=     60.6Kb     30.3Kb     30.3Kb     15.2KB
--------------------------------------------------------------------------------------------
Total send rate:                                     32.1Mb     16.0Mb     16.0Mb
Total receive rate:                                  60.6Kb     30.3Kb     30.3Kb
Total send and receive rate:                         32.1Mb     16.1Mb     16.1Mb

4.10 指定配置文件 —— iftop -c /dev/null -t -s 4 -n -N -i eth0

用途:用自定义配置文件驱动 iftop;传空文件则全部走默认值。

参数说明:-c config file 帮助原文:specifies an alternative configuration file。man 说明:不指定 -c 时依次尝试 ~/.iftoprc;本环境没有 /etc/iftoprc(实测 ls: cannot access '/etc/iftoprc': No such file or directory)。

iftop -c /dev/null -t -s 4 -n -N -i eth0
   1 172.22.0.2                               =>     32.1Mb     24.1Mb     24.1Mb     12.0MB
     172.22.0.3                               <=     62.6Kb     47.3Kb     47.3Kb     23.7KB
--------------------------------------------------------------------------------------------
Total send rate:                                     32.1Mb     24.1Mb     24.1Mb
Total receive rate:                                  62.6Kb     47.3Kb     47.3Kb
Total send and receive rate:                         32.1Mb     24.1Mb     24.1Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     32.1Mb     62.6Kb     32.1Mb
Cumulative (sent/received/total):                    12.0MB     23.7KB     12.1MB
============================================================================================

解读:/dev/null 为空配置,输出与基础模式一致。man 还给出配置指令示例(interface: eth0、port-display: (off|source-only|destination-only|on)、max-bandwidth:、net-filter: 等,命令行参数优先级高于配置文件)。

4.11 交互模式无 TTY 行为 —— iftop -n -N

用途(预期):进入全屏交互界面。

实测结果:不带 -t 时 iftop 尝试初始化 ncurses,无终端环境直接失败退出(先输出头部三行,随后报错),RC=1:

iftop -n -N
TERM=[]
interface: eth0
IP address is: 172.22.0.2
MAC address is: 72:18:8c:51:b4:75
Error opening terminal: unknown.

补充实测:显式指定 TERM=xterm 后 ncurses 不再立即报错,而是把控制序列写到无终端的输出流——得到乱码帧(刻度行、由 q 组成的条形区、TX/RX/TOTAL 汇总行),且程序持续运行需外部终止(35 秒超时终止):

                12.5Kb          25.0Kb          37.5Kb          50.0Kb    62.5Kb
mqvqvqvqvqq

qq
TX:             cum:      0B    peak:      0b   rates:      0b      0b      0b
RX:                       0B               0b               0b      0b      0b
TOTAL:                    0B               0b               0b      0b      0b

结论:交互界面需要真实终端(TTY)支撑;无终端采集必须用 -t。

4.12 交互模式按键参考

来源:iftop 二进制内嵌帮助屏(运行中按 H 或 ? 呼出,以下为从二进制提取的帮助原文)+ man 手册页(本文摘录取自 CentOS 8 环境随附的 iftop.8,Ubuntu 侧最小化安装 man 页被移除)。

内嵌帮助屏原文(逐行):

Press H or ? for help
 n - toggle DNS host resolution         P - pause display
 s - toggle show source host            h - toggle this help display
 d - toggle show destination host        b - toggle bar graph display
 t - cycle line display mode             B - cycle bar graph average
                                        T - toggle cumulative line totals
Port display:                           j/k - scroll display
 N - toggle service resolution          f - edit filter code
 S - toggle show source port            l - set screen filter
 D - toggle show destination port       L - lin/log scales
 p - toggle port display                ! - shell command
                                        q - quit
 1/2/3 - sort by 1st/2nd/3rd column
 < - sort by source name
 > - sort by dest name
 o - freeze current order

man 手册补充(按键的扩展说明):

  • s / d:把所有流量按源/目的聚合显示,在网关或混杂模式下最有用(man SOURCE/DEST AGGREGATION 节)。
  • S / D / p:分别切换源端口、目的端口、端口整体显示。
  • t:在四种行显示模式间循环——默认双行(发送/接收分行)与三种单行模式(发送、接收、总量)。
  • 1 / 2 / 3:按第 1/2/3 列(2s/10s/40s 平均值)排序;< / >:按源/目的主机名排序。
  • l:输入 POSIX 扩展正则做屏幕过滤——只影响显示,不影响捕获和底部合计(Display filters DO NOT affect the totals at the bottom of the screen)。
  • P:暂停刷新;o:冻结当前屏幕顺序,副作用是冻结后新出现的主机对不再显示(但计入合计)。
  • j / k:滚动主机列表,配合冻结顺序使用最佳。
  • f:运行中编辑 BPF 过滤代码(man 提示可能有意料之外的行为)。
  • 按键操作后底部有状态回显(二进制内嵌字符串实测):Port display ON/OFF/DEST/SOURCE、Display unpaused/paused、Sort by col 1/2/3、Sort by source/dest、Invalid regexp。

README 附注(/usr/share/doc/iftop/README 原文要点):iftop must be run as root;ncurses/libpcap 版本过旧会导致崩溃或无法编译。

五、帮助输出

5.1 iftop -h(帮助全文,Ubuntu 22.04 / Debian 版 1.0~pre4-7)

iftop: display bandwidth usage on an interface by host

Synopsis: iftop -h | [-npblNBP] [-i interface] [-f filter code]
                               [-F net/mask] [-G net6/mask6]

   -h                  display this message
   -n                  don't do hostname lookups
   -N                  don't convert port numbers to services
   -p                  run in promiscuous mode (show traffic between other
                       hosts on the same network segment)
   -b                  don't display a bar graph of traffic
   -B                  Display bandwidth in bytes
   -i interface        listen on named interface
   -f filter code      use filter code to select packets to count
                       (default: none, but only IP packets are counted)
   -F net/mask         show traffic flows in/out of IPv4 network
   -G net6/mask6       show traffic flows in/out of IPv6 network
   -l                  display and count link-local IPv6 traffic (default: off)
   -P                  show ports as well as hosts
   -m limit            sets the upper limit for the bandwidth scale
   -c config file      specifies an alternative configuration file
   -t                  use text interface without ncurses

   Sorting orders:
   -o 2s                Sort by first column (2s traffic average)
   -o 10s               Sort by second column (10s traffic average) [default]
   -o 40s               Sort by third column (40s traffic average)
   -o source            Sort by source address
   -o destination       Sort by destination address

   The following options are only available in combination with -t
   -s num              print one single text output afer num seconds, then quit
   -L num              number of lines to print

iftop, version 1.0pre4
copyright (c) 2002 Paul Warren <pdw@ex-parrot.com> and contributors

5.2 iftop -v / iftop -V(版本查询,不被支持)

iftop 1.0pre4 没有 -v/-V 选项,两者均报 iftop: unknown option -v(-V 同理)并回显帮助,RC=1。版本号的正确获取方式是 iftop -h 尾部自带的 iftop, version 1.0pre4,或包管理器:dpkg -l iftop → ii iftop 1.0~pre4-7 amd64 displays bandwidth usage information on an network interface。

iftop: unknown option -v
iftop: display bandwidth usage on an interface by host
...(回显帮助全文,同上)...

六、常见问题

6.1 网段过滤无输出的原因

Q:为什么 -F 172.22.0.0/16 过滤时完全没有输出? A:iftop 的 -F 以网段边界判定收发方向,只统计进出该网段的流量。当两端主机同属 172.22.0.0/16 时流量未跨越边界,故为空;改用只覆盖单侧主机的 /32(或 /255.255.0.0)即可看到流量。

6.2 无终端环境的采集方式

Q:无终端环境直接跑 iftop -n -N 报 Error opening terminal: unknown.? A:交互界面依赖 ncurses,必须有真实 TTY。脚本化/重定向采集一律加 -t(文本模式);显式设 TERM=xterm 只会在无终端流出控制序列乱码帧,程序仍无法正常显示。

6.3 版本号的正确获取方式

Q:iftop -v 想查版本为什么会报错? A:iftop 1.0pre4 没有 -v/-V 选项,须用 iftop -h 尾部的 iftop, version 1.0pre4 或包管理器(dpkg -l iftop / rpm -q iftop)获取版本号。

七、参考链接

  • iftop 官方站点:https://www.ex-parrot.com/pdw/iftop/
  • man 手册页:iftop(8)(Linux man-pages,随发行版安装)
  • 本文素材来源:Ubuntu 22.04 + CentOS 8 双系统实测采集(iftop 1.0pre4)