Linux之traceroute命令

Posted 入门小站

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux之traceroute命令相关的知识,希望对你有一定的参考价值。

  • TCP traceroute(使用 tracetcp 程序可以实现)
  • UDP traceroute(Cisco 和 Linux 默认情况下使用)
  • ICMP traceroute ( MS Windows 默认情况下使用)

命令格式

traceroute(选项)(参数)

命令选项

  • -d:使用Socket层级的排错功能;
  • -f<存活数值>:设置第一个检测数据包的存活数值TTL的大小;
  • -F:设置勿离断位;
  • -g<网关>:设置来源路由网关,最多可设置8个;
  • -i<网络界面>:使用指定的网络界面送出数据包;
  • -I:使用ICMP回应取代UDP资料信息;
  • -m<存活数值>:设置检测数据包的最大存活数值TTL的大小;
  • -n:直接使用IP地址而非主机名称;
  • -p<通信端口>:设置UDP传输协议的通信端口;
  • -r:忽略普通的Routing Table,直接将数据包送到远端主机上。
  • -s<来源地址>:设置本地主机送出数据包的IP地址;
  • -t<服务类型>:设置检测数据包的TOS数值;
  • -v:详细显示指令的执行过程;
  • -w<超时秒数>:设置等待远端主机回报的时间;
  • -x:开启或关闭数据包的正确性检验。

命令参数

简单用法

> traceroute rumenz.com
traceroute to rumenz.com (203.195.152.245), 64 hops max, 52 byte packets
 1  192.168.0.1 (192.168.0.1)  1.242 ms  0.949 ms  2.494 ms
 2  192.168.1.1 (192.168.1.1)  1.337 ms  1.101 ms  0.953 ms
 3  100.64.0.1 (100.64.0.1)  4.663 ms  28.187 ms  3.346 ms
 4  111.175.227.145 (111.175.227.145)  7.321 ms  12.203 ms  10.503 ms
 5  111.175.210.221 (111.175.210.221)  4.810 ms  7.400 ms  3.673 ms
 6  202.97.17.5 (202.97.17.5)  20.276 ms
    202.97.98.25 (202.97.98.25)  38.174 ms
    202.97.17.1 (202.97.17.1)  28.942 ms
 7  * 113.96.5.158 (113.96.5.158)  26.277 ms *
 8  * * *
 9  14.18.199.106 (14.18.199.106)  22.518 ms  20.543 ms  22.583 ms
10  * * 14.18.199.74 (14.18.199.74)  22.378 ms
11  * * *
12  * * *
13  * * *
14  203.195.152.245 (203.195.152.245)  25.766 ms  21.599 ms *

设置跳数

> traceroute -m 10 rumenz.com
traceroute to rumenz.com (203.195.152.245), 10 hops max, 52 byte packets
 1  192.168.0.1 (192.168.0.1)  1.829 ms  0.746 ms  1.684 ms
 2  192.168.1.1 (192.168.1.1)  2.326 ms  3.365 ms  1.203 ms
 3  100.64.0.1 (100.64.0.1)  4.659 ms  5.613 ms  4.055 ms
 4  111.175.227.145 (111.175.227.145)  12.399 ms  7.991 ms  8.013 ms
 5  111.175.210.221 (111.175.210.221)  6.997 ms  10.958 ms  4.180 ms
 6  202.97.98.29 (202.97.98.29)  21.216 ms
    202.97.98.25 (202.97.98.25)  21.428 ms *
 7  * * *
 8  * * *
 9  * 14.18.199.106 (14.18.199.106)  22.443 ms  20.399 ms
10  * * *

显示IP地址,不查主机名

> traceroute -n rumenz.com
traceroute to rumenz.com (203.195.152.245), 64 hops max, 52 byte packets
 1  192.168.0.1  2.484 ms  1.271 ms  0.813 ms
 2  192.168.1.1  1.320 ms  1.103 ms  1.075 ms
 3  100.64.0.1  2.914 ms  4.107 ms  4.372 ms
 4  111.175.227.145  7.279 ms  7.915 ms  9.007 ms
 5  111.175.210.221  3.729 ms  4.100 ms  4.160 ms
 6  202.97.29.49  25.172 ms
    202.97.34.66  23.798 ms
    202.97.29.53  18.034 ms
 7  * * *
 8  * * *
 9  * * *
