第10章 网络分析工具
完整 485 页的 《编译与调试完全指南》见: lengjingzju-notes发布页
网络问题是系统调试中最常见的问题类型之一。Linux 提供了丰富的网络分析工具,覆盖从抓包分析、连接状态查看、性能测试到流量监控的完整链路。本章按照"抓包→连接→性能→流量→配置→DNS"的逻辑组织,帮助读者快速定位和解决网络问题。
#mermaid-svg-mqvwyL7Zj2DpIGXw{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-mqvwyL7Zj2DpIGXw .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-mqvwyL7Zj2DpIGXw .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-mqvwyL7Zj2DpIGXw .error-icon{fill:#552222;}#mermaid-svg-mqvwyL7Zj2DpIGXw .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-mqvwyL7Zj2DpIGXw .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-mqvwyL7Zj2DpIGXw .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-mqvwyL7Zj2DpIGXw .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-mqvwyL7Zj2DpIGXw .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-mqvwyL7Zj2DpIGXw .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-mqvwyL7Zj2DpIGXw .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-mqvwyL7Zj2DpIGXw .marker{fill:#333333;stroke:#333333;}#mermaid-svg-mqvwyL7Zj2DpIGXw .marker.cross{stroke:#333333;}#mermaid-svg-mqvwyL7Zj2DpIGXw svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-mqvwyL7Zj2DpIGXw p{margin:0;}#mermaid-svg-mqvwyL7Zj2DpIGXw .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-mqvwyL7Zj2DpIGXw .cluster-label text{fill:#333;}#mermaid-svg-mqvwyL7Zj2DpIGXw .cluster-label span{color:#333;}#mermaid-svg-mqvwyL7Zj2DpIGXw .cluster-label span p{background-color:transparent;}#mermaid-svg-mqvwyL7Zj2DpIGXw .label text,#mermaid-svg-mqvwyL7Zj2DpIGXw span{fill:#333;color:#333;}#mermaid-svg-mqvwyL7Zj2DpIGXw .node rect,#mermaid-svg-mqvwyL7Zj2DpIGXw .node circle,#mermaid-svg-mqvwyL7Zj2DpIGXw .node ellipse,#mermaid-svg-mqvwyL7Zj2DpIGXw .node polygon,#mermaid-svg-mqvwyL7Zj2DpIGXw .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-mqvwyL7Zj2DpIGXw .rough-node .label text,#mermaid-svg-mqvwyL7Zj2DpIGXw .node .label text,#mermaid-svg-mqvwyL7Zj2DpIGXw .image-shape .label,#mermaid-svg-mqvwyL7Zj2DpIGXw .icon-shape .label{text-anchor:middle;}#mermaid-svg-mqvwyL7Zj2DpIGXw .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-mqvwyL7Zj2DpIGXw .rough-node .label,#mermaid-svg-mqvwyL7Zj2DpIGXw .node .label,#mermaid-svg-mqvwyL7Zj2DpIGXw .image-shape .label,#mermaid-svg-mqvwyL7Zj2DpIGXw .icon-shape .label{text-align:center;}#mermaid-svg-mqvwyL7Zj2DpIGXw .node.clickable{cursor:pointer;}#mermaid-svg-mqvwyL7Zj2DpIGXw .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-mqvwyL7Zj2DpIGXw .arrowheadPath{fill:#333333;}#mermaid-svg-mqvwyL7Zj2DpIGXw .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-mqvwyL7Zj2DpIGXw .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-mqvwyL7Zj2DpIGXw .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-mqvwyL7Zj2DpIGXw .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-mqvwyL7Zj2DpIGXw .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-mqvwyL7Zj2DpIGXw .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-mqvwyL7Zj2DpIGXw .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-mqvwyL7Zj2DpIGXw .cluster text{fill:#333;}#mermaid-svg-mqvwyL7Zj2DpIGXw .cluster span{color:#333;}#mermaid-svg-mqvwyL7Zj2DpIGXw div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-mqvwyL7Zj2DpIGXw .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-mqvwyL7Zj2DpIGXw rect.text{fill:none;stroke-width:0;}#mermaid-svg-mqvwyL7Zj2DpIGXw .icon-shape,#mermaid-svg-mqvwyL7Zj2DpIGXw .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-mqvwyL7Zj2DpIGXw .icon-shape p,#mermaid-svg-mqvwyL7Zj2DpIGXw .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-mqvwyL7Zj2DpIGXw .icon-shape .label rect,#mermaid-svg-mqvwyL7Zj2DpIGXw .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-mqvwyL7Zj2DpIGXw .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-mqvwyL7Zj2DpIGXw .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-mqvwyL7Zj2DpIGXw :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 网络分析工具全景
抓包与协议分析
tcpdump / Wireshark
连接与端口分析
ss / netstat / lsof
网络性能测试
iperf3 / ping / traceroute / mtr
流量与带宽监控
iftop / nethogs / nload / bmon / vnstat / nstat
网络配置与诊断
ip / ifconfig / route / ethtool / tc / dropwatch
DNS与ARP
dig / nslookup / host / arp
10.1 抓包与协议分析
本节介绍网络数据包捕获和协议分析的核心工具。tcpdump 是命令行下最强大的抓包工具,适合服务器环境和脚本化场景;Wireshark 则提供图形化的深度协议解析能力,适合复杂协议的详细分析。
10.1.1 tcpdump(命令行抓包与过滤)
tcpdump 是 Linux 下最强大的命令行网络数据包分析器,能够捕获网络接口上的数据包并进行过滤和显示。它是网络故障排查的首选工具。
基本用法
bash
tcpdump [options] [filter_expression]
常用选项
捕获控制类选项:
| 选项 | 说明 | 示例 |
|---|---|---|
-i <iface> |
指定网络接口(any 表示所有接口) |
tcpdump -i eth0 |
-c <count> |
捕获指定数量的包后退出 | tcpdump -c 100 |
-w <file> |
将原始数据包写入文件(pcap 格式) | tcpdump -w capture.pcap |
-r <file> |
从文件读取数据包 | tcpdump -r capture.pcap |
-s <snaplen> |
设置每个包的捕获字节数(默认 262144) | tcpdump -s 0 |
-B <size> |
设置捕获缓冲区大小(KiB) | tcpdump -B 4096 |
-Q <dir> |
指定捕获方向:in / out / inout |
tcpdump -Q in |
-p |
不将接口设为混杂模式 | tcpdump -p |
-D |
列出所有可用的网络接口 | tcpdump -D |
输出格式类选项:
| 选项 | 说明 | 示例 |
|---|---|---|
-n |
不将地址解析为主机名 | tcpdump -n |
-nn |
不解析主机名和端口名 | tcpdump -nn |
-v / -vv / -vvv |
递增的详细输出级别 | tcpdump -vv |
-A |
以 ASCII 格式打印每个包的数据部分 | tcpdump -A |
-X |
以十六进制和 ASCII 同时显示数据 | tcpdump -X |
-x |
以十六进制显示数据(不含链路层头) | tcpdump -x |
-e |
显示链路层头(MAC 地址等) | tcpdump -e |
-q |
快速输出,减少协议信息 | tcpdump -q |
-l |
使标准输出变为行缓冲(便于管道) | `tcpdump -l |
-U |
包缓冲模式(每收到一个包立即输出) | tcpdump -U -w file |
文件轮转与时间戳类选项:
| 选项 | 说明 | 示例 |
|---|---|---|
-C <size> |
文件达到指定大小后轮转(MB) | tcpdump -C 100 -w cap |
-G <seconds> |
按时间轮转文件 | tcpdump -G 3600 -w cap |
-W <count> |
限制轮转文件数量 | tcpdump -W 10 -C 100 -w cap |
-tt |
显示 Unix 时间戳格式 | tcpdump -tt |
-ttt |
显示与上一行的时间差 | tcpdump -ttt |
-tttt |
显示完整日期时间 | tcpdump -tttt |
-S |
显示绝对 TCP 序列号(而非相对) | tcpdump -S |
-Q 方向过滤选项详解
-Q 选项用于指定捕获的数据包方向,这在排查单向通信问题时非常有用。
| 方向值 | 含义 | 典型场景 |
|---|---|---|
in |
仅捕获入站(接收)数据包 | 排查服务端收到的请求 |
out |
仅捕获出站(发送)数据包 | 排查客户端发出的请求 |
inout |
捕获双向数据包(默认行为) | 完整分析通信过程 |
bash
# 只抓取入方向流量(排查服务端接收问题)
tcpdump -nni eth0 -Q in 'port 8080'
# 只抓取出方向流量(排查客户端发送问题)
tcpdump -nni eth0 -Q out 'host 10.0.0.1'
# 双向捕获(默认行为,等同于不加 -Q)
tcpdump -nni eth0 -Q inout 'port 443'
注意:
-Q选项并非所有平台都支持。在 Linux 上需要内核和 libpcap 版本支持。如果不支持,tcpdump 会报错提示。
-B 缓冲区大小选项详解
-B 选项设置操作系统捕获缓冲区的大小(单位为 KiB)。在高流量场景下,默认缓冲区可能不够大,导致内核丢弃数据包。
bash
# 设置缓冲区为 4MB(高流量场景)
tcpdump -nni eth0 -B 4096 -w capture.pcap
# 配合 -c 使用,避免高流量下丢包
tcpdump -nni eth0 -B 8192 -c 100000 -w large_capture.pcap
判断是否需要增大缓冲区: tcpdump 退出时会报告 packets dropped by kernel,如果该值大于 0,说明缓冲区不足,应增大 -B 的值。
^C
1523 packets captured
1523 packets received by filter
127 packets dropped by kernel ← 有丢包,需要增大 -B
过滤表达式
tcpdump 的过滤表达式基于 BPF(Berkeley Packet Filter)语法,支持丰富的过滤条件:
| 过滤类型 | 语法 | 示例 |
|---|---|---|
| 主机过滤 | host <addr> |
tcpdump host 192.168.1.1 |
| 源主机 | src host <addr> |
tcpdump src 192.168.1.1 |
| 目的主机 | dst host <addr> |
tcpdump dst 192.168.1.1 |
| 端口过滤 | port <num> |
tcpdump port 80 |
| 源端口 | src port <num> |
tcpdump src port 22 |
| 目的端口 | dst port <num> |
tcpdump dst port 443 |
| 协议过滤 | tcp / udp / icmp / arp |
tcpdump tcp |
| 网段过滤 | net <subnet> |
tcpdump net 192.168.1.0/24 |
| 组合(与) | and |
tcpdump host 1.1.1.1 and port 80 |
| 组合(或) | or |
tcpdump port 80 or port 443 |
| 组合(非) | not |
tcpdump not port 22 |
| TCP 标志 | tcp[tcpflags] |
tcpdump 'tcp[tcpflags] & tcp-syn != 0' |
| 广播 | broadcast |
tcpdump broadcast |
| 组播 | multicast |
tcpdump multicast |
实战示例
bash
# 抓取所有接口上的包(不解析名称)
tcpdump -ni any
# 抓取指定主机和端口的 HTTP 流量
tcpdump -nni eth0 'host 192.168.1.100 and port 80'
# 抓取 ICMP 包(ping 诊断)
tcpdump -nni eth0 icmp
# 抓取 SYN 包(观察连接建立)
tcpdump -nn 'tcp[tcpflags] & (tcp-syn) != 0'
# 抓取 SYN 和 FIN 包(观察连接建立和关闭)
tcpdump -nn 'tcp[tcpflags] & (tcp-syn|tcp-fin) != 0'
# 抓取并保存到文件(后续用 Wireshark 分析)
tcpdump -nni eth0 -w capture.pcap 'port 3306'
# 从文件读取并显示
tcpdump -nnr capture.pcap
# 抓取 100 个包后退出
tcpdump -nni eth0 -c 100 'host 10.0.0.1'
# 以 ASCII 显示 HTTP 请求内容
tcpdump -nni eth0 -A 'tcp port 80'
# 按时间轮转文件,每小时一个文件
tcpdump -nni eth0 -G 3600 -w '/tmp/capture_%Y%m%d_%H.pcap'
# 只抓取入方向流量
tcpdump -nni eth0 -Q in 'port 8080'
# 高流量场景:增大缓冲区 + 写入文件
tcpdump -nni eth0 -B 8192 -C 100 -W 20 -w '/tmp/cap_%Y%m%d.pcap'
输出解读
10:30:01.123456 IP 192.168.1.100.52341 > 93.184.216.34.80: Flags [S], seq 768512, win 65535, options [mss 1460,sackOK,TS val 123456 ecr 0,nop,wscale 7], length 0
| 字段 | 含义 |
|---|---|
10:30:01.123456 |
时间戳 |
IP |
协议类型 |
192.168.1.100.52341 |
源 IP.源端口 |
> |
方向(从源到目的) |
93.184.216.34.80 |
目的 IP.目的端口 |
Flags [S] |
TCP 标志(S=SYN, .=ACK, P=PSH, F=FIN, R=RST) |
seq |
序列号 |
win |
窗口大小 |
options |
TCP 选项 |
length |
数据长度 |
TCP 标志速查:
| 标志 | 含义 | 场景 |
|---|---|---|
S |
SYN | 连接请求(三次握手第一步) |
S. |
SYN+ACK | 连接确认(三次握手第二步) |
. |
ACK | 确认 |
P. |
PSH+ACK | 数据推送 |
F. |
FIN+ACK | 连接关闭请求 |
R |
RST | 连接重置 |
易错点
| 问题 | 原因 | 解决方法 |
|---|---|---|
| 过滤表达式不生效 | 含有特殊字符(如 &、(、))被 shell 解释 |
必须用单引号括起来 |
-w 写入文件时无输出 |
默认不显示 | 加 --print 选项同时显示 |
| 高流量下丢包 | 缓冲区不足 | 通过 -B 增大缓冲区 |
| 权限不足 | 需要 root 或 CAP_NET_RAW |
使用 sudo 或设置 capability |
-Q 选项报错 |
平台不支持 | 检查 libpcap 版本,或改用过滤表达式 |
10.1.2 Wireshark(图形化协议分析)
Wireshark 是图形化网络协议分析器,能够解析数百种协议,提供强大的过滤、搜索和统计功能。
| 特性 | 说明 |
|---|---|
| 图形界面 | 直观的数据包浏览和过滤 |
| 协议解析 | 支持数百种协议的深度解析 |
| 显示过滤器 | 如 tcp.port == 80、http.request |
| 统计功能 | 协议层次统计、会话统计、端点统计 |
| 流跟踪 | 跟踪 TCP/UDP 流,还原完整会话 |
| 文件格式 | 支持 pcap/pcapng 格式,与 tcpdump 互通 |
典型工作流程
bash
# 1. 使用 tcpdump 抓包
tcpdump -nni eth0 -w capture.pcap 'host 10.0.0.1'
# 2. 使用 Wireshark 打开分析
wireshark capture.pcap
常用显示过滤器
| 过滤器 | 说明 |
|---|---|
tcp.port == 80 |
过滤 80 端口的 TCP 包 |
ip.addr == 192.168.1.1 |
过滤指定 IP |
tcp.flags.syn == 1 |
过滤 SYN 包 |
http.request |
过滤 HTTP 请求 |
dns |
过滤 DNS 包 |
tcp.analysis.retransmission |
过滤 TCP 重传 |
10.2 连接与端口分析
本节介绍查看网络连接状态和端口占用情况的工具。ss 是现代推荐工具,netstat 是传统工具(正在被淘汰),lsof -i 则从进程角度查看网络连接。
10.2.1 ss(查看 socket 状态)
ss(Socket Statistics)是显示 socket 信息的现代工具,用于替代 netstat。它直接从内核的 netlink 接口获取信息,速度远快于 netstat(后者读取 /proc/net/tcp 文件)。
基本用法
bash
ss [options] [FILTER]
常用选项
| 选项 | 说明 | 示例 |
|---|---|---|
-t |
显示 TCP 套接字 | ss -t |
-u |
显示 UDP 套接字 | ss -u |
-a |
显示所有套接字(含监听) | ss -ta |
-l |
仅显示监听状态的套接字 | ss -tl |
-n |
不解析服务名称(显示数字端口) | ss -tn |
-p |
显示使用套接字的进程 | ss -tp |
-s |
显示汇总统计信息 | ss -s |
-o |
显示定时器信息(重传、keepalive 等) | ss -to |
-e |
显示扩展信息(uid、inode、socket cookie) | ss -te |
-m |
显示 socket 内存使用信息 | ss -tm |
-i |
显示 TCP 内部信息(RTT、拥塞窗口等) | ss -ti |
-4 |
仅显示 IPv4 | ss -4 |
-6 |
仅显示 IPv6 | ss -6 |
-x |
显示 Unix 域套接字 | ss -x |
-K |
强制关闭匹配的套接字 | ss -K dst 10.0.0.1 |
-H |
不显示表头 | ss -Ht |
-K 强制关闭连接选项详解
-K(--kill)选项尝试强制关闭匹配的套接字。它会显示成功关闭的套接字,并静默跳过内核不支持关闭的套接字。仅支持 IPv4 和 IPv6 套接字。
典型使用场景:
| 场景 | 命令 |
|---|---|
| 关闭到指定主机的所有连接 | ss -K dst 10.0.0.1 |
| 关闭到指定主机指定端口的连接 | ss -K dst 10.0.0.1 dport = :80 |
| 关闭所有 TIME_WAIT 连接 | ss -K state time-wait |
| 关闭所有到某网段的连接 | ss -K dst 192.168.1.0/24 |
bash
# 强制关闭到指定目标的连接
ss -K dst 10.0.0.1 dport = :80
# 强制关闭所有到某主机的 TCP 连接
ss -Kt dst 10.0.0.1
# 强制关闭处于 CLOSE_WAIT 状态的连接(应用未正确关闭)
ss -K state close-wait
注意:
-K选项需要内核支持 TCP_INFO 和 TCP_CLOSE 功能(Linux 4.5+)。需要 root 权限。
状态过滤器
| 状态 | 说明 |
|---|---|
established |
已建立连接 |
syn-sent |
已发送 SYN |
syn-recv |
已收到 SYN |
fin-wait-1 |
FIN_WAIT_1 状态 |
fin-wait-2 |
FIN_WAIT_2 状态 |
time-wait |
TIME_WAIT 状态 |
closed |
已关闭 |
close-wait |
CLOSE_WAIT 状态 |
last-ack |
LAST_ACK 状态 |
listening |
监听状态 |
closing |
CLOSING 状态 |
all |
所有状态 |
connected |
除 listening 和 closed 外所有状态 |
bucket |
TIME_WAIT 和 SYN_RECV(迷你套接字) |
big |
与 bucket 相反 |
实战示例
bash
# 查看所有 TCP/UDP 连接及进程信息
ss -tunap
# 查看监听的 TCP/UDP 端口
ss -tuln
# 查看套接字统计摘要
ss -s
# 查看所有已建立的 SSH 连接
ss -o state established '( dport = :ssh or sport = :ssh )'
# 查看 TIME_WAIT 状态的连接数量
ss -s state time-wait
# 查看指定端口的连接
ss -tn 'sport = :8080'
# 查看连接到特定目标的 TCP 连接
ss -tn dst 10.0.0.1
# 查看 TCP 连接的内部信息(RTT、拥塞窗口等)
ss -ti dst 10.0.0.1
# 查看 socket 内存使用情况
ss -tm dst 10.0.0.1
# 查看所有 Unix 域套接字
ss -xa
# 强制关闭到指定目标的连接
ss -K dst 10.0.0.1 dport = :80
输出字段解读
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
ESTAB 0 0 192.168.1.100:22 10.0.0.5:52341 users:(("sshd",pid=1234,fd=3))
| 字段 | 说明 |
|---|---|
State |
连接状态 |
Recv-Q |
接收队列中未读取的字节数 |
Send-Q |
发送队列中未确认的字节数 |
Local Address:Port |
本地地址和端口 |
Peer Address:Port |
对端地址和端口 |
Process |
使用该套接字的进程信息 |
Recv-Q 和 Send-Q 的诊断意义:
| 状态 | Recv-Q 含义 | Send-Q 含义 |
|---|---|---|
| ESTABLISHED | 接收缓冲区中未被应用读取的字节数 | 发送缓冲区中未被对端确认的字节数 |
| LISTEN | 当前 accept 队列中的连接数 | listen backlog 上限 |
诊断技巧: 如果 ESTABLISHED 状态下 Recv-Q 持续很大,说明应用层读取数据太慢;如果 Send-Q 持续很大,说明对端接收能力不足或网络拥塞。
10.2.2 netstat(传统连接查看)
netstat 是传统的网络连接查看工具,虽然功能与 ss 类似,但在连接数较多时性能较差(需要读取 /proc/net/tcp 等文件)。
基本用法
bash
netstat [options]
常用选项
| 选项 | 说明 |
|---|---|
-t |
显示 TCP 连接 |
-u |
显示 UDP 连接 |
-n |
不解析名称(显示数字) |
-a |
显示所有连接(含监听) |
-l |
仅显示监听状态 |
-p |
显示进程信息(需要 root) |
-s |
显示协议统计信息 |
-r |
显示路由表 |
-i |
显示网络接口统计 |
-c |
持续输出(每秒刷新) |
实战示例
bash
# 查看所有 TCP/UDP 连接及进程
netstat -tunap
# 查看监听的端口
netstat -tuln
# 查看协议统计(TCP/UDP/IP/ICMP)
netstat -s
# 查看 TCP 统计
netstat -st
# 查看路由表
netstat -rn
# 查看网络接口统计
netstat -i
ss 与 netstat 对比
| 特性 | ss | netstat |
|---|---|---|
| 数据来源 | netlink(内核直接提供) | /proc/net/tcp(文件解析) |
| 速度 | 快(连接数多时优势明显) | 慢 |
| 状态过滤 | 支持(state established) |
不支持 |
| 内存信息 | 支持(-m) |
不支持 |
| TCP 内部信息 | 支持(-i) |
不支持 |
| 强制关闭连接 | 支持(-K) |
不支持 |
| 推荐程度 | ✓ 推荐使用 | 逐步淘汰 |
10.2.3 lsof -i(进程级连接查看)
lsof 的 -i 选项可以列出所有网络连接,适合快速查看特定进程或端口的网络活动。
基本用法
bash
lsof -i[46] [protocol][@hostname|hostaddr][:service|port]
实战示例
bash
# 列出所有网络连接
lsof -i
# 查看 TCP 连接
lsof -i TCP
# 查看 UDP 连接
lsof -i UDP
# 查看指定端口的连接
lsof -i :80
# 查看指定主机的连接
lsof -i @192.168.1.1
# 查看指定进程的网络连接
lsof -i -p 1234
# 查看指定用户的网络连接
lsof -i -u username
10.3 网络性能测试
本节介绍网络连通性、延迟、带宽和路由路径的测试工具。iperf3 用于精确的带宽测量,ping 用于连通性和延迟测试,traceroute 和 mtr 用于路由路径分析。
10.3.1 iperf3(带宽测试)
iperf3 是网络带宽和性能测试的标准工具,支持 TCP/UDP 测试,可测量带宽、延迟、丢包率等指标。
基本用法
服务器端:
bash
# 启动 iperf3 服务器(默认端口 5201)
iperf3 -s
# 指定端口
iperf3 -s -p 5202
# 以守护进程模式运行
iperf3 -s -D
客户端:
bash
# TCP 带宽测试(默认)
iperf3 -c <server_ip>
# 指定测试时间(秒)
iperf3 -c <server_ip> -t 30
# UDP 测试(指定带宽 100Mbps)
iperf3 -c <server_ip> -u -b 100M
# 并行连接测试
iperf3 -c <server_ip> -P 4
# 反向测试(服务器发送到客户端)
iperf3 -c <server_ip> -R
# 指定窗口大小
iperf3 -c <server_ip> -w 256K
# 指定 MSS(最大段大小)
iperf3 -c <server_ip> -M 1400
# 双向同时测试
iperf3 -c <server_ip> --bidir
# JSON 格式输出
iperf3 -c <server_ip> -J
常用选项
| 选项 | 说明 |
|---|---|
-s |
服务器模式 |
-c <host> |
客户端模式,指定服务器地址 |
-p <port> |
指定端口(默认 5201) |
-t <seconds> |
测试持续时间(默认 10 秒) |
-i <seconds> |
报告间隔(默认 1 秒) |
-u |
使用 UDP(默认 TCP) |
-b <bandwidth> |
目标带宽(UDP 默认 1Mbps) |
-P <n> |
并行连接数 |
-R |
反向模式 |
-w <size> |
TCP 窗口大小 |
-M <size> |
TCP MSS |
-l <size> |
缓冲区长度 |
-D |
守护进程模式 |
-J |
JSON 输出 |
--bidir |
双向同时测试 |
--bidir 双向同时测试详解
--bidir 选项使 iperf3 同时进行双向带宽测试(客户端→服务器 和 服务器→客户端),这对于检测网络不对称性问题非常有用。
与 -R 的区别:
| 选项 | 行为 | 适用场景 |
|---|---|---|
| 默认(无选项) | 仅测试 客户端→服务器 方向 | 测试下载/上传带宽 |
-R |
仅测试 服务器→客户端 方向 | 测试反向带宽 |
--bidir |
同时测试两个方向 | 检测不对称性、全双工问题 |
bash
# 双向同时测试(检测网络不对称性)
iperf3 -c 192.168.1.1 --bidir -t 30
# 双向测试 + 并行连接
iperf3 -c 192.168.1.1 --bidir -P 4 -t 30
输出示例(双向测试):
[ ID] Interval Transfer Bitrate Retr Cwnd
[ 5] 0.00-1.00 sec 112 MBytes 940 Mbits/sec 0 456 KBytes
[ 7] 0.00-1.00 sec 112 MBytes 938 Mbits/sec 0 452 KBytes
...
[SUM] 0.00-10.00 sec 1.10 GBytes 940 Mbits/sec 0 sender
[SUM] 0.00-10.01 sec 1.10 GBytes 939 Mbits/sec receiver
[ ID] Interval Transfer Bitrate Retr Cwnd
[ 7] 0.00-1.00 sec 111 MBytes 932 Mbits/sec 0 448 KBytes
...
[SUM] 0.00-10.00 sec 1.09 GBytes 932 Mbits/sec 0 sender
[SUM] 0.00-10.01 sec 1.09 GBytes 931 Mbits/sec receiver
注意:
--bidir不能与-R同时使用。双向测试会同时占用两个方向的带宽,测试时间可能略长。
输出解读
[ ID] Interval Transfer Bitrate Retr Cwnd
[ 5] 0.00-1.00 sec 112 MBytes 940 Mbits/sec 0 456 KBytes
[ 5] 1.00-2.00 sec 112 MBytes 940 Mbits/sec 0 456 KBytes
...
[ 5] 0.00-10.00 sec 1.10 GBytes 940 Mbits/sec 0 sender
[ 5] 0.00-10.01 sec 1.10 GBytes 939 Mbits/sec receiver
| 字段 | 说明 |
|---|---|
Transfer |
传输数据量 |
Bitrate |
比特率(带宽) |
Retr |
重传次数(TCP) |
Cwnd |
拥塞窗口大小 |
分析要点:
| 指标 | 正常范围 | 异常含义 |
|---|---|---|
| Retr = 0 | 无重传 | 网络质量好 |
| Retr 较大 | 多次重传 | 网络拥塞或丢包 |
| Cwnd 持续增长 | 拥塞窗口扩大 | 带宽未饱和 |
| Cwnd 停滞/下降 | 拥塞窗口受限 | 存在丢包或瓶颈 |
| sender ≈ receiver | 传输正常 | --- |
| sender >> receiver | 接收端处理慢 | 检查接收端性能 |
10.3.2 ping(连通性与延迟测试)
ping 通过发送 ICMP Echo Request 报文测试网络连通性和延迟。
基本用法
bash
ping [options] <host>
常用选项
| 选项 | 说明 | 示例 |
|---|---|---|
-c <count> |
发送指定数量的包后停止 | ping -c 4 8.8.8.8 |
-i <interval> |
设置发送间隔(秒) | ping -i 0.5 8.8.8.8 |
-s <size> |
设置数据包大小(字节) | ping -s 1472 8.8.8.8 |
-W <timeout> |
等待响应的超时时间(秒) | ping -W 2 8.8.8.8 |
-f |
洪水模式(快速发送,需 root) | ping -f 8.8.8.8 |
-q |
安静模式,只显示统计 | ping -q -c 100 8.8.8.8 |
-I <iface> |
指定网络接口 | ping -I eth0 8.8.8.8 |
-M <mode> |
设置 DF 位模式(do / dont / want) |
ping -M do -s 1472 8.8.8.8 |
输出解读
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=3.42 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=118 time=3.38 ms
--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 3.380/3.400/3.420/0.020 ms
| 字段 | 说明 |
|---|---|
icmp_seq |
ICMP 序列号(检测丢包) |
ttl |
生存时间(可推断经过的路由跳数) |
time |
往返延迟(RTT) |
packet loss |
丢包率 |
rtt min/avg/max/mdev |
延迟统计(最小/平均/最大/偏差) |
实用技巧
bash
# 检测 MTU 大小(不分片模式 + 递增包大小)
ping -M do -s 1472 8.8.8.8 # 1472 + 28(IP+ICMP头) = 1500
# 持续监控网络质量
ping -i 0.2 -c 1000 8.8.8.8 | while read line; do echo "$(date) $line"; done
10.3.3 traceroute(路由路径跟踪)
traceroute 通过递增 TTL 值发送探测包,跟踪数据包从源到目的经过的路由路径。
基本用法
bash
traceroute [options] <host>
常用选项
| 选项 | 说明 |
|---|---|
-n |
不解析主机名 |
-m <max_hops> |
最大跳数(默认 30) |
-w <timeout> |
等待超时(秒) |
-q <nqueries> |
每跳探测次数(默认 3) |
-I |
使用 ICMP 而非 UDP |
-T |
使用 TCP |
-p <port> |
指定目的端口 |
实战示例
bash
# 基本路由跟踪
traceroute 8.8.8.8
# 不解析域名(加速显示)
traceroute -n 8.8.8.8
# 使用 ICMP 模式
traceroute -I 8.8.8.8
# 使用 TCP 模式(可穿透某些防火墙)
traceroute -T -p 80 8.8.8.8
输出解读
1 192.168.1.1 (192.168.1.1) 0.5 ms 0.4 ms 0.4 ms
2 10.0.0.1 (10.0.0.1) 2.1 ms 2.0 ms 2.2 ms
3 * * *
4 8.8.8.8 (8.8.8.8) 15.3 ms 15.1 ms 15.2 ms
- 每行代表一跳(一个路由器)
- 三个时间值代表三次探测的 RTT
*表示该路由器不响应(可能配置了不回复 ICMP)
10.3.4 mtr(持续路由监控)
mtr(My Traceroute)结合了 ping 和 traceroute 的功能,持续发送探测包并实时显示每一跳的延迟和丢包统计。
基本用法
bash
mtr [options] <host>
常用选项
| 选项 | 说明 |
|---|---|
-r |
报告模式(发送 10 个包后输出统计) |
-c <count> |
发送指定数量的包 |
-n |
不解析主机名 |
-4 |
仅使用 IPv4 |
-6 |
仅使用 IPv6 |
--tcp |
使用 TCP 探测 |
--udp |
使用 UDP 探测 |
-p <port> |
指定端口 |
实战示例
bash
# 实时交互模式
mtr 8.8.8.8
# 报告模式(适合脚本和日志)
mtr -r -c 100 8.8.8.8
# 不解析域名
mtr -rn 8.8.8.8
报告模式输出解读
HOST: myhost Loss% Snt Last Avg Best Wrst StDev
1.|-- 192.168.1.1 0.0% 100 0.5 0.4 0.3 1.2 0.1
2.|-- 10.0.0.1 0.0% 100 2.1 2.0 1.8 3.5 0.2
3.|-- ??? 100.0% 100 0.0 0.0 0.0 0.0 0.0
4.|-- 8.8.8.8 0.0% 100 15.3 15.1 14.8 16.2 0.3
| 列 | 说明 |
|---|---|
Loss% |
丢包率 |
Snt |
已发送包数 |
Last |
最近一次延迟 |
Avg |
平均延迟 |
Best |
最小延迟 |
Wrst |
最大延迟 |
StDev |
延迟标准差 |
mtr 与 traceroute 对比
| 特性 | mtr | traceroute |
|---|---|---|
| 持续探测 | ✓(实时更新) | ✗(一次性) |
| 丢包统计 | ✓ | ✗ |
| 延迟统计 | ✓(min/avg/max/stdev) | 仅显示各次探测值 |
| 交互模式 | ✓ | ✗ |
| 报告模式 | ✓(-r) |
默认 |
10.4 流量与带宽监控
本节介绍实时监控网络流量和带宽使用的工具。从连接级(iftop)到进程级(nethogs),从接口级图形化(nload/bmon)到历史统计(vnstat),以及协议栈统计(nstat),覆盖了流量监控的各个层面。
10.4.1 iftop(连接级带宽监控)
iftop 实时显示网络接口上的带宽使用情况,按连接对显示流量。
bash
# 基本用法
iftop
# 指定接口
iftop -i eth0
# 不解析主机名
iftop -n
# 不解析端口名
iftop -N
# 显示端口号
iftop -P
交互键:
| 键 | 说明 |
|---|---|
n |
切换是否解析主机名 |
s |
切换是否显示源地址 |
d |
切换是否显示目的地址 |
t |
切换显示模式 |
q |
退出 |
10.4.2 nethogs(进程级带宽监控)
nethogs 按进程显示网络带宽使用,适合定位"哪个进程在占用带宽"。
bash
# 基本用法(需要 root)
nethogs
# 指定接口
nethogs eth0
# 以 KB/s 为单位
nethogs -s
10.4.3 nload(接口流量图形化)
nload 直观展示单个网络接口的实时入站/出站流量,带有图形化柱状图。
bash
# 基本用法
nload
# 指定接口
nload eth0
# 指定刷新间隔(毫秒)
nload -t 500 eth0
10.4.4 bmon(多接口带宽统计)
bmon(Bandwidth Monitor)是轻量级多接口带宽统计工具,支持图形化显示。
bash
# 基本用法
bmon
# 指定接口
bmon -p eth0
# 以 ASCII 模式输出
bmon -o ascii
10.4.5 vnstat(历史流量统计)
vnstat 是网络流量统计工具,基于内核计数器记录历史流量数据(按小时/天/月统计),适合长期监控。
bash
# 查看实时流量
vnstat -l
# 查看每日统计
vnstat -d
# 查看每月统计
vnstat -m
# 查看每小时统计
vnstat -h
# 查看指定接口
vnstat -i eth0 -d
# 查看 TOP 10 流量日
vnstat -t
10.4.6 nstat(协议栈统计)
nstat 显示网络协议栈的详细统计信息(基于 /proc/net/snmp 和 /proc/net/netstat)。
bash
# 查看所有统计并重置计数器
nstat
# 查看统计但不重置
nstat -s
# 查看特定统计项
nstat -s TcpRetransSegs
nstat -s TcpExtTCPTimeouts
# 以 JSON 格式输出
nstat -j
流量监控工具对比
| 工具 | 粒度 | 适用场景 | 特点 |
|---|---|---|---|
| iftop | 连接对 | 查看哪个连接占带宽 | 实时、按连接显示 |
| nethogs | 进程 | 查看哪个进程占带宽 | 按进程聚合 |
| nload | 接口 | 查看接口总流量 | 图形化、简单直观 |
| bmon | 接口 | 多接口带宽统计 | 轻量、多接口 |
| vnstat | 接口(历史) | 长期流量统计 | 记录历史数据 |
| nstat | 协议栈 | 协议层统计 | 内核计数器 |
10.5 网络配置与诊断
本节介绍网络接口配置、路由管理、网卡参数查看与调优、流量控制以及内核丢包定位等工具。ip 命令是现代网络配置的核心,ethtool 用于网卡底层参数诊断,tc 用于流量控制,dropwatch 用于定位内核丢包。
10.5.1 ip(网络配置管理)
ip 是 iproute2 套件的核心工具,用于配置和查看网络接口、路由、地址等。它是 ifconfig/route 的现代替代。
子命令结构
bash
ip [options] <object> <command> [arguments]
| 对象 | 说明 |
|---|---|
link |
网络接口(设备) |
addr |
协议地址 |
route |
路由表 |
neigh |
邻居表(ARP) |
netns |
网络命名空间 |
rule |
路由策略规则 |
实战示例
bash
# 查看所有网络接口
ip link show
# 查看接口详细信息(含统计)
ip -s link show eth0
# 查看所有 IP 地址
ip addr show
# 查看特定接口地址
ip addr show eth0
# 查看路由表
ip route show
# 查看默认路由
ip route show default
# 查看 ARP 表
ip neigh show
# 添加 IP 地址
ip addr add 192.168.1.100/24 dev eth0
# 删除 IP 地址
ip addr del 192.168.1.100/24 dev eth0
# 启用/禁用接口
ip link set eth0 up
ip link set eth0 down
# 添加路由
ip route add 10.0.0.0/8 via 192.168.1.1
# 删除路由
ip route del 10.0.0.0/8
# 添加默认路由
ip route add default via 192.168.1.1
# 查看网络命名空间
ip netns list
# 在命名空间中执行命令
ip netns exec myns ip addr show
10.5.2 ifconfig(传统接口配置)
ifconfig 是传统的网络接口配置和查看工具(属于 net-tools 包),正在被 ip 命令替代。
bash
# 查看所有接口
ifconfig -a
# 查看特定接口
ifconfig eth0
# 设置 IP 地址
ifconfig eth0 192.168.1.100 netmask 255.255.255.0
# 启用/禁用接口
ifconfig eth0 up
ifconfig eth0 down
# 设置 MTU
ifconfig eth0 mtu 9000
ip 与 ifconfig 对比
| 操作 | ip 命令 | ifconfig |
|---|---|---|
| 查看接口 | ip link show |
ifconfig -a |
| 查看地址 | ip addr show |
ifconfig |
| 添加地址 | ip addr add |
ifconfig eth0 x.x.x.x |
| 查看路由 | ip route show |
route -n |
| 查看 ARP | ip neigh show |
arp -a |
| 推荐程度 | ✓ 推荐 | 逐步淘汰 |
10.5.3 route(路由表管理)
route 用于查看和操作内核路由表。
bash
# 查看路由表
route -n
# 添加默认网关
route add default gw 192.168.1.1
# 添加静态路由
route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.1
# 删除路由
route del -net 10.0.0.0 netmask 255.0.0.0
10.5.4 ethtool(网卡参数与统计)
ethtool 用于查询和配置网卡参数及查看硬件统计信息。
bash
# 查看网卡基本信息(速率、双工、链路状态)
ethtool eth0
# 查看网卡驱动信息
ethtool -i eth0
# 查看网卡统计信息(收发包数、错误数等)
ethtool -S eth0
# 查看网卡支持的参数
ethtool -k eth0
# 设置速率和双工模式
ethtool -s eth0 speed 1000 duplex full autoneg off
# 查看网卡 ring buffer 大小
ethtool -g eth0
# 设置 ring buffer 大小
ethtool -G eth0 rx 4096 tx 4096
# 查看网卡中断合并设置
ethtool -c eth0
# 测试网卡(在线测试)
ethtool -t eth0
常用输出字段:
| 字段 | 说明 |
|---|---|
| Speed | 链路速率(如 1000Mb/s) |
| Duplex | 双工模式(Full/Half) |
| Auto-negotiation | 是否自动协商 |
| Link detected | 链路是否连接 |
| RX/TX errors | 收发错误数 |
| RX/TX dropped | 收发丢包数 |
10.5.5 tc(流量控制)
tc(Traffic Control)是 Linux 流量控制工具,用于配置 QoS(服务质量)、限速、整形等。
bash
# 查看接口的 qdisc(排队规则)
tc qdisc show dev eth0
# 查看接口的 class(类)
tc class show dev eth0
# 查看接口的 filter(过滤器)
tc filter show dev eth0
# 添加限速(限制 eth0 出口带宽为 10Mbps)
tc qdisc add dev eth0 root tbf rate 10mbit burst 32kbit latency 400ms
# 删除限速
tc qdisc del dev eth0 root
# 查看统计
tc -s qdisc show dev eth0
10.5.6 dropwatch(内核丢包定位)
dropwatch 用于监控和定位内核中的网络丢包位置。
bash
# 启动 dropwatch
dropwatch
# 在 dropwatch 交互界面中
> start # 开始监控
> stop # 停止监控
> set alertcount 10 # 设置告警阈值
替代方案(使用内核统计):
bash
# 查看网络协议栈丢包统计
nstat -s | grep -i drop
# 查看接口丢包
ip -s link show eth0
# 查看 netfilter 丢包
iptables -L -v -n
10.6 DNS 与 ARP
本节介绍 DNS 域名解析和 ARP 地址解析的查询与管理工具。dig 是功能最全面的 DNS 查询工具,nslookup 提供交互式查询,host 输出最简洁,arp 管理 ARP 缓存。
10.6.1 dig(DNS查询)
dig(Domain Information Groper)是功能最全面的 DNS 查询工具。
bash
# 查询 A 记录(默认)
dig example.com
# 查询指定记录类型
dig example.com AAAA # IPv6 记录
dig example.com MX # 邮件交换记录
dig example.com NS # 名称服务器记录
dig example.com CNAME # 别名记录
dig example.com TXT # 文本记录
dig example.com SOA # 起始授权记录
# 指定 DNS 服务器查询
dig @8.8.8.8 example.com
# 反向查询(IP 到域名)
dig -x 8.8.8.8
# 简洁输出
dig +short example.com
# 仅显示答案部分
dig +noall +answer example.com
# 跟踪完整解析路径
dig +trace example.com
# 查询 DNS 响应时间
dig example.com | grep "Query time"
10.6.2 nslookup(交互式DNS查询)
nslookup 是交互式 DNS 查询工具。
bash
# 基本查询
nslookup example.com
# 指定 DNS 服务器
nslookup example.com 8.8.8.8
# 查询特定记录类型
nslookup -type=MX example.com
# 反向查询
nslookup 8.8.8.8
10.6.3 host(简洁DNS查询)
host 是简单的 DNS 查询工具,输出简洁。
bash
# 基本查询
host example.com
# 查询特定类型
host -t MX example.com
# 指定 DNS 服务器
host example.com 8.8.8.8
# 反向查询
host 8.8.8.8
DNS 查询工具对比
| 特性 | dig | nslookup | host |
|---|---|---|---|
| 输出详细度 | 最详细 | 中等 | 最简洁 |
| 交互模式 | ✗ | ✓ | ✗ |
| 脚本友好 | ✓(+short) |
一般 | ✓ |
| 跟踪解析路径 | ✓(+trace) |
✗ | ✗ |
| 推荐程度 | ✓ 推荐 | 传统 | 简单场景 |
10.6.4 arp(ARP缓存管理)
arp 用于查看和修改系统的 ARP(地址解析协议)缓存表。
bash
# 查看 ARP 缓存
arp -a
# 查看特定主机的 ARP 条目
arp -a 192.168.1.1
# 删除 ARP 条目
arp -d 192.168.1.1
# 添加静态 ARP 条目
arp -s 192.168.1.1 00:11:22:33:44:55
# 使用 ip 命令查看 ARP(推荐)
ip neigh show
# 使用 ip 命令刷新 ARP
ip neigh flush all
10.7 TCP 流量分析
本节介绍基于 pcap 文件的 TCP 连接性能分析工具,用于离线分析已捕获的网络数据。
10.7.1 tcptrace(TCP连接性能分析)
tcptrace 用于分析 TCP 连接的性能指标,如重传率、RTT 分布、窗口大小等。
bash
# 分析 pcap 文件
tcptrace -s capture.pcap # 基本统计
tcptrace -l capture.pcap # 长格式输出
tcptrace -c capture.pcap # 完整输出
tcptrace --csv capture.pcap # CSV 格式输出
分析指标:
| 指标 | 说明 |
|---|---|
| RTT | 往返时间 |
| Retransmissions | 重传次数和比例 |
| Window size | TCP 窗口大小变化 |
| Throughput | 吞吐量 |
| Zero window | 零窗口事件(接收方缓冲区满) |
10.8 网络调试组合与决策流程
本节将前面介绍的工具组合起来,形成系统化的网络问题排查流程。通过决策图和典型场景示例,帮助读者快速选择合适的工具组合。
10.8.1 按问题类型决策图
#mermaid-svg-En2Y3x8JBUELuYHU{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-En2Y3x8JBUELuYHU .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-En2Y3x8JBUELuYHU .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-En2Y3x8JBUELuYHU .error-icon{fill:#552222;}#mermaid-svg-En2Y3x8JBUELuYHU .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-En2Y3x8JBUELuYHU .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-En2Y3x8JBUELuYHU .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-En2Y3x8JBUELuYHU .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-En2Y3x8JBUELuYHU .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-En2Y3x8JBUELuYHU .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-En2Y3x8JBUELuYHU .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-En2Y3x8JBUELuYHU .marker{fill:#333333;stroke:#333333;}#mermaid-svg-En2Y3x8JBUELuYHU .marker.cross{stroke:#333333;}#mermaid-svg-En2Y3x8JBUELuYHU svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-En2Y3x8JBUELuYHU p{margin:0;}#mermaid-svg-En2Y3x8JBUELuYHU .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-En2Y3x8JBUELuYHU .cluster-label text{fill:#333;}#mermaid-svg-En2Y3x8JBUELuYHU .cluster-label span{color:#333;}#mermaid-svg-En2Y3x8JBUELuYHU .cluster-label span p{background-color:transparent;}#mermaid-svg-En2Y3x8JBUELuYHU .label text,#mermaid-svg-En2Y3x8JBUELuYHU span{fill:#333;color:#333;}#mermaid-svg-En2Y3x8JBUELuYHU .node rect,#mermaid-svg-En2Y3x8JBUELuYHU .node circle,#mermaid-svg-En2Y3x8JBUELuYHU .node ellipse,#mermaid-svg-En2Y3x8JBUELuYHU .node polygon,#mermaid-svg-En2Y3x8JBUELuYHU .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-En2Y3x8JBUELuYHU .rough-node .label text,#mermaid-svg-En2Y3x8JBUELuYHU .node .label text,#mermaid-svg-En2Y3x8JBUELuYHU .image-shape .label,#mermaid-svg-En2Y3x8JBUELuYHU .icon-shape .label{text-anchor:middle;}#mermaid-svg-En2Y3x8JBUELuYHU .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-En2Y3x8JBUELuYHU .rough-node .label,#mermaid-svg-En2Y3x8JBUELuYHU .node .label,#mermaid-svg-En2Y3x8JBUELuYHU .image-shape .label,#mermaid-svg-En2Y3x8JBUELuYHU .icon-shape .label{text-align:center;}#mermaid-svg-En2Y3x8JBUELuYHU .node.clickable{cursor:pointer;}#mermaid-svg-En2Y3x8JBUELuYHU .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-En2Y3x8JBUELuYHU .arrowheadPath{fill:#333333;}#mermaid-svg-En2Y3x8JBUELuYHU .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-En2Y3x8JBUELuYHU .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-En2Y3x8JBUELuYHU .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-En2Y3x8JBUELuYHU .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-En2Y3x8JBUELuYHU .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-En2Y3x8JBUELuYHU .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-En2Y3x8JBUELuYHU .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-En2Y3x8JBUELuYHU .cluster text{fill:#333;}#mermaid-svg-En2Y3x8JBUELuYHU .cluster span{color:#333;}#mermaid-svg-En2Y3x8JBUELuYHU div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-En2Y3x8JBUELuYHU .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-En2Y3x8JBUELuYHU rect.text{fill:none;stroke-width:0;}#mermaid-svg-En2Y3x8JBUELuYHU .icon-shape,#mermaid-svg-En2Y3x8JBUELuYHU .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-En2Y3x8JBUELuYHU .icon-shape p,#mermaid-svg-En2Y3x8JBUELuYHU .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-En2Y3x8JBUELuYHU .icon-shape .label rect,#mermaid-svg-En2Y3x8JBUELuYHU .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-En2Y3x8JBUELuYHU .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-En2Y3x8JBUELuYHU .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-En2Y3x8JBUELuYHU :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 连通性问题
否
是
性能问题
否
是
丢包问题
DNS问题
连接异常
是
否
网络问题
问题类型?
ping 测试连通性
ping 通?
traceroute/mtr 定位断点
检查路由: ip route show
检查防火墙: iptables -L
检查端口: ss -tn / telnet
iperf3 测试带宽
带宽正常?
ethtool -S 检查网卡错误
mtr 检查中间节点丢包
检查应用层: tcpdump 抓包分析
mtr 定位丢包节点
nstat -s 查看协议栈统计
ethtool -S 查看网卡丢包
ss -s 查看连接状态统计
dig +trace 跟踪解析
检查 /etc/resolv.conf
nslookup 指定服务器测试
ss -tunap 查看连接状态
大量 TIME_WAIT?
检查 tcp_tw_reuse / tcp_fin_timeout
tcpdump 抓包分析握手/挥手
10.8.2 典型排查流程示例
场景一:服务无法访问
bash
# 1. 测试连通性
ping -c 4 server_ip
# 2. 测试端口连通性
ss -tn dst server_ip dport = :8080
# 3. 检查本地路由
ip route get server_ip
# 4. 跟踪路由路径
mtr -rn server_ip
# 5. 抓包分析
tcpdump -nni eth0 'host server_ip and port 8080'
场景二:网络延迟高
bash
# 1. 持续 ping 观察延迟和丢包
ping -c 100 server_ip
# 2. 使用 mtr 定位延迟节点
mtr -r -c 100 server_ip
# 3. 检查网卡错误
ethtool -S eth0 | grep -i error
# 4. 检查 TCP 重传
nstat -s TcpRetransSegs
# 5. 抓包分析重传
tcpdump -nni eth0 'tcp[tcpflags] & tcp-syn != 0' -w syn.pcap
场景三:带宽不足
bash
# 1. 使用 iperf3 测试实际带宽
iperf3 -c server_ip -t 30
# 2. 检查网卡速率
ethtool eth0 | grep Speed
# 3. 实时监控流量
iftop -i eth0
# 4. 检查是否有异常进程占用带宽
nethogs eth0
# 5. 检查 TCP 参数
sysctl net.ipv4.tcp_rmem
sysctl net.ipv4.tcp_wmem
sysctl net.core.rmem_max
sysctl net.core.wmem_max
10.8.3 网络分析工具对比总结表
| 工具 | 类别 | 主要用途 | 典型命令 |
|---|---|---|---|
| tcpdump | 抓包 | 命令行抓包和过滤 | tcpdump -nni eth0 'port 80' |
| Wireshark | 抓包 | 图形化协议分析 | wireshark capture.pcap |
| ss | 连接 | 查看 socket 状态 | ss -tunap |
| netstat | 连接 | 传统连接查看 | netstat -tunap |
| lsof | 连接 | 进程级连接查看 | lsof -i :80 |
| iperf3 | 性能 | 带宽测试 | iperf3 -c server |
| ping | 性能 | 连通性和延迟 | ping -c 4 host |
| traceroute | 性能 | 路由路径跟踪 | traceroute host |
| mtr | 性能 | 持续路由监控 | mtr -r host |
| iftop | 流量 | 连接级带宽监控 | iftop -i eth0 |
| nethogs | 流量 | 进程级带宽监控 | nethogs eth0 |
| nload | 流量 | 接口流量图形化 | nload eth0 |
| bmon | 流量 | 多接口带宽统计 | bmon |
| vnstat | 流量 | 历史流量统计 | vnstat -d |
| nstat | 流量 | 协议栈统计 | nstat -s |
| ip | 配置 | 接口/路由/地址管理 | ip addr show |
| ifconfig | 配置 | 传统接口配置 | ifconfig eth0 |
| route | 配置 | 路由表管理 | route -n |
| ethtool | 诊断 | 网卡参数和统计 | ethtool -S eth0 |
| tc | 诊断 | 流量控制/QoS | tc qdisc show |
| dropwatch | 诊断 | 内核丢包定位 | dropwatch |
| dig | DNS | DNS 查询 | dig example.com |
| nslookup | DNS | DNS 查询(交互) | nslookup example.com |
| host | DNS | DNS 查询(简洁) | host example.com |
| arp | ARP | ARP 缓存管理 | arp -a |
| tcptrace | 分析 | TCP 连接性能分析 | tcptrace -s file.pcap |
10.9 本章小结
本章系统介绍了 Linux 网络分析的完整工具链,从数据包捕获、连接状态查看、性能测试到流量监控和配置诊断,覆盖了网络问题排查的各个环节。以下是核心知识速查:
工具选择速查:
| 场景 | 首选工具 | 备选工具 |
|---|---|---|
| 抓包分析(命令行) | tcpdump | --- |
| 抓包分析(图形化) | Wireshark | --- |
| 查看连接状态 | ss | netstat / lsof -i |
| 强制关闭连接 | ss -K | --- |
| 带宽测试 | iperf3 | --- |
| 连通性测试 | ping | --- |
| 路由路径分析 | mtr | traceroute |
| 查看哪个连接占带宽 | iftop | --- |
| 查看哪个进程占带宽 | nethogs | --- |
| 长期流量统计 | vnstat | --- |
| 协议栈统计 | nstat | netstat -s |
| 网卡错误诊断 | ethtool -S | ip -s link |
| DNS 查询 | dig | nslookup / host |
| 内核丢包定位 | dropwatch | nstat / iptables |
| 流量限速 | tc | --- |
核心判断口诀:
| 现象 | 判断 | 下一步 |
|---|---|---|
| ping 不通 | 网络层问题 | traceroute/mtr 定位断点 |
| ping 通但端口不通 | 传输层/防火墙问题 | ss 检查端口、iptables 检查规则 |
| 连接建立慢 | DNS 或握手问题 | dig +trace、tcpdump 分析 SYN |
| 传输速度慢 | 带宽/拥塞问题 | iperf3 测带宽、ss -ti 看拥塞窗口 |
| 大量重传 | 网络丢包 | mtr 定位、ethtool -S 检查网卡 |
| 大量 TIME_WAIT | 短连接过多 | 调整 tcp_tw_reuse / tcp_fin_timeout |
| 大量 CLOSE_WAIT | 应用未正确关闭连接 | 检查应用代码 |