网络调试指南
ping — 连通性测试
# 基本 ping
ping google.com
# 限制次数
ping -c 4 google.com
# 指定间隔(0.2 秒)
ping -i 0.2 -c 10 8.8.8.8
# 指定包大小(测试 MTU)
ping -s 1472 -c 3 10.0.0.1
traceroute — 路由追踪
# Linux/macOS
traceroute google.com
# 使用 ICMP
traceroute -I google.com
# 使用 TCP 端口 80
traceroute -T -p 80 google.com
# Windows
tracert google.com
# mtr(更好的 traceroute)
mtr --report --report-cycles 10 google.com
nslookup / dig — DNS 查询
# 基本查询
nslookup example.com
# 查询特定记录类型
nslookup -type=MX example.com
nslookup -type=TXT example.com
# dig(更详细)
dig example.com
dig example.com MX
dig example.com NS +short
# 反向 DNS
dig -x 8.8.8.8
# 指定 DNS 服务器
dig @8.8.8.8 example.com
# 追踪完整解析过程
dig +trace example.com
curl — HTTP 调试
# 显示请求响应详情
curl -v https://example.com
# 仅显示响应头
curl -I https://example.com
# 显示各阶段耗时
curl -w "DNS:%{time_namelookup} Connect:%{time_connect} Total:%{time_total}\n" \
-o /dev/null -s https://example.com
# POST JSON 请求
curl -X POST https://api.example.com/data \
-H "Content-Type: application/json" \
-d '{"key":"value"}'
netstat / ss — 连接状态
# 列出监听端口
netstat -tlnp # Linux
ss -tlnp # 现代替代命令
# 全部连接
ss -an
# 按端口过滤
ss -tnp sport = :80
# 已建立的连接
ss -tn state established
tcpdump — 抓包
# 在 eth0 接口抓包
tcpdump -i eth0
# 过滤主机
tcpdump -i eth0 host 10.0.0.1
# 过滤端口
tcpdump -i eth0 port 80
# 保存到文件
tcpdump -i eth0 -w capture.pcap
# 读取抓包文件
tcpdump -r capture.pcap
快速诊断清单
| 症状 | 命令 |
|---|---|
| 无法连接主机 | ping host → traceroute host |
| DNS 无法解析 | dig host → dig @8.8.8.8 host |
| 端口不可访问 | nc -zv host port |
| 延迟高 | mtr host 找到瓶颈节点 |
| HTTP 响应慢 | curl -w time_total ... |
| 丢包严重 | ping -c 100 host 观察丢包率 |