🔍 执行 MTR/Traceroute 诊断
本指南将教您如何使用 MTR(My Traceroute)和 Traceroute 诊断网络连接问题、识别延迟点并分析网络连接质量。
📋 先决条件
- 具有 root 或 sudo 访问权限的 VPS 服务器
- 活动的 SSH 连接
- Ubuntu/Debian(命令适用于这些发行版)
🔧 安装
安装 Traceroute(基本工具)
# 在 Ubuntu/Debian 上 sudo apt update sudo apt install traceroute -y
安装 MTR(高级工具)
MTR 将 ping 和 traceroute 的功能组合到一个强大的工具中。
# 在 Ubuntu/Debian 上 sudo apt update sudo apt install mtr-tiny -y # 完整版本,带图形界面(可选) sudo apt install mtr -y
验证安装
# 验证 traceroute 已安装 traceroute --version # 验证 MTR 已安装 mtr --version
🛠️ 使用 Traceroute
基本命令
# 追踪到目标 traceroute google.com # 追踪到 IP traceroute 8.8.8.8 # 限制跳数(默认 30) traceroute -m 20 google.com # 指定超时(秒) traceroute -w 5 google.com
有用的选项
# 使用 UDP(默认) traceroute -U google.com # 使用 TCP(更可靠) traceroute -T google.com # 使用 ICMP(如 ping) traceroute -I google.com # 不解析 DNS 名称(更快) traceroute -n google.com
示例输出
traceroute to google.com (142.250.185.14), 30 hops max, 60 byte packets
1 10.0.0.1 (10.0.0.1) 0.123 ms 0.089 ms 0.067 ms
2 192.168.1.1 (192.168.1.1) 5.234 ms 5.189 ms 5.145 ms
3 * * *
4 8.8.8.8 (8.8.8.8) 12.456 ms 12.389 ms 12.334 ms
...
🚀 使用 MTR(推荐)
MTR 更强大,因为它持续发送数据包并实时计算统计信息。
基本命令
# MTR 交互模式(推荐) mtr google.com # MTR 报告模式(非交互) mtr --report google.com # MTR 使用 10 个测试数据包 mtr --report --report-cycles 10 google.com # MTR 到 IP mtr --report 8.8.8.8
高级选项
# 指定要发送的数据包数量 mtr --report --report-cycles 50 google.com # 使用 TCP 代替 ICMP mtr --tcp --port 80 google.com # 使用 UDP mtr --udp --port 53 8.8.8.8 # 不解析 DNS 名称 mtr --no-dns google.com # 数据包之间的间隔(秒) mtr --interval 2 google.com # 数据包大小(字节) mtr --psize 64 google.com
MTR 交互模式
当您在没有 --report 选项的情况下启动 mtr 时,您进入交互模式:
- d : 显示/隐藏详细统计信息
- n : 启用/禁用 DNS 模式(名称解析)
- r : 重置统计信息
- s : 更改数据包大小
- j : 更改数据包之间的间隔
- q : 退出 MTR
📊 解释结果
正常延迟
- < 50 ms : 优秀,本地网络或非常接近
- 50-100 ms : 良好,区域连接
- 100-200 ms : 可接受,洲际连接
- > 200 ms : 高,可能导致问题
数据包丢失
- 0% : 完美
- < 1% : 互联网连接正常
- 1-5% : 可接受,但可能导致问题
- > 5% : 有问题,需要调查
星号 (*) 的含义
在 traceroute 中,* 表示:
- 单个 * 代替时间 : 路由器未响应该特定数据包
- *三个连续的 's : 路由器完全没有响应(超时或防火墙过滤)
🎯 用例
诊断高延迟
# 测试到 Google DNS mtr --report --report-cycles 30 8.8.8.8 # 测试到 Cloudflare DNS mtr --report --report-cycles 30 1.1.1.1 # 识别有问题的跳 mtr --report --report-cycles 50 your-domain.com
诊断数据包丢失
# 使用 TCP 测试以绕过 ICMP 过滤器 mtr --tcp --port 443 --report --report-cycles 50 google.com # 测试到特定服务器 mtr --report --report-cycles 100 your-server.com
从您的 VPS 测试到您的位置
# 测试到您的公共 IP 地址 # 首先获取您的公共 IP curl ifconfig.me # 然后从您的 VPS 测试 mtr --report --report-cycles 30 YOUR_PUBLIC_IP
🔍 故障排除常见问题
问题:特定跳上的高延迟
如果您看到中间跳上的高延迟:
# 使用不同协议测试 mtr --tcp --port 443 --report google.com mtr --udp --port 53 --report 8.8.8.8 mtr --report google.com
Important: 中间跳上的高延迟可能是正常的,如果该路由器不对诊断请求快速响应,但最终延迟仍然良好。
问题:显著数据包丢失
如果您看到高数据包丢失:
# 使用更多数据包测试以确认 mtr --report --report-cycles 100 destination.com # 如果 ICMP 被过滤,使用 TCP 测试 mtr --tcp --port 80 --report --report-cycles 50 destination.com
Warning: 如果所有跳上都出现数据包丢失,问题可能出在您的 VPS 或本地连接上。如果仅在特定跳上出现,问题出在您的 VPS 和目标之间的网络上。
问题:重复超时
# 增加超时 mtr --timeout 10 --report destination.com # 使用不同协议测试 mtr --tcp --port 443 --timeout 10 --report destination.com
📝 有用命令
完整诊断脚本
创建脚本以测试多个目标:
#!/bin/bash echo "=== MTR 测试到 Google DNS ===" mtr --report --report-cycles 30 8.8.8.8 echo -e "\n=== MTR 测试到 Cloudflare DNS ===" mtr --report --report-cycles 30 1.1.1.1 echo -e "\n=== MTR 测试到您的域名 ===" mtr --report --report-cycles 30 your-domain.com
保存结果
# 将报告保存到文件 mtr --report --report-cycles 30 google.com > mtr-report-$(date +%Y%m%d).txt # CSV 格式 mtr --report --report-cycles 30 --csv google.com > mtr-report.csv
✅ 最佳实践
- 使用 MTR 而不是 traceroute : MTR 提供更完整的统计信息
- 测试多个目标 : 比较不同服务器的结果
- 进行多次测试 : 网络可能会变化,在不同时间运行多次测试
- 如果 ICMP 被过滤,使用 TCP : 某些路由器过滤 ICMP 但不过滤 TCP
- 正确解释结果 : 中间跳上的高延迟不总是问题,如果最终延迟良好
🆘 故障排除
MTR 无法正确显示
# 如果 MTR 在交互模式下不起作用,使用报告模式 mtr --report destination.com # 检查 MTR 版本 mtr --version
错误 "mtr: command not found"
# 重新安装 MTR sudo apt update sudo apt install mtr-tiny -y # 或安装完整版本 sudo apt install mtr -y
所有结果显示星号
如果所有跳都显示 *,可能意味着:
# 您的防火墙正在阻止 ICMP # 改用 TCP 测试 mtr --tcp --port 443 --report destination.com # 检查防火墙规则 sudo ufw status sudo iptables -L
📚 其他资源
❓ 常见问题
问:Traceroute 和 MTR 有什么区别?
答:Traceroute 发送几个数据包并显示结果。MTR 持续发送数据包并实时计算统计信息,这对诊断更有用。
问:为什么某些跳显示星号?
答:这意味着路由器没有响应。这可能是由于防火墙过滤、路由器配置或路由器不对诊断请求响应。
问:我应该担心中间跳上的高延迟吗?
答:不一定。如果到目标的最终延迟良好,中间跳上的高延迟可能是正常的。真正重要的是最终延迟。
问:如何解释 MTR 中的数据包丢失?
答:数据包丢失 < 1% 是正常的。在 1-5% 之间,是可接受的,但可能导致问题。超过 5%,是有问题的,需要调查。