iftop 命令
基本介绍
iftop(Interface Top)是 Linux 系统中用于实时监控网络带宽使用情况的工具,类似于 top 命令但专门用于网络监控。它可以显示每个连接的网络流量,按带宽使用量排序,帮助系统管理员定位网络带宽消耗大户和网络瓶颈。
资料合集:https://pan.quark.cn/s/6fe3007c3e95、https://pan.quark.cn/s/561de99256a5、https://pan.quark.cn/s/985f55b13d94、https://pan.quark.cn/s/d0fb20abd19a
语法
iftop [OPTIONS] [INTERFACE]
常用选项
基本选项
-i, --interface=INTERFACE:指定要监控的网络接口-f, --filter=FILTER:使用 BPF 过滤器过滤流量-p, --promiscuous:将接口设置为混杂模式-B, --bytes:以字节为单位显示带宽-K, --kilobits:以千比特为单位显示带宽(默认)-m, --max-bandwidth=BW:设置显示的最大带宽
输出选项
-n, --no-dns:不解析主机名-N, --numeric:不解析端口号为服务名-P, --show-ports:显示端口号-b, --no-bar:不显示条形图-t, --text:文本模式,非交互式输出-s, --print=NUM:输出 NUM 次后退出
其他选项
-h, --help:显示帮助信息-v, --version:显示版本信息
使用示例
1. 基本使用
bash
iftop
输出示例:
Interface: eth0
IP address is: 192.168.1.100
MAC address is: 00:11:22:33:44:55
# Host name (port/service if enabled) last 2s last 10s last 40s cumulative
--------------------------------------------------------------------------------------------
1 192.168.1.200:80 100Kb/s 95Kb/s 90Kb/s 1.2MB
=> 192.168.1.100:45678 100Kb/s 95Kb/s 90Kb/s 1.2MB
2 192.168.1.1:53 50Kb/s 48Kb/s 45Kb/s 600KB
=> 192.168.1.100:5353 50Kb/s 48Kb/s 45Kb/s 600KB
--------------------------------------------------------------------------------------------
Total send rate: 150Kb/s
Total receive rate: 0b/s
Total send and receive rate: 150Kb/s
2. 指定网络接口
bash
iftop -i eth0
iftop -i wlan0
3. 不解析主机名
bash
iftop -n
4. 显示端口号
bash
iftop -P
5. 以字节为单位显示
bash
iftop -B
6. 使用过滤器
bash
# 只显示 HTTP 流量
iftop -f "tcp and port 80"
# 只显示特定 IP 的流量
iftop -f "host 192.168.1.200"
# 只显示出站流量
iftop -f "src host 192.168.1.100"
7. 文本模式输出
bash
iftop -t
iftop -t -s 5
8. 设置最大带宽显示
bash
iftop -m 100M
输出格式说明
主要字段说明
| 字段 | 说明 |
|---|---|
# |
排名 |
Host name |
主机名或 IP 地址 |
port/service |
端口号或服务名 |
last 2s |
过去 2 秒的平均带宽 |
last 10s |
过去 10 秒的平均带宽 |
last 40s |
过去 40 秒的平均带宽 |
cumulative |
累积流量 |
箭头含义
| 符号 | 含义 |
|---|---|
=> |
出站流量(发送) |
<= |
入站流量(接收) |
交互命令
在 iftop 运行时,可以使用以下交互命令:
| 命令 | 说明 |
|---|---|
h |
显示帮助信息 |
n |
切换主机名解析 |
s |
切换端口号显示 |
d |
切换端口号解析 |
t |
切换显示格式(文本/条形图) |
p |
暂停/继续更新 |
q |
退出 iftop |
1-9 |
按相应数字排序 |
+ |
增加过滤 |
- |
减少过滤 |
实用技巧
监控网络带宽瓶颈
bash
# 实时监控所有网络活动
iftop -nP
# 监控特定接口
iftop -i eth0 -n
定位高带宽连接
bash
# 查看带宽使用最高的连接
iftop -t -s 1 | head -20
# 只显示出站流量
iftop -f "src net 192.168.1.0/24"
分析特定协议流量
bash
# 监控 HTTP 流量
iftop -f "tcp and port 80"
# 监控 HTTPS 流量
iftop -f "tcp and port 443"
# 监控 DNS 流量
iftop -f "udp and port 53"
脚本中使用
bash
# 获取总带宽使用
total_bandwidth=$(iftop -t -s 1 | grep "Total send and receive" | awk '{print $6}')
echo "Total Bandwidth: $total_bandwidth"
生成流量报告
bash
# 生成 1 分钟的流量报告
iftop -t -s 60 > network_report.txt
# 分析报告
cat network_report.txt | grep -E "^[ 0-9]" | sort -k 5 -r | head -10
相关命令
top:实时系统监控htop:增强版 topnetstat:网络连接统计ss:套接字统计工具tcpdump:网络流量捕获nload:网络带宽监控vnstat:网络流量统计bmon:带宽监控工具
注意事项
- 权限要求 :
iftop需要 root 权限才能正常运行 - 安装方法 :
- Debian/Ubuntu:
apt-get install iftop - CentOS/RHEL:
yum install iftop
- Debian/Ubuntu:
- 性能影响 :
iftop本身会产生一定的系统开销 - 混杂模式 :使用
-p选项会将接口设置为混杂模式 - BPF 过滤器:可以使用 tcpdump 风格的过滤器语法
总结
iftop 是 Linux 系统中用于实时监控网络带宽使用情况的强大工具。它可以帮助系统管理员快速定位网络带宽消耗大户和网络瓶颈,是网络性能监控和故障诊断的必备工具。通过结合其他工具(如 tcpdump、netstat、ss),可以全面了解系统的网络状态。