nethogs / iftop —— 按进程或按连接看实时带宽

运维工具手册 02-进程与网络诊断 4.2 KB 原文

nethogs / iftop —— 按进程或按连接看实时带宽

分类:02 进程与网络诊断 适用:Linux(nethogs 依赖 libpcap;iftop 依赖 libpcap + ncurses) 一句话:带宽被打满时,nethogs 找进程,iftop 找对端。

1. 两者怎么选

工具 维度 适合回答
nethogs 按进程 是哪个程序在跑流量
iftop 按连接/对端 IP 流量跑到哪台机器去了

补充:ss -tnp 只能看连接数,看不到速率;iptables/nftables 统计能看总量但不能实时分进程。

2. 安装

apt install -y nethogs iftop
dnf install -y nethogs iftop        # nethogs 可能需 EPEL
apk add nethogs iftop               # iftop 在 testing 源

3. nethogs 用法

nethogs                  # 默认 eth0,1 秒刷新
nethogs eth0             # 指定网卡
nethogs eth0 eth1        # 多网卡
nethogs -d 2             # 2 秒刷新
nethogs -v 3             # 显示模式:0=KB/s 1=总KB 2=总B 3=总MB
nethogs -t               # trace 模式,追加输出,适合日志采集
nethogs -C               # 只显示流量(不显示总累计)
nethogs -a               # 监控所有网卡
nethogs -p               # 混杂模式(默认开启,-p 显式)

输出

Refreshing every: 2s
PID   USER    PROGRAM                                  DEV    SENT     RECEIVED
1234  root    /usr/sbin/nginx                          eth0  12.340    45.670 KB/sec
5678  app     /usr/bin/java -jar app.jar               eth0   0.120   890.450 KB/sec
?     root    unknown TCP                                        0.000     0.000 KB/sec
列 含义
PID 进程号,? 表示无法归属(短连接已退出、内核流量)
USER 运行用户
PROGRAM 程序路径与参数
DEV 网卡
SENT 发送速率
RECEIVED 接收速率

交互键

键 作用
m 切换单位(KB/s、KB、B、MB)
s / r 按发送 / 接收排序
q 退出

-t 模式输出示例(便于落盘做趋势):

nethogs -t -d 5 >> /var/log/nethogs.log 2>&1 &

4. iftop 用法

iftop                    # 默认第一个网卡
iftop -i eth0
iftop -i eth0 -n         # 不解析主机名(必加)
iftop -i eth0 -N         # 不解析端口名
iftop -i eth0 -P         # 显示端口
iftop -i eth0 -B         # 单位 bytes(默认 bits)
iftop -i eth0 -F 10.0.0.0/24    # 只看该网段
iftop -i eth0 -f 'port 80'      # pcap 过滤器
iftop -i eth0 -m 100M    # 设置带宽刻度上限

输出

                 10.0.0.5:443  <=>  10.0.0.9:51234    1.23Mb  2.45Mb  1.98Mb
                 10.0.0.5:3306 <=>  10.0.0.7:45210    0.45Mb  0.12Mb  0.30Mb
───────────────
TX:             cum:  12.3MB   peak:  3.45Mb    rates:  1.20Mb  0.80Mb  1.05Mb
RX:                    45.6MB          8.90Mb            4.50Mb  3.20Mb  3.80Mb
TOTAL:                 57.9MB         12.30Mb            5.70Mb  4.00Mb  4.85Mb

三列分别是 2s、10s、40s 滑动平均。底部 cum 是累计、peak 是峰值。

交互键

键 作用
t 切换显示模式:两行 / 一行 / 只收 / 只发
n 开关 DNS 解析
s / d 显示源 / 目标主机
p 显示端口
P 暂停刷新
j / k 上下滚动
l 过滤(输入后回车)
L 切换刻度
b 显示平均流量条
B 切换 bits/bytes
T 显示累计总量
q 退出

5. 实战场景

5.1 带宽打满,先找进程

nethogs -d 1
# 等 5 秒让数值稳定,按 s 按发送排序

常见元凶:日志外发、备份同步、镜像拉取、被入侵后的外联。

5.2 再找对端

iftop -i eth0 -n -P
# 看是哪个 IP 吃满带宽,再判断是否内网正常流量

5.3 确认是否被 DDoS / 挖矿外联

iftop -i eth0 -n -P -f 'not port 22 and not port 53'
# 出现陌生外网 IP 且持续高流量 → 进一步查
ss -tnp | grep <可疑IP>
ps -p <PID> -o lstart,args

5.4 长期记录(nethogs trace 模式)

nohup nethogs -t -d 10 > /var/log/nethogs_$(date +%F).log 2>&1 &
# 输出是纯文本,可被 filebeat/awk 采集

5.5 容器/云主机的局限

# 容器内 nethogs 通常无效,改在宿主机抓 veth
ip -br link | grep veth
nethogs veth1234
# 或按容器 cgroup 看
cat /sys/fs/cgroup/system.slice/docker-<id>.scope/network.* 2>/dev/null

6. 常见坑

  1. 需要 root 或 CAP_NET_ADMIN,否则只能看到部分流量或直接报错。
  2. nethogs 的 ? 行很常见:短连接进程已退出,流量无处归属,占比高说明是短连接密集型业务。
  3. 不统计非 TCP/UDP:ICMP、ARP 不计入。
  4. 容器内基本不可用:PID/网络 namespace 隔离,需在宿主机对 veth 抓。
  5. iftop 默认单位是 bits:看到 100Mb 是 100 Mbit/s ≈ 12.5 MB/s,别误判。
  6. iftop 的 40s 平均列:判断持续带宽要看最长那列,短列容易误读瞬时尖峰。
  7. nethogs 对 SNAT 后的流量归属不准:内核视角是连接,不是进程。
  8. 两者都有性能开销:高 PPS 场景会加重负载,短时使用。
  9. VPN/隧道流量归属:nethogs 会把流量算在隧道进程(如 openvpn、frpc)上,看不到内层真实进程。

7. 速查表

# nethogs
nethogs -d 1                    # 每秒刷新
nethogs -v 3                    # MB 累计模式
nethogs -t -d 10 > log &        # trace 落盘
nethogs eth0 eth1               # 多网卡
# 交互:m 切单位 / s 按发送 / r 按接收 / q 退出

# iftop
iftop -i eth0 -n -P             # 最常用
iftop -i eth0 -n -B             # bytes 单位
iftop -i eth0 -F 10.0.0.0/24    # 网段过滤
iftop -i eth0 -f 'port 3306'    # 端口过滤
# 交互:t 模式 / n DNS / p 端口 / l 过滤 / q 退出
nethogs / iftop —— 按进程或按连接看实时带宽 - 知识库 - 起航社区