traceroute是Unix和类Unix系统中的一个命令,用于诊断和跟踪网络数据包从源主机到目标主机的路由路径。它通过发送一系列的Internet控制消息协议(ICMP)回显请求(ping)数据包来实现。一条路径上的每个设备traceroute要测3次。输出结果中包括每次测试的时间(ms)和设备的名称(如有的话)及其IP地址。
以下是对traceroute命令的详细解释:
1. 命令格式:
traceroute [选项] 目标主机
选项:
-I
:使用ICMP Echo请求代替UDP数据包。-T
:使用TCP SYN(同步)请求代替UDP数据包。-U
:使用UDP数据包。-q <查询数>
:指定每个跃点上发送的查询数,默认为3。-w <超时时间>
:指定等待每个回显请求的超时时间(以秒为单位),默认为5s。-m <跳数>
:指定最大跃点数,默认为30。-p
: 设置UDP传输协议的通信端口。-n
:使用此选项时,traceroute将不会尝试将IP地址解析为主机名。
功能:
traceroute命令通过发送一系列的ICMP或UDP数据包,逐跳地确定到达目标主机的路径。每个数据包都在IP头的TTL字段中具有不同的值,以便在每一跳上确定路径。当一个数据包到达跳数的上限或目标主机时,它将被丢弃,并返回信息给发送者。traceroute命令显示每个跳点的IP地址和主机名,以及在每个跳点上的往返时间(RTT)。
工作原理请参考:https://blog.csdn.net/lida2003/article/details/125383921
使用示例:
traceroute www.google.com
:跟踪到达Google的路径。traceroute -I 8.8.8.8
:使用ICMP Echo请求,跟踪到达Google DNS服务器的路径。traceroute -m 10 www.example.com
:指定最大跳数为10,跟踪到达达示例网站的路径。
[root@localhost ~]# traceroute www.baidu.com
traceroute to www.baidu.com (153.3.238.110), 30 hops max, 60 byte packets
1 gateway (192.168.159.2) 0.251 ms 0.189 ms 0.174 ms
2 * * *
3 * * *
4 * * *
5 * * *
6 * * * //记录按序列号从1开始,每个纪录就是一跳 ,每跳表示一个网关,我们看到每行有三个时间,单位是 ms,其实就是-q的默认参数。
7 * * * //探测数据包向每个网关发送三个数据包后,网关响应后返回的时间;如果您用 `traceroute -q 4 www.58.com` ,表示向每个网关发送4个数据包。
8 * * *
9 * * *
10 * * *
11 * * *
12 * * *
13 * * *
14 * * *
15 * * *
16 * * *
17 * * *
18 * * *
19 * * *
20 * * *
21 * * *
22 * * *
23 * * *
24 * * *
25 * * *
26 * * *
27 * * *
28 * * *
29 * * *
30 * * *
[root@localhost ~]#
[root@localhost ~]# traceroute 192.168.159.1
traceroute to 192.168.159.1 (192.168.159.1), 30 hops max, 60 byte packets
1 192.168.159.1 (192.168.159.1) 1.689 ms * *
[root@localhost ~]#
[root@localhost ~]# traceroute 192.17.323.12
192.17.323.12: 未知的名称或服务
Cannot handle "host" cmdline arg `192.17.323.12' on position 1 (argc 1)
[root@localhost ~]#
traceroute -n www.baidu.com
:显示IP地址,不查主机名traceroute -p 6888 www.baidu.com
:探测包使用的基本UDP端口设置6888traceroute -q 4 www.baidu.com
:把探测包的个数设置为值4
[root@localhost ~]# traceroute -q 4 www.baidu.com
traceroute to www.baidu.com (153.3.238.102), 30 hops max, 60 byte packets
1 gateway (192.168.159.2) 0.228 ms 0.269 ms 0.215 ms 0.244 ms
2 * * * *
...
traceroute -r www.baidu.com
:绕过正常的路由表,直接发送到网络相连的主机
[root@localhost ~]# traceroute -r www.baidu.com
traceroute to www.baidu.com (153.3.238.102), 30 hops max, 60 byte packets
connect: 网络不可达
[root@localhost ~]#
traceroute -w 3 www.baidu.com
:把对外发探测包的等待响应时间设置为3秒
traceroute命令对于网络故障排除和帮助确定网络延迟问题非常有用。通过观察路径中的跳点,可以了解到达目标主机所经过的网络节点,并判断网络连接质量以及延迟情况。