linux-sre-handbook

04-网络排查工具链

排查层次

应用层问题      → curl, telnet, openssl s_client
传输层问题      → ss, nc, tcpdump
网络层问题      → ping, traceroute, ip route
链路/物理层     → ethtool, ip link, iwconfig

核心工具

ping — 连通性测试

ping -c 4 example.com           # 发 4 个包
ping -i 0.2 example.com         # 间隔 0.2 秒
ping -s 1400 example.com        # 指定包大小 (测试 MTU)
ping -W 2 example.com           # 超时 2 秒

traceroute / mtr — 路径追踪

traceroute example.com          # UDP 方式
traceroute -T example.com       # TCP SYN 方式
traceroute -n example.com       # 不解析 DNS
mtr example.com                 # 实时路径 + 丢包率
mtr -r -c 10 example.com        # 报告模式

ss — Socket 统计

ss -tlnp             # TCP 监听端口
ss -unp              # UDP 监听
ss -s                # 汇总统计
ss -tan state established | wc -l  # 已建立连接数
ss -tan state time-wait           # TIME_WAIT 连接

nc (netcat) — 网络瑞士军刀

nc -zv host port           # 扫描端口
nc -l 8080                 # 监听端口
nc host port < file.txt    # 发送文件

tcpdump — 抓包分析

tcpdump -i eth0 port 80                    # 指定接口和端口
tcpdump -i any host 10.0.0.1               # 指定主机
tcpdump -i eth0 -w capture.pcap            # 写入文件
tcpdump -r capture.pcap                    # 读取文件
tcpdump -i eth0 'tcp[tcpflags] & tcp-syn != 0'  # 只看 SYN
tcpdump -i eth0 -s 0 -w /tmp/cap.pcap      # 完整包

HTTP/API 调试

# curl — 查看请求/响应头
curl -v https://api.example.com
curl -I https://api.example.com            # 只看响应头
curl -w "\n%{http_code}\n%{time_total}s\n" https://api.example.com

# 详细计时
curl -w "DNS: %{time_namelookup}s\nConnect: %{time_connect}s\n
         TLS: %{time_appconnect}s\nTTFB: %{time_starttransfer}s\n
         Total: %{time_total}s\n" https://api.example.com

# 测试 SSL
openssl s_client -connect example.com:443 -servername example.com
openssl s_client -connect example.com:443 -showcerts
echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates  # 证书过期时间

故障排查决策树

连接不通?
├─ DNS 解析正常? → dig, nslookup
├─ 路由可达?     → ping, traceroute
├─ 端口开放?     → nc -zv, telnet
├─ 防火墙阻挡?   → iptables -L -n
├─ 服务正常?     → curl 本地检查
└─ 应用日志?     → tail -f / journalctl

延伸阅读