如何在 Linux 下监控每个进程的网络 I/O 使用情况?

Ano*_*ous 33 networking linux io

诸如 iftop/iptraf 之类的已知工具显示每个接口和每个连接的网络 I/O。有没有办法查看每个进程的网络 I/O 统计信息?

mos*_*hen 36

nethogs看起来它会做你想做的。

编辑:我需要安装 ncurses-devel、libpcap 和 libpcap-devel 来构建。


luk*_*yca 5

要查找与每个进程关联的连接,请使用 lsof。例如:

lsof | grep TCP
Run Code Online (Sandbox Code Playgroud)

这将为您提供一个连接列表,如下所示:

bash    10887 luke    3u     IPv4 44638801      0t0      TCP littleyerry.example.com:55212->barista.example.com:ldap (ESTABLISHED)
bash    10913 luke    3u     IPv4 44638905      0t0      TCP littleyerry.example.com:55216->barista.example.com:ldap (ESTABLISHED)
ssh     10935 luke    3u     IPv4 44639001      0t0      TCP littleyerry.example.com:55219->barista.example.com:ldap (ESTABLISHED)
ssh     10935 luke    4u     IPv4 44639008      0t0      TCP littleyerry.example.com:59459->launchpad.example.com:ssh (ESTABLISHED)
bash    10938 luke    3u     IPv4 44639107      0t0      TCP littleyerry.example.com:55221->barista.example.com:ldap (ESTABLISHED)
Run Code Online (Sandbox Code Playgroud)

从那里,您应该能够使用您提到的工具(iftop、iptraf)单独找出每个连接。您可以构建一个小脚本来聚合您要查找的特定数据。

  • `lsof -niTCP` 是等效的,但速度更快,并且 `lsof -niTCP -sTCP:ESTABLISHED` 显示当前连接。 (5认同)