Home  >  Article  >  System Tutorial  >  My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

王林
王林forward
2024-02-09 19:00:211251browse

I. Introduction

As a network engineer or operation and maintenance engineer, the traceroute command will be familiar to you. Its function is similar to the ping command, which is used to diagnose network connectivity. However, the commands output by the traceroute command will be much richer than the ping command. You can track the commands from The path from the source system to the target system.

Many engineers only use the traceroute command for basic purposes, but in actual combat, basic operations cannot solve the problem. In this article, I will introduce you to several examples of how to use the traceroute command so that you can fully master the traceroute command. Let’s get started!

2. Prerequisite knowledge

Before formally introducing the use of commands, please take a look at the following topology diagram:

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

As shown in the figure, if the computer wants to access the server, whether it should go to Computer->R1->R2->R4->Server or Computer->R1-> R3->R4->Server, this can be achieved through the traceroute command, which is also the value of the traceroute command.

3. Install the traceroute command in Linux

First we use the command lsb_release -a to see what release version our server is:

root@ecs-adf0-0003:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:        20.04
Codename:       focal

As can be seen from the output, our system is an Ubuntu system, so the installation traceroute command is as follows:

apt install traceroute

Installation process:

If your Linux system distribution is CentOS or Fedora, you can use the yum install traceroute command.

4. Using the traceroute command in Linux

4.1 Basic usage

The most basic usage of the traceroute command is:

traceroute host

After the command is executed, the list of routers required to reach the destination will be displayed.

For example we traceroute our blog site:

traceroute www.wljslmz.cn

Results of the:

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

如图所示,我们执行完traceroute www.wljslmz.cn命令后,看到从我的ecs服务器到达www.wljslmz.cn所在服务器一共经历了20台路由器,每台路由器响应的时间也都打印下来了。

4.2 更改等待时间

traceroute 中的默认等待时间是 3 秒,这个意思就是,假如有20台路由器,每经历一台路由器默认等待时间是3秒,如果我们想改变这个值,可以加一个参数-w

traceroute -w 1 www.wljslmz.cn

执行结果:

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

4.3 更改收发数据包的数量

默认情况下,traceroute 一次发送 3 个包,假如想更改这个数量,可以使用-q参数,比如我现在把它改成5个包:

traceroute -q 5 www.wljslmz.cn

执行结果:

由此可见,现在每台路由器收发就变成了5个包了。

4.4 更改最大跳数

所谓跳数就是一次请求经历的路由器的数量,还是这张图:

比如流量走向是电脑->R1->R2->R4->服务器,那么其跳数是3.

traceroute 中默认单次查询的最大跳数是 30,可以扩展到 255。

如果想要更改这个参数,可以加上-m参数。

我们上面执行的跳数都是20,我们为了显示出效果,将这个值改为10:

traceroute -m 10 www.wljslmz.cn

执行结果:

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

可以看到最大跳数改为10以后,原先经历20台路由器,现在输出显示只有10台,参数生效了。

4.5 更改TTL开始值

首先给大家解释一下什么是TTL。

TTL英文全称:Time To Live,翻译成中文就是生存时间,是网络技术中比较常见的专业术语。

如果你使用过ping命令,那么TTL经常会看到:

root@ecs-adf0-0003:~# ping www.wljslmz.cn

执行结果:

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

TTL,专业点解释就是用于限制数据包在 Internet 传输系统中“存活”的时间,或者说数据包在被丢弃之前在网络中的时间限制。

如果你还是不明白,你就把TTL看作是跳数限制,目的是防止数据包在网络中永远流通,最大 TTL 值为 255。

数据包的 TTL 字段由发送方设置,并由到达目的地的路径上的每个路由器减少,路由器在转发 IP 数据包时将 TTL 值减一,当数据包 TTL 值达到 0 时,路由器将其丢弃并向始发主机发送回ICMP 消息。

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

这就是TTL的全部解释了,如果还想更深入的了解TTL的底层原理,瑞哥可以后期专门出一篇文章进行解释。

默认情况下,traceroute 将从第一个 TTL 开始,如果想要更改这个值,我们可以使用-f参数实现:

traceroute -f 8 www.wljslmz.cn

执行结果:

可以看到我们这里是从第8个TTL开始的。

4.6 禁用主机名到 IP 地址的映射

我们在执行traceroute www.wljslmz.cn命令时,注意箭头所指的位置,会有域名的出现:

现在是因为域名比较少,假如你跟踪的服务器中间路由器涉及到的域名比较多,那么是不便于排查问题的,所以我们需要去除主机名到 IP 地址的映射,可以使用-n参数:

traceroute -n www.wljslmz.cn

执行结果:

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

可以看到已经没有域名了。

4.7 更改跟踪路由目的端口

先问大家可以问题:traceroute www.wljslmz.cn这条命令跟踪的是什么端口?

答案是80端口,如果我们想更改这个目的端口,怎么办?

可以使用-p参数,比如改成跟踪其2222端口:

traceroute -p 2222 www.wljslmz.cn

执行结果:

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

4.8 更改最大数据包大小

默认情况下,最大数据包为60字节,如下图箭头所示:

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

如果我们觉得跟踪的路由器数量比较多,一看就不止60字节,这个时候想调大,可以这样操作:

traceroute www.wljslmz.cn 200

执行结果:

可以看到直接在命令后加入数字即可。

我们尝试执行一下这条命令:

traceroute www.wljslmz.cn 1

猜一下执行结果中,最大数据包大小是多少?

1?

当然不是!

我们看到最终的大小是28字节,也就是说这条命令只能用来限制最大大小,而不是设置多大,包就被过滤了,28字节是跟踪包的最小大小,你设置的值小于这个大小,那么不管用!

4.9 启用ipv4跟踪

traceroute -4 www.wljslmz.cn

执行结果:

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

4.10 启用ipv6跟踪

traceroute -6 www.wljslmz.cn

执行结果:

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

说明我们的网站未支持ipv6.

4.11 使用ICMP ECHO

默认情况下,traceroute 命令使用 UDP 端口进行跟踪路由,要使用 ICMP ECHO,可以结合参数-I

traceroute -I www.wljslmz.cn

执行结果:

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

4.12 将traceroute信息保存到文件

这个跟traceroute命令本身没有太大关系,有时候我们需要将traceroute的信息离线分析,这个时候就需要将traceroute返回信息持久化,我们可以使用以下命令:

traceroute  www.wljslmz.cn > wljslmz.txt

执行结果:

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

我们通过命令more wljslmz.txt来看下这个wljslmz.txt文件:

My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!

五、总结

traceroute 命令是一个有用且易于运行的网络诊断工具,本文给大家介绍了12个traceroute 命令示例,希望本文能够对您使用traceroute 命令有所帮助,如果有问题可以在下方评论区与我讨论!

The above is the detailed content of My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete