


My boss asked me to use traceroute in Linux to troubleshoot server network problems. Fortunately, I saved this article!
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:

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:

如图所示,我们执行完traceroute www.wljslmz.cn
命令后,看到从我的ecs服务器到达www.wljslmz.cn
所在服务器一共经历了20台路由器,每台路由器响应的时间也都打印下来了。
4.2 更改等待时间
traceroute 中的默认等待时间是 3 秒,这个意思就是,假如有20台路由器,每经历一台路由器默认等待时间是3秒,如果我们想改变这个值,可以加一个参数-w
:
traceroute -w 1 www.wljslmz.cn
执行结果:

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
执行结果:

可以看到最大跳数改为10以后,原先经历20台路由器,现在输出显示只有10台,参数生效了。
4.5 更改TTL开始值
首先给大家解释一下什么是TTL。
TTL英文全称:Time To Live,翻译成中文就是生存时间,是网络技术中比较常见的专业术语。
如果你使用过ping命令,那么TTL经常会看到:
root@ecs-adf0-0003:~# ping www.wljslmz.cn
执行结果:

TTL,专业点解释就是用于限制数据包在 Internet 传输系统中“存活”的时间,或者说数据包在被丢弃之前在网络中的时间限制。
如果你还是不明白,你就把TTL看作是跳数限制,目的是防止数据包在网络中永远流通,最大 TTL 值为 255。
数据包的 TTL 字段由发送方设置,并由到达目的地的路径上的每个路由器减少,路由器在转发 IP 数据包时将 TTL 值减一,当数据包 TTL 值达到 0 时,路由器将其丢弃并向始发主机发送回ICMP 消息。

这就是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
执行结果:

可以看到已经没有域名了。
4.7 更改跟踪路由目的端口
先问大家可以问题:traceroute www.wljslmz.cn
这条命令跟踪的是什么端口?
答案是80端口,如果我们想更改这个目的端口,怎么办?
可以使用-p
参数,比如改成跟踪其2222端口:
traceroute -p 2222 www.wljslmz.cn
执行结果:

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

如果我们觉得跟踪的路由器数量比较多,一看就不止60字节,这个时候想调大,可以这样操作:
traceroute www.wljslmz.cn 200
执行结果:
可以看到直接在命令后加入数字即可。
我们尝试执行一下这条命令:
traceroute www.wljslmz.cn 1
猜一下执行结果中,最大数据包大小是多少?
1?
当然不是!
我们看到最终的大小是28字节,也就是说这条命令只能用来限制最大大小,而不是设置多大,包就被过滤了,28字节是跟踪包的最小大小,你设置的值小于这个大小,那么不管用!
4.9 启用ipv4跟踪
traceroute -4 www.wljslmz.cn
执行结果:

4.10 启用ipv6跟踪
traceroute -6 www.wljslmz.cn
执行结果:

说明我们的网站未支持ipv6.
4.11 使用ICMP ECHO
默认情况下,traceroute 命令使用 UDP 端口进行跟踪路由,要使用 ICMP ECHO,可以结合参数-I
:
traceroute -I www.wljslmz.cn
执行结果:

4.12 将traceroute信息保存到文件
这个跟traceroute命令本身没有太大关系,有时候我们需要将traceroute的信息离线分析,这个时候就需要将traceroute返回信息持久化,我们可以使用以下命令:
traceroute www.wljslmz.cn > wljslmz.txt
执行结果:

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

五、总结
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!

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools