traceroute 通过发送具有不同生存时间(TTL)值的探测数据包来确定数据包到达目标主机所经过的路由器路径。它可以显示每个中间节点的延迟和响应信息,帮助诊断网络连接问题。
traceroute [选项] 目标主机 [数据包长度]
参数说明:
目标主机 - 目标主机的域名或IP地址数据包长度 - 发送数据包的大小(字节,可选)| 选项 | 说明 |
|---|---|
-4 |
强制使用IPv4 |
-6 |
强制使用IPv6 |
-f 首次TTL |
设置起始TTL值 |
-m 最大TTL |
设置最大TTL值(默认30) |
-n |
不进行域名解析,显示IP地址 |
-p 端口 |
设置目标端口(UDP模式) |
-q 次数 |
设置每跳的探测包数量(默认3) |
-w 超时 |
设置等待回复的超时时间(秒) |
-I |
使用ICMP Echo请求(默认使用UDP) |
-T |
使用TCP SYN包 |
-i 接口 |
指定网络接口 |
-v |
显示详细输出 |
-z 延迟 |
设置探测包之间的延迟(秒) |
# 追踪到Google DNS服务器的路由路径
traceroute 8.8.8.8
# 输出示例:
# traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
# 1 router.local (192.168.1.1) 1.234 ms 1.456 ms 1.678 ms
# 2 10.0.0.1 (10.0.0.1) 5.432 ms 5.654 ms 5.876 ms
# 3 203.0.113.1 (203.0.113.1) 10.123 ms 10.345 ms 10.567 ms
# 4 72.14.205.1 (72.14.205.1) 15.432 ms 15.654 ms 15.876 ms
# 5 216.239.46.1 (216.239.46.1) 20.123 ms 20.345 ms 20.567 ms
# 6 google-public-dns-a.google.com (8.8.8.8) 25.432 ms 25.654 ms 25.876 ms
# 显示IP地址而不是域名
traceroute -n 8.8.8.8
# 输出示例:
# traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
# 1 192.168.1.1 1.234 ms 1.456 ms 1.678 ms
# 2 10.0.0.1 5.432 ms 5.654 ms 5.876 ms
# 3 203.0.113.1 10.123 ms 10.345 ms 10.567 ms
# 4 72.14.205.1 15.432 ms 15.654 ms 15.876 ms
# 5 216.239.46.1 20.123 ms 20.345 ms 20.567 ms
# 6 8.8.8.8 25.432 ms 25.654 ms 25.876 ms
# 设置起始TTL为5,最大TTL为15
traceroute -f 5 -m 15 google.com
# 只追踪特定跳数的路由
traceroute -f 10 -m 12 google.com
# 使用ICMP Echo请求(某些网络可能阻止UDP)
traceroute -I google.com
# 使用TCP SYN包(绕过某些防火墙)
traceroute -T google.com
# 每跳发送5个探测包
traceroute -q 5 google.com
# 设置等待回复超时为2秒
traceroute -w 2 google.com
# 设置探测包之间的延迟为0.5秒
traceroute -z 0.5 google.com
# 通过特定网络接口发送探测包
traceroute -i eth0 google.com
# 通过无线接口
traceroute -i wlan0 google.com
| 跳数 | 从源主机到该路由器的跳数(经过的路由器数量) |
| 路由器地址 | 路由器的域名或IP地址 |
| 响应时间 | 三个时间值表示三个探测包的往返时间(RTT),单位毫秒 |
| * | 表示该探测包未收到回复(超时) |
| !N | ICMP网络不可达(N可以是0-15,表示不同原因) |
| !H | ICMP主机不可达 |
| !P | ICMP协议不可达 |
| !X | ICMP通信被管理员禁止 |
| !S | 源路由失败 |
# 典型traceroute输出示例
traceroute to example.com (93.184.216.34), 30 hops max, 60 byte packets
1 router (192.168.1.1) 1.2 ms 1.3 ms 1.4 ms
2 10.10.0.1 (10.10.0.1) 5.6 ms 5.7 ms 5.8 ms
3 * * *
4 border1.example.net (203.0.113.1) 15.2 ms 15.3 ms 15.4 ms
5 core1.example.net (198.51.100.1) 20.1 ms 20.2 ms 20.3 ms
6 93.184.216.34 (93.184.216.34) 25.0 ms 25.1 ms 25.2 ms
解读:
可能原因: 路由器配置为不响应ICMP/UDP包
解决方案:
# 1. 使用不同的协议
traceroute -I google.com # 使用ICMP
traceroute -T google.com # 使用TCP
# 2. 增加超时时间
traceroute -w 5 google.com
# 3. 增加每跳的探测包数量
traceroute -q 10 google.com
# 4. 尝试不同的端口
traceroute -p 443 google.com
常见ICMP错误代码:
# !H - 主机不可达 (Host Unreachable)
# !N - 网络不可达 (Network Unreachable)
# !P - 协议不可达 (Protocol Unreachable)
# !S - 源路由失败 (Source Route Failed)
# !F - 需要分片但设置了DF标志 (Fragmentation Needed)
# !X - 通信被管理员禁止 (Communication Administratively Prohibited)
# 解决方案:
# 1. 检查目标主机是否在线
ping google.com
# 2. 检查本地网络配置
ip route show
# 3. 尝试不同的目标主机
traceroute 8.8.8.8
# 4. 检查防火墙规则
sudo iptables -L -n
可能原因: 路由环路或防火墙阻止
解决方案:
# 1. 检查是否出现路由环路(相同的IP重复出现)
# 示例输出:
# 10 203.0.113.1 15.2 ms 15.3 ms 15.4 ms
# 11 198.51.100.1 20.1 ms 20.2 ms 20.3 ms
# 12 203.0.113.1 15.2 ms 15.3 ms 15.4 ms # 回到之前的节点!
# 2. 设置最大TTL限制
traceroute -m 20 google.com
# 3. 从不同位置进行追踪,比较结果
# 使用在线traceroute工具进行对比
# 4. 联系网络管理员报告路由问题
# 先ping测试基本连通性
ping -c 5 google.com
# 如果有问题,再用traceroute诊断
traceroute google.com
# 或者使用mtr(My TraceRoute)工具
mtr google.com
# 追踪到Web服务器80端口的路由
traceroute -T -p 80 example.com
# 追踪到HTTPS服务器443端口的路由
traceroute -T -p 443 example.com
# 使用脚本批量追踪
for host in google.com yahoo.com bing.com; do
echo "=== Tracing route to $host ==="
traceroute -n $host
echo ""
done
# 将结果保存到文件
traceroute google.com > google_trace.txt
traceroute yahoo.com > yahoo_trace.txt
# 安装tcptraceroute
sudo apt install tcptraceroute # Debian/Ubuntu
sudo yum install tcptraceroute # RHEL/CentOS
# 使用tcptraceroute(更强大的TCP追踪)
tcptraceroute google.com 80
tcptraceroute -n -p 443 google.com
| 工具 | 描述 | 特点 |
|---|---|---|
traceroute |
标准路由追踪工具 | 使用UDP/ICMP,默认安装 |
mtr |
My TraceRoute | 结合ping和traceroute,实时更新 |
tracepath |
简单路由追踪 | 不需要root权限,使用UDP |
tcptraceroute |
TCP路由追踪 | 使用TCP SYN包,绕过防火墙 |
lft |
Layer Four Traceroute | 支持多种协议,功能丰富 |
hping3 |
网络探测工具 | 自定义数据包,安全测试 |
当用户无法访问某个网站时,使用traceroute确定问题出现在哪个网络节点。
识别网络延迟的来源,优化服务器部署位置,减少网络跳数和延迟。
检查数据包的路由路径,确保敏感数据不经过不安全的网络区域。
-T)traceroute6命令-T, -I)可能需要root权限ping测试网络连通性
mtr实时路由追踪
netstat网络连接状态
ip网络配置工具