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

在 Ubuntu 22.04 上安装 nethogs 0.8.6-3 并逐参数实测按进程带宽监控命令输入与回显,涵盖 -t/-c/-d/-v/-a/-b/-p/多设备、无流量形态、无 TTY 行为与离线安装。

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

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

目标:在 Ubuntu 22.04 上安装 nethogs 0.8.6,并逐参数实测按进程带宽监控命令的输入与输出,涵盖追踪模式(-t)下的 -c、-d、-v、-a、-b、-p、多设备监控,无流量输出形态、无 TTY 行为,以及在线/离线安装。本文命令与输出均为 Ubuntu 22.04(nethogs 0.8.6-3)实测回显。

一、环境信息

  • 操作系统:Ubuntu 22.04 LTS
  • nethogs 版本:0.8.6(Debian/Ubuntu 包 0.8.6-3,nethogs -V → version 0.8.6-3)
  • 网卡: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 循环下载请求制造流量
  • 说明:本环境无 TERM 环境变量;NetHogs 按进程归组带宽,重度依赖 /proc

二、工具概述

NetHogs(Network Hogs)按进程归组带宽(it groups bandwidth by process),而非像 iftop 按主机/连接显示——能直接回答「哪个程序在消耗网卡带宽」。它不依赖内核模块,主要针对 Linux(Since NetHogs heavily relies on /proc, most features are only available on Linux)。本文针对 Ubuntu 22.04 上的 0.8.6-3 实测。

三、安装

3.1 在线安装(apt)

sudo apt update
sudo apt install -y nethogs

💡 Ubuntu 22.04 仓库中的 nethogs 包版本标识为 0.8.6-3。

安装后验证:

nethogs -V
 version 0.8.6-3

3.2 离线安装(.deb 包)

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

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

安装后验证:

nethogs -V
 version 0.8.6-3

追踪模式(-t)输出格式说明:每轮刷新先打印 Refreshing:,随后每行一个「进程/UID/PID」加两列数值(默认 KB/s 口径下的发送、接收)。无连接时打印占位行 unknown TCP/0/0 0 0;连接尚未映射到本地进程时,进程行以连接五元组形式出现(如 172.22.0.2:8000-172.22.0.3:58792/0/0,PID/UID 为 0)。

四、命令实测

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

4.1 基础文本模式 3 轮 —— nethogs -t -c 3 eth0

用途:按进程统计 eth0 收发速率,刷新 3 轮后退出。

参数说明:-t tracemode(文本输出,无需 ncurses);-c 3 刷新 3 轮(帮助原文:number of updates. default is 0 (unlimited))。

nethogs -t -c 3 eth0
Adding local address: 172.22.0.2
Ethernet link detected

Refreshing:
unknown TCP/0/0 0       0

Refreshing:
172.22.0.2:8000-172.22.0.3:58792/0/0    44.5871 0.213086
unknown TCP/0/0 0       0

Refreshing:
172.22.0.2:8000-172.22.0.3:58792/0/0    860.179 2.04746
unknown TCP/0/0 0       0

解读:本视角采集端为 HTTP 服务端(python3,PID 3219),流量为短连接,映射到进程前以连接五元组行显示;客户端视角(CentOS 8 侧同命令)则直接看到 curl/PID/0 行。

4.2 刷新间隔 2 秒 —— nethogs -t -c 3 -d 2 eth0

用途:拉长统计窗口,平滑动抖。

参数说明:-d seconds 帮助原文:delay for update refresh rate in seconds. default is 1。

nethogs -t -c 3 -d 2 eth0
Adding local address: 172.22.0.2
Ethernet link detected

Refreshing:
unknown TCP/0/0 0       0

Refreshing:
172.22.0.2:8000-172.22.0.3:58844/0/0    1437.92 3.49434
unknown TCP/0/0 0       0

Refreshing:
172.22.0.2:8000-172.22.0.3:58844/0/0    3081.61 7.2041
unknown TCP/0/0 0       0

解读:默认 1s 时第 2、3 轮为 821/1605(KB/s 口径);-d 2 后单轮窗口翻倍,数值相应增大(1437/3081),轮次间隔变为 2 秒。

4.3 视图模式 —— nethogs -t -c 3 -v <0|1|2|3> eth0

用途:切换速率/累计两种口径与 KB/B/MB 三种单位。

参数说明:0.8.6 版帮助原文:-v : view mode (0 = KB/s, 1 = total KB, 2 = total B, 3 = total MB, 4 = MB/s, 5 = GB/s). default is 0.。逐个实测(-v 0、-v 1、-v 2、-v 3 各一轮,取每轮最后一行):

