Linux之traceroute命令

时人不识凌云木,直待凌云始道高。这篇文章主要讲述Linux之traceroute命令相关的知识,希望能为你提供帮助。

  • TCP traceroute(使用 tracetcp 程序可以实现)
  • UDP traceroute(Cisco 和 Linux 默认情况下使用)
  • ICMP traceroute ( MS Windows 默认情况下使用)
命令格式
【Linux之traceroute命令】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 1192.168.0.1 (192.168.0.1)1.242 ms0.949 ms2.494 ms 2192.168.1.1 (192.168.1.1)1.337 ms1.101 ms0.953 ms 3100.64.0.1 (100.64.0.1)4.663 ms28.187 ms3.346 ms 4111.175.227.145 (111.175.227.145)7.321 ms12.203 ms10.503 ms 5111.175.210.221 (111.175.210.221)4.810 ms7.400 ms3.673 ms 6202.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* * * 914.18.199.106 (14.18.199.106)22.518 ms20.543 ms22.583 ms 10* * 14.18.199.74 (14.18.199.74)22.378 ms 11* * * 12* * * 13* * * 14203.195.152.245 (203.195.152.245)25.766 ms21.599 ms *

设置跳数
> traceroute -m 10 rumenz.com traceroute to rumenz.com (203.195.152.245), 10 hops max, 52 byte packets 1192.168.0.1 (192.168.0.1)1.829 ms0.746 ms1.684 ms 2192.168.1.1 (192.168.1.1)2.326 ms3.365 ms1.203 ms 3100.64.0.1 (100.64.0.1)4.659 ms5.613 ms4.055 ms 4111.175.227.145 (111.175.227.145)12.399 ms7.991 ms8.013 ms 5111.175.210.221 (111.175.210.221)6.997 ms10.958 ms4.180 ms 6202.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 ms20.399 ms 10* * *

显示IP地址,不查主机名
> traceroute -n rumenz.com traceroute to rumenz.com (203.195.152.245), 64 hops max, 52 byte packets 1192.168.0.12.484 ms1.271 ms0.813 ms 2192.168.1.11.320 ms1.103 ms1.075 ms 3100.64.0.12.914 ms4.107 ms4.372 ms 4111.175.227.1457.279 ms7.915 ms9.007 ms 5111.175.210.2213.729 ms4.100 ms4.160 ms 6202.97.29.4925.172 ms 202.97.34.6623.798 ms 202.97.29.5318.034 ms 7* * * 8* * * 9* * * 10* * * 11* * * 12* * * 13* * * 14* 203.195.152.24521.635 ms22.201 ms

把探测包的个数设置为值4
> traceroute -q 4 rumenz.com traceroute to rumenz.com (203.195.152.245), 64 hops max, 52 byte packets 1192.168.0.1 (192.168.0.1)2.265 ms2.010 ms0.669 ms1.103 ms 2192.168.1.1 (192.168.1.1)1.159 ms1.207 ms1.082 ms1.181 ms 3100.64.0.1 (100.64.0.1)6.105 ms4.068 ms2.737 ms3.858 ms 4111.175.227.145 (111.175.227.145)12.785 ms11.772 ms11.710 ms7.463 ms 5111.175.210.221 (111.175.210.221)11.646 ms5.033 ms4.373 ms5.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* * * * 914.18.199.106 (14.18.199.106)26.460 ms20.790 ms25.815 ms * 10* * 14.18.199.74 (14.18.199.74)22.642 ms * 11* * * * 12* * * * 13* * * * 14203.195.152.245 (203.195.152.245)22.046 ms22.201 ms20.328 ms24.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 1192.168.0.1 (192.168.0.1)2.835 ms0.894 ms0.967 ms 2192.168.1.1 (192.168.1.1)1.289 ms1.174 ms1.091 ms 3100.64.0.1 (100.64.0.1)3.327 ms2.985 ms4.280 ms 4111.175.227.145 (111.175.227.145)13.337 ms12.301 ms9.081 ms 5111.175.210.221 (111.175.210.221)3.484 ms5.052 ms4.033 ms 6202.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 ms21.387 ms

Traceroute的工作原理
UDP 和 ICMP Traceroute
Linux之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=

    推荐阅读