10  * * *
11  * * *
12  * * *
13  * * *
14  * 203.195.152.245  21.635 ms  22.201 ms

把探测包的个数设置为值4

> traceroute -q 4 rumenz.com
traceroute to rumenz.com (203.195.152.245), 64 hops max, 52 byte packets
 1  192.168.0.1 (192.168.0.1)  2.265 ms  2.010 ms  0.669 ms  1.103 ms
 2  192.168.1.1 (192.168.1.1)  1.159 ms  1.207 ms  1.082 ms  1.181 ms
 3  100.64.0.1 (100.64.0.1)  6.105 ms  4.068 ms  2.737 ms  3.858 ms
 4  111.175.227.145 (111.175.227.145)  12.785 ms  11.772 ms  11.710 ms  7.463 ms
 5  111.175.210.221 (111.175.210.221)  11.646 ms  5.033 ms  4.373 ms  5.631 ms
 6  * 202.97.29.57 (202.97.29.57)  21.332 ms *
    202.97.17.5 (202.97.17.5)  26.080 ms
 7  * * * *
 8  * * * *
 9  14.18.199.106 (14.18.199.106)  26.460 ms  20.790 ms  25.815 ms *
10  * * 14.18.199.74 (14.18.199.74)  22.642 ms *
11  * * * *
12  * * * *
13  * * * *
14  203.195.152.245 (203.195.152.245)  22.046 ms  22.201 ms  20.328 ms  24.846 ms

绕过正常的路由表,直接发送到网络相连的主机

> traceroute -r rumenz.com
traceroute to rumenz.com (203.195.152.245), 64 hops max, 52 byte packets
traceroute: sendto: Network is unreachable
 1 traceroute: wrote rumenz.com 52 chars, ret=-1
 *traceroute: sendto: Network is unreachable
traceroute: wrote rumenz.com 52 chars, ret=-1
 *traceroute: sendto: Network is unreachable
traceroute: wrote rumenz.com 52 chars, ret=-1

探测包的等待响应时间设置为3秒

> traceroute -w 3 rumenz.com
traceroute to rumenz.com (203.195.152.245), 64 hops max, 52 byte packets
 1  192.168.0.1 (192.168.0.1)  2.835 ms  0.894 ms  0.967 ms
 2  192.168.1.1 (192.168.1.1)  1.289 ms  1.174 ms  1.091 ms
 3  100.64.0.1 (100.64.0.1)  3.327 ms  2.985 ms  4.280 ms
 4  111.175.227.145 (111.175.227.145)  13.337 ms  12.301 ms  9.081 ms
 5  111.175.210.221 (111.175.210.221)  3.484 ms  5.052 ms  4.033 ms
 6  202.97.98.25 (202.97.98.25)  19.580 ms
    202.97.17.1 (202.97.17.1)  21.139 ms
    202.97.34.66 (202.97.34.66)  21.232 ms
 7  * * 113.96.5.82 (113.96.5.82)  26.111 ms
 8  * * *
 9  * 14.18.199.106 (14.18.199.106)  22.279 ms *
10  * * *
11  * * *
12  * * *
13  * * *
14  * 203.195.152.245 (203.195.152.245)  22.335 ms  21.387 ms

Traceroute的工作原理

UDP 和 ICMP Traceroute

  • UDP 向外发送的是一个 UDP 数据包,final reply 是 ICMP Destination Unreachable
  • ICMP 向外发送的是一个 ICMP Echo Request,final reply 是 ICMP Echo Reply

TCP Traceroute

原文链接:https://rumenz.com/rumenbiji/linux-traceroute.html
微信公众号:入门小站

![](https://s4.51cto.com/images/blog/202203/07141646_6225a34e3a27424674.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=

以上是关于Linux之traceroute命令的主要内容,如果未能解决你的问题,请参考以下文章

Linux 命令(202)—— traceroute 命令

每天一个linux命令:traceroute命令

Linux traceroute命令详解

(转)linux traceroute命令参数及用法详解--linux跟踪路由命令

Linux命令:traceroute(windows中为tracert)

linux 管理 网络命令相关