nethogs -t -c 3 -v 0 eth0
nethogs -t -c 3 -v 1 eth0
nethogs -t -c 3 -v 2 eth0
nethogs -t -c 3 -v 3 eth0
模式含义(帮助原文)Ubuntu 0.8.6 实测行(服务端视角)
-v 0KB/s(速率,默认)172.22.0.2:8000-172.22.0.3:58980/0/0 1605.29 3.46719
-v 1total KB(累计)172.22.0.2:8000-172.22.0.3:59046/0/0 7442.96 16.3809
-v 2total B(累计字节数)172.22.0.2:8000-172.22.0.3:54792/0/0 7.6166e+06 17806
-v 3total MB(累计)172.22.0.2:8000-172.22.0.3:54878/0/0 7.02006 0.0161295

解读:-v 0 为实时速率;-v 1/2/3 为自 nethogs 启动起的累计量,同一轮次间数值单调增长(如 0.8.6 侧 total B 从 3.41072e+06 增至 7.6166e+06),单位换算与三种模式互相印证。0.8.6 版帮助还列出 -v 4 = MB/s、-v 5 = GB/s(本篇未实测)。

4.4 监控全部设备 —— nethogs -t -c 3 -a eth0

用途:把停止/回环等非常规状态的设备也纳入监控。

参数说明:帮助原文:-a : monitor all devices, even loopback/stopped ones.(注意:-a 作用对象是设备而非协议;同时捕获 TCP+UDP 由 -C 控制。)

nethogs -t -c 3 -a eth0
Adding local address: 172.22.0.2
Ethernet link detected

Refreshing:
unknown TCP/0/0 0       0

Refreshing:
172.22.0.2:8000-172.22.0.3:54932/0/0    821.765 1.75937
unknown TCP/0/0 0       0

Refreshing:
172.22.0.2:8000-172.22.0.3:54932/0/0    1484.63 3.28906
unknown TCP/0/0 0       0

实测结论:本环境未出现 kernel stats 类警告(如实记录);因有效流量都在 eth0 上,输出与基础模式一致。

4.5 排障模式 —— nethogs -t -c 3 -b eth0

用途:调试 nethogs 自身映射问题(会把 /proc/net/tcp 连接表整表转储)。

参数说明:帮助原文:-b : bughunt mode - implies tracemode.(即 -b 自带文本模式,可不依赖终端。)

nethogs -t -c 3 -b eth0
Adding local address: 172.22.0.2
Ethernet link detected
ci:    0: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 37317224 1 ffff8b2f68b16f00 100 0 0 10 0
ci:    1: 00000000:0CEA 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 37317226 1 ffff8b2f68b11bc0 100 0 0 10 0
ci:    2: 0B00007F:879D 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 37216883 2 ffff8b2efceb2500 100 0 0 10 0
……(中略:每轮刷新前转储 /proc/net/tcp 连接表,ci 序号行共数百行,含大量 TIME_WAIT 短连接表项)……

Refreshing:
python3/3219/0  1626.87 3.53633
unknown TCP/0/0 0       0

解读:bughunt 模式每轮先输出 ci: 连接表调试转储再输出进程行(全程 326 行,此处头尾节选);末轮可看到进程已正确映射为 python3/3219/0(HTTP 服务端进程),速率 1626.87 KB/s。

4.6 -m(总 MB 计)—— nethogs -t -c 3 -m eth0(非法选项,如实记录)

实测结果:0.8.6 没有 -m 命令行选项,报 nethogs: invalid option -- 'm' 并回显帮助,RC=1。「按总 MB 显示」实为交互按键 m(见下文帮助原文:switch between total (KB, B, MB) and throughput (KB/s, MB/s, GB/s) mode),不是启动参数。

nethogs -t -c 3 -m eth0
nethogs: invalid option -- 'm'
usage: nethogs [-V] [-h] [-b] [-d seconds] [-v mode] [-c count] [-t] [-p] [-s] [-a] [-l] [-f filter] [-C][device [device [device ...]]]
                -V : prints version.
                ...(回显帮助全文,同下文「帮助输出」)...

4.7 混杂嗅探 —— nethogs -t -c 3 -p eth0

用途:在混杂模式下嗅探(帮助原文提示不推荐:sniff in promiscious mode (not recommended))。

nethogs -t -c 3 -p eth0
Adding local address: 172.22.0.2
Ethernet link detected

Refreshing:
unknown TCP/0/0 0       0

Refreshing:
172.22.0.2:8000-172.22.0.3:55076/0/0    661.035 1.84727
unknown TCP/0/0 0       0

Refreshing:
172.22.0.2:8000-172.22.0.3:55076/0/0    1437.93 3.37832
unknown TCP/0/0 0       0

4.8 多设备监控 —— nethogs -t -c 3 eth0 any

用途:同时监控多个接口;any 为 libpcap 伪设备(Linux 上代表所有接口),实测 any 与 lo 均被接受、无报错,RC=0。

nethogs -t -c 3 eth0 any
Adding local address: 172.22.0.2
Ethernet link detected

Refreshing:
unknown TCP/0/0 0       0

Refreshing:
python3/3219/0  410.741 1.04492
unknown TCP/0/0 0       0

Refreshing:
python3/3219/0  1027.07 2.47285
unknown TCP/0/0 0       0

