温馨提示×

如何用linux ifconfig测流量

小樊
45
2026-10-16 16:26:45
栏目: 智能运维

在 Linux 里,ifconfig 本身不能实时测流量,但它可以查看网络接口的流量统计信息(累计值),你可以据此手动计算流量。


一、用 ifconfig 看流量统计

执行:

ifconfig eth0

(把 eth0 换成你的网卡名,如 ens33、wlan0)

你会看到类似:

RX packets 123456  bytes 987654321 (987.6 MB)
TX packets 654321  bytes 123456789 (123.4 MB)

含义:

  • RX:接收流量(下载)
  • TX:发送流量(上传)
  • bytes:累计字节数

二、用 ifconfig “测”一段时间内的流量

方法:前后差值计算

1️⃣ 记录开始流量:

ifconfig eth0 | grep bytes

2️⃣ 等一段时间(比如 10 秒)

3️⃣ 再记录一次:

ifconfig eth0 | grep bytes

4️⃣ 手动相减:

流量 = 后 bytes - 前 bytes
速率 = 流量 / 时间

示例:

  • 10 秒内 RX 增加 10MB
  • 速率 ≈ 1 MB/s

三、更简单(推荐)的工具

ifconfig 已逐渐废弃,建议用这些:

1️⃣ ip 命令(替代 ifconfig)

ip -s link show eth0

2️⃣ 实时流量监控

watch -n 1 "ifconfig eth0 | grep bytes"

或更好用:

iftop
nload
vnstat

安装示例(Debian/Ubuntu):

sudo apt install iftop nload vnstat

四、总结

✅ ifconfig 能看累计流量
❌ 不能直接测实时速率
✅ 用「前后差值」可算速率
✅ 推荐 iftop / nload / vnstat

如果你告诉我:

  • 是想测 服务器总流量
  • 还是 某个 IP / 端口
  • 还是 实时网速

我可以给你更精确的方案。

0 踩