Linux mtr Command

Linux 命令大全Linux Command Encyclopedia


mtr (My Traceroute) is a combination oftracerouteandpingthe functions of a powerful network diagnostic tool. It can display the transmission path of data packets in the network in real time and provide detailed statistical information. It is a powerful tool for network administrators and developers to troubleshoot network problems.

Main Features of mtr

  • Real-time updates: Continuously display routing and latency information
  • Bidirectional diagnosis: Can display paths in both sending and receiving directions simultaneously
  • Comprehensive statistics: Provides key metrics such as packet loss rate and latency
  • Visual interface: Interactively displays network path status

Installing the mtr Command

Installing on Common Linux Distributions

Example

# Debian/Ubuntu systems
sudo apt-get install mtr

# CentOS/RHEL systems
sudo yum install mtr

# Fedora systems
sudo dnf install mtr

# Arch Linux systems
sudo pacman -S mtr

Installing on macOS

Example

# Install using Homebrew
brew install mtr

mtr Basic Syntax

The basic syntax format of the mtr command is as follows:

mtr [选项] 目标主机

Common Options and Parameters Description

Option Description
-4 Force use of IPv4
-6 Force use of IPv6
-c COUNT Set the number of ping packets sent
-i SECONDS Set the ping interval time (seconds)
-n Do not resolve hostnames, display IP addresses
-r Generate report mode (non-interactive)
-s BYTES Set the ping packet size (bytes)
-w Wide output mode, display full hostnames
-z Display AS (Autonomous System) numbers
--report Equivalent to-r, exits after generating the report
--report-wide Wide format report

mtr Usage Examples

Basic Usage: Diagnose the network path to a target host

mtr google.com

After execution, a real-time updated interface will be displayed, containing the following information:

  • Hop count (Hop)
  • Hostname or IP address
  • Packet loss rate (Loss%)
  • Latest latency (Last)
  • Average latency (Avg)
  • Best latency (Best)
  • Worst latency (Wrst)
  • Standard deviation (StDev)

Generate report mode (suitable for script invocation)

mtr -r -c 10 google.com > mtr_report.txt

This command will send 10 ping packets to google.com, and then save the results to the mtr_report.txt file.

Set ping packet size and interval

mtr -s 100 -i 0.5 example.com

This command sets each ping packet size to 100 bytes and the interval time to 0.5 seconds.

Also display AS number

mtr -z example.com

This command will display the AS (Autonomous System) number of each node in the results, helping to identify the network affiliation.


Interpreting mtr Output

Interactive Interface Explanation

When running mtr, you will see output similar to the following:

My traceroute  [v0.92]
example.com (192.0.2.1)                                                                2022-01-01T12:00:00+0000
Keys:  Help   Display mode   Restart statistics   Order of fields   quit
                                                                 Packets               Pings
 Host                                                        Loss%   Snt   Last   Avg  Best  Wrst StDev
 1. 192.168.1.1                                              0.0%    10    2.1   2.2   1.9   2.5   0.2
 2. 10.0.0.1                                                 0.0%    10    5.3   5.5   5.1   6.0   0.3
 3. 203.0.113.1                                              0.0%    10   15.2  15.5  15.0  16.1   0.4
 4. example.com                                              0.0%    10   20.1  20.3  20.0  21.0   0.3

Explanation of Key Metrics

  1. Loss%: Packet loss rate, the higher the value, the more unstable the network
  2. Snt: The number of probe packets sent
  3. Last: The latency of the most recent probe (milliseconds)
  4. Avg: Average latency (milliseconds)
  5. Best: Best latency (milliseconds)
  6. Wrst: Worst latency (milliseconds)
  7. StDev: Standard deviation of latency, reflecting network stability

Advanced Usage and Tips

1. Trace both IPv4 and IPv6 paths simultaneously

Example

mtr -4 example.com  # IPv4 path
mtr -6 example.com  # IPv6 path

2. Use TCP SYN instead of ICMP (to bypass certain firewalls)

mtr --tcp example.com

3. Specify the source port (for specific route testing)

mtr --port 8080 example.com

4. Save results to CSV format

mtr --csv example.com > result.csv

5. Compare network conditions at two time points

Example

mtr -c 60 -i 1 -w example.com > result1.txt
# After a period of time
mtr -c 60 -i 1 -w example.com > result2.txt
diff result1.txt result2.txt

Common Problem Troubleshooting

1. High packet loss problem

If high packet loss occurs at a certain node:

  • It may be that the node is overloaded
  • It may be a network device configuration problem
  • It may be an interconnection problem between ISPs

2. Sudden latency increase problem

If latency suddenly increases at a certain node:

  • It may be network congestion
  • It may be route changes
  • It may be a device performance problem

3. Asterisk (*) display

If you see an asterisk (*) instead of an IP address:

  • It may be that the node does not respond to ICMP requests
  • It may be that the firewall blocked the probe packets

Comparison of mtr with Other Tools

Tool Real-time updates Bidirectional diagnosis Packet loss statistics Latency statistics Interactive interface
ping ❌ ❌ ✔️ ✔️ ❌
traceroute ❌ ❌ ❌ ✔️ ❌
mtr ✔️ ✔️ ✔️ ✔️ ✔️

Practical Exercises

  1. Use mtr to check the network path to websites you frequently visit
  2. Compare the network quality difference between Wi-Fi and wired connections
  3. Try testing network performance with different ping packet sizes
  4. Set up a scheduled task to periodically generate network quality reports

Example

# Example: Generate a network status report daily
(crontab -l 2>/dev/null; echo "0 2 * * * /usr/sbin/mtr -r -c 100 google.com > ~/network_report_$(date +%Y%m%d).txt") | crontab -

Summary

mtr is a powerful network diagnostic tool that combines the advantages of traceroute and ping, providing real-time and comprehensive network path analysis capabilities. Through this article, you should be able to:

  1. Understand the basic principles and working methods of mtr
  2. Skillfully use mtr to diagnose network problems
  3. Interpret the various indicators output by mtr
  4. Apply mtr to solve real-world network problems

Mastering the mtr command will greatly improve your efficiency in troubleshooting network problems, and it is a skill that every system administrator and developer should possess.


Linux 命令大全Linux Command Encyclopedia

Other Extensions