网络排障命令速查表
网络问题是运维和开发中最常见的故障之一。本速查表汇集了 Linux 环境下最实用的网络排障命令,覆盖从物理接口、套接字连接到 DNS 解析、路由追踪、数据包抓取、性能基准测试和 TLS 安全诊断的完整排障链路。每个命令都配有实际可运行示例,帮助你快速定位网络瓶颈和故障点。
场景速查
| 你要做什么 | 用哪个命令 |
| 查看网络接口和 IP | ip addr show 或 ip a |
| 查看监听端口 | ss -tuln |
| DNS 域名解析 | dig example.com |
| 路由追踪 | traceroute example.com |
| 持续追踪 + 延迟 | mtr example.com |
| 基础连通性测试 | ping -c 5 example.com |
| 端口连通性测试 | nc -vz host port |
| 抓包分析 | tcpdump -i eth0 |
| 带宽测试 | iperf3 -c server_ip |
| TLS 握手诊断 | openssl s_client -connect host:443 |
网络接口
``bash
# 列出所有网络接口及状态
ip link show
# 查看 IP 地址分配 ip addr show # 或简写 ip a
# 启用/禁用网络接口 ip link set eth0 up ip link set eth0 down
# 查看接口统计(收发包、错误、丢包) ip -s link show eth0
# 查询网卡驱动能力和当前设置 ethtool eth0
# 查看网卡驱动信息和固件版本 ethtool -i eth0
# 查看网卡协商速率和双工模式 ethtool eth0 | grep -E 'Speed|Duplex|Link detected'
# 查看 PCI 设备信息(物理网卡定位)
lspci | grep -i ethernet
`
套接字与连接
`bash
# 列出所有监听中的 TCP/UDP 端口(数字格式)
ss -tuln
# 查看已建立的 TCP 连接及对应进程 ss -tup
# 套接字统计摘要(总连接数、各状态统计) ss -s
# 按状态过滤连接(如 TIME-WAIT) ss -t state time-wait
# 查看特定端口的连接 ss -t sport = :22
# netstat 方式查看监听端口(传统兼容) netstat -tuln
# 查看网络统计信息 netstat -s
# 测试 TCP 端口是否开放 nc -vz example.com 443
# 测试 UDP 端口 nc -vzu example.com 53
# 创建一个简单的 TCP 监听服务(用于测试) nc -lk 9999
# TCP 全连接扫描 nmap -sT example.com
# SYN 隐式扫描(需要 root) nmap -sS example.com
# 扫描指定端口 nmap -p 22,80,443 example.com
# 快速扫描常见端口
nmap -F example.com
`
DNS 解析
`bash
# 详细 DNS 查询(A 记录)
dig example.com
# 指定 DNS 服务器查询 dig @8.8.8.8 example.com
# 反向 DNS 查询(IP 到域名) dig -x 8.8.8.8
# 查询特定记录类型(MX) dig example.com MX
# 查询所有记录类型 dig example.com ANY
# 短格式输出(仅结果) dig +short example.com
# 追踪 DNS 解析路径 dig +trace example.com
# 基础 DNS 查询 nslookup example.com
# 指定 DNS 服务器 nslookup example.com 8.8.8.8
# 快速查询(简洁输出) host example.com
# 查询特定记录类型 host -t AAAA example.com
# 反向查询
host 8.8.8.8
`
路由与路径
`bash
# 查看路由表
ip route show
# 或简写
ip r
# 添加默认网关 ip route add default via 10.0.0.1
# 添加静态路由 ip route add 10.0.0.0/24 via 192.168.1.1
# 删除路由 ip route del 10.0.0.0/24
# 查看路由缓存(策略路由) ip route get 8.8.8.8
# 追踪到目标的路由路径 traceroute example.com
# 快速追踪(不解析主机名) traceroute -n example.com
# 设置最大跳数和超时 traceroute -m 30 -w 3 example.com
# 使用 ICMP 模式(穿越更多防火墙) traceroute -I example.com
# 持续追踪 + 实时延迟统计 mtr example.com
# 以报告模式运行(发送指定数量探测包后退出) mtr -r -c 50 example.com
# 不解析主机名(更快的 MTR) mtr -n example.com
# 基础 ping(发送 5 个包) ping -c 5 example.com
# 指定间隔(0.2 秒一个包) ping -i 0.2 -c 20 example.com
# 洪泛 ping(压力测试,需 root)
ping -f -c 1000 target
`
包捕获与分析
`bash
# 抓取接口上的所有流量
tcpdump -i eth0
# 限制抓取数量 tcpdump -i eth0 -c 100
# 抓取特定端口的流量 tcpdump -i eth0 port 443
# 抓取特定主机的流量 tcpdump -i eth0 host 10.0.0.1
# 组合条件(与) tcpdump -i eth0 port 80 and host 10.0.0.1
# 组合条件(或) tcpdump -i eth0 port 80 or port 443
# 保存抓包到文件(供 Wireshark 分析) tcpdump -i eth0 -w capture.pcap
# 读取保存的抓包文件 tcpdump -r capture.pcap
# 显示十六进制和 ASCII 内容 tcpdump -i eth0 -X
# 抓包时显示包内容(-A 仅 ASCII) tcpdump -i eth0 -A host example.com
# 不解析主机名/端口名(更快) tcpdump -i eth0 -nn
# 抓取 ICMP 包(如 ping) tcpdump -i eth0 icmp
# 抓取特定大小的包
tcpdump -i eth0 greater 1000
`
网络性能
`bash
# 启动 iperf3 服务端
iperf3 -s
# iperf3 客户端测速(默认 TCP) iperf3 -c 10.0.0.1
# iperf3 UDP 测试(指定带宽) iperf3 -c 10.0.0.1 -u -b 100m
# iperf3 反向测试(测服务端到客户端) iperf3 -c 10.0.0.1 -R
# iperf3 双向测试(同时收发) iperf3 -c 10.0.0.1 --bidir
# iperf3 指定测试时长(秒) iperf3 -c 10.0.0.1 -t 30
# iperf3 指定并行流数 iperf3 -c 10.0.0.1 -P 4
# 测试 MTU(最大传输单元) ping -M do -s 1472 target
# 检测路径 MTU
traceroute --mtu target
`
TLS/SSL 诊断
`bash
# 测试 TLS 握手(查看证书、加密套件)
openssl s_client -connect example.com:443
# 查看完整证书链 openssl s_client -connect example.com:443 -showcerts
# 带 SNI 的 TLS 连接(多域名场景必需) openssl s_client -connect 1.2.3.4:443 -servername example.com
# 查看证书有效期 openssl s_client -connect example.com:443 \ 2>/dev/null | openssl x509 -noout -dates
# 查看证书主题和颁发者 openssl s_client -connect example.com:443 \ 2>/dev/null | openssl x509 -noout -subject -issuer
# 指定 TLS 版本 openssl s_client -connect example.com:443 -tls1_2
# 测试特定加密套件 openssl s_client -connect example.com:443 \ -cipher 'ECDHE-RSA-AES128-GCM-SHA256'
# 检查证书链是否完整 openssl s_client -connect example.com:443 \ -verify_return_error -showcerts \ < /dev/null 2>/dev/null ``
网络接口(5)
| 命令 | 难度 | ||
|---|---|---|---|
ip link show列出所有网络接口及其状态 | 基础 | ip link show | |
ip addr show / ip a查看网络接口的 IP 地址分配 | 基础 | ip addr show | |
ip link set <iface> up/down启用或禁用网络接口 | 中级 | ip link set eth0 up | |
ip -s link show <iface>查看接口收发统计(包数、错误、丢包) | 中级 | ip -s link show eth0 | |
ethtool <iface>查询网卡驱动能力与当前设置(速率、双工等) | 中级 | ethtool eth0 |
套接字与连接(9)
| 命令 | 难度 | ||
|---|---|---|---|
ss -tuln列出所有监听中的 TCP/UDP 端口(数字格式) | 基础 | ss -tuln | |
ss -tup查看已建立的 TCP 连接及对应进程 | 中级 | ss -tup | |
ss -s查看套接字统计摘要(总连接数、各状态分布) | 中级 | ss -s | |
netstat -tuln传统方式查看监听端口(net-tools 兼容) | 基础 | netstat -tuln | |
nc -vz <host> <port>测试 TCP 端口是否可达(端口扫描) | 基础 | nc -vz example.com 443 | |
nc -lk <port>创建一个简单的 TCP 监听服务(用于测试) | 中级 | nc -lk 9999 | |
nmap -sT <target>TCP 全连接扫描(完整三次握手) | 中级 | nmap -sT example.com | |
nmap -sS <target>SYN 隐式扫描(半开扫描,需 root 权限) | 高级 | nmap -sS example.com | |
nmap -p <ports> <target>扫描指定的一个或多个端口 | 中级 | nmap -p 22,80,443 example.com |
DNS 解析(5)
| 命令 | 难度 | ||
|---|---|---|---|
dig <domain>详细 DNS 查询(默认查询 A 记录) | 基础 | dig example.com | |
dig @<server> <domain>指定特定 DNS 服务器进行查询 | 中级 | dig @8.8.8.8 example.com | |
dig -x <ip>反向 DNS 查询(IP 地址到域名) | 中级 | dig -x 8.8.8.8 | |
nslookup <domain>基础 DNS 查询工具(支持交互模式) | 基础 | nslookup example.com | |
host <domain>快速 DNS 查询(简洁输出) | 基础 | host example.com |
路由与路径(8)
| 命令 | 难度 | ||
|---|---|---|---|
ip route show查看系统路由表 | 中级 | ip route show | |
ip route add <prefix> via <gateway>添加静态路由或默认网关 | 中级 | ip route add default via 10.0.0.1 | |
ip route get <ip>查看某 IP 的路由决策路径 | 中级 | ip route get 8.8.8.8 | |
traceroute <host>追踪到目标主机的网络路径 | 中级 | traceroute example.com | |
traceroute -n <host>路径追踪(不解析主机名,速度更快) | 中级 | traceroute -n example.com | |
mtr <host>持续路由追踪 + 实时延迟统计 | 中级 | mtr example.com | |
ping -c <count> <host>发送指定数量的 ICMP Echo 请求 | 基础 | ping -c 5 example.com | |
ping -f <host>洪泛 ping(高频率发包,需 root 权限) | 高级 | ping -f -c 1000 target |
包捕获与分析(5)
| 命令 | 难度 | ||
|---|---|---|---|
tcpdump -i <iface>在指定网络接口上抓包 | 中级 | tcpdump -i eth0 | |
tcpdump <filter>使用 BPF 过滤表达式抓取特定流量 | 中级 | tcpdump -i eth0 port 443 or port 80 | |
tcpdump -w <file> -r <file>保存抓包到文件或读取已保存的文件 | 中级 | tcpdump -i eth0 -w capture.pcap && tcpdump -r capture.pcap | |
tcpdump -X / -A显示包内容(十六进制+ASCII 或纯 ASCII) | 高级 | tcpdump -i eth0 -X host example.com | |
tcpdump -nn抓包时不解析主机名和端口名(更快更安全) | 中级 | tcpdump -i eth0 -nn |
网络性能(5)
| 命令 | 难度 | ||
|---|---|---|---|
iperf3 -s启动 iperf3 服务端(等待客户端连接) | 中级 | iperf3 -s | |
iperf3 -c <server>iperf3 客户端测速(默认 TCP) | 中级 | iperf3 -c 10.0.0.1 | |
iperf3 -u -b <bandwidth>iperf3 UDP 测试(指定目标带宽) | 中级 | iperf3 -c 10.0.0.1 -u -b 100m | |
iperf3 -Riperf3 反向测试(服务端到客户端方向) | 中级 | iperf3 -c 10.0.0.1 -R | |
ping -M do -s <size> <host>MTU 探测(禁止分片,指定载荷大小) | 高级 | ping -M do -s 1472 target |
TLS/SSL 诊断(3)
| 命令 | 难度 | ||
|---|---|---|---|
openssl s_client -connect <host>:<port>测试 TLS 握手并查看证书、加密套件等信息 | 中级 | openssl s_client -connect example.com:443 | |
openssl s_client -showcerts显示完整的 TLS 证书链 | 高级 | openssl s_client -connect example.com:443 -showcerts | |
openssl s_client -servername <host>带 SNI 的 TLS 连接(多域名场景必需) | 高级 | openssl s_client -connect 1.2.3.4:443 -servername example.com |