nethogs -t -c 3 eth0 lo 同样正常(输出结构与基础模式一致,回环口无流量不产生额外行)。帮助原文对设备参数的说明:device : device(s) to monitor. default is all interfaces up and running excluding loopback——即不传设备时默认监控除回环外所有运行中的接口。

4.9 无流量输出形态 —— nethogs -t -c 2 eth0

用途:记录「无连接」时的输出形态(本条目未制造任何流量)。

nethogs -t -c 2 eth0
Adding local address: 172.22.0.2
Ethernet link detected

Refreshing:
unknown TCP/0/0 0       0

Refreshing:
unknown TCP/0/0 0       0

解读:每轮仅打印 Refreshing: 与占位行 unknown TCP/0/0 0 0,无任何进程行;程序正常退出(RC=0)。(0.8.5 版同场景会先多打一行 Waiting for first packet to arrive (see sourceforge.net bug 1019381)。)

4.10 交互模式无 TTY 行为 —— nethogs -c 2 eth0

实测结果:不带 -t 时 nethogs 初始化 ncurses,无终端环境直接报错退出:

nethogs -c 2 eth0
TERM=[]
Error opening terminal: unknown.

例外:-b(bughunt)自带 tracemode,无 TTY 也能正常跑(见上文 4.5 节)。

五、交互模式按键参考

nethogs -h 帮助原文(运行中按键区):

When nethogs is running, press:
 q: quit
 s: sort by SENT traffic
 r: sort by RECEIVE traffic
 l: display command line
 m: switch between total (KB, B, MB) and throughput (KB/s, MB/s, GB/s) mode

README/手册附注(/usr/share/doc/nethogs/README.md 与 nethogs.8 原文要点):NetHogs 按进程归组带宽,不依赖内核模块;重度依赖 /proc,主要功能仅 Linux 可用。老版手册页(2004 年)记载的交互键为 m(循环 kb/s、kb、b、mb 显示模式)、r/s(按收/发排序)、q(退出);现行帮助输出增加了 l 键。

六、帮助输出

6.1 nethogs -h(Ubuntu 22.04,nethogs 0.8.6-3)

usage: nethogs [-V] [-h] [-b] [-d seconds] [-v mode] [-c count] [-t] [-p] [-s] [-a] [-l] [-f filter] [-C][device [device [device ...]]]
                -V : prints version.
                -h : prints this help.
                -b : bughunt mode - implies tracemode.
                -d : delay for update refresh rate in seconds. default is 1.
                -v : view mode (0 = KB/s, 1 = total KB, 2 = total B, 3 = total MB, 4 = MB/s, 5 = GB/s). default is 0.
                -c : number of updates. default is 0 (unlimited).
                -t : tracemode.
                -p : sniff in promiscious mode (not recommended).
                -s : sort output by sent column.
                -l : display command line.
                -a : monitor all devices, even loopback/stopped ones.
                -C : capture TCP and UDP.
                -f : EXPERIMENTAL: specify string pcap filter (like tcpdump). This may be removed or changed in a future version.
                device : device(s) to monitor. default is all interfaces up and running excluding loopback

When nethogs is running, press:
 q: quit
 s: sort by SENT traffic
 r: sort by RECEIVE traffic
 l: display command line
 m: switch between total (KB, B, MB) and throughput (KB/s, MB/s, GB/s) mode

6.2 nethogs -V(版本输出,注意前导空格为原样回显)

 version 0.8.6-3

七、常见问题

7.1 -m 不是命令行选项

Q:nethogs -t -c 3 -m eth0 报 invalid option -- 'm'? A:-m 不是命令行选项,「按总 MB 显示」是运行中的交互按键 m。想用累计口径请用启动参数 -v 1/2/3(total KB/B/MB)。

7.2 服务端视角连接行与进程映射

Q:为什么服务端视角看到的是 IP:端口-IP:端口/0/0 而非进程名? A:短连接流量在映射到本地进程前会先以连接五元组行出现(PID/UID 为 0);-b bughunt 模式会转储 /proc/net/tcp 连接表便于排查映射问题,末轮通常能映射为实际进程(如 python3/3219/0)。

7.3 无终端环境的采集方式

Q:无终端跑 nethogs -c 2 eth0 报 Error opening terminal: unknown.? A:交互界面依赖 ncurses,必须真实 TTY。脚本化/重定向采集需加 -t(追踪模式)或用 -b(自带 tracemode)。

7.4 视图模式 -v 参数的含义

Q:-v 0/1/2/3 数字表示什么含义? A:0=KB/s(实时速率,默认)、1=total KB、2=total B、3=total MB(均为自启动起的累计量)。0.8.6 另支持 4=MB/s、5=GB/s。

八、参考链接

  • NetHogs 官方站点:https://github.com/raboof/nethogs
  • man 手册页:nethogs(8)
  • 本文素材来源:Ubuntu 22.04 + CentOS 8 双系统实测采集(nethogs 0.8.6-3)