Install mtr Command on Linux | Easy and Best Setup

Posted on

Install mtr Command on Linux | Easy and Best Setup

Install mtr Command on Linux | Easy and Best Setup

In this guide from Orcacore, we will demonstrate How To Install mtr Command on Linux and begin using it. MTR (My Traceroute) or Matt’s TraceRoute is a highly favored traceroute tool available. It has become a go-to tool, often replacing Ping and Traceroute for many network administrators. MTR, as a network monitor, is a vital diagnostic tool used to probe remote servers and continuously measure latency variations over time.

MTR is valuable for several reasons. Firstly, it merges Ping and Traceroute functionalities, allowing for the measurement of availability of devices within a network, alongside route monitoring. Crucially, it offers a significant advantage over Traceroute: its output is updated continuously. Unlike Traceroute, which requires manual scan updates to track network performance changes, MTR provides real-time monitoring that persists until stopped. This capability to observe network performance changes over time is indispensable for effective troubleshooting. Let’s learn how to Install mtr Command on Linux.

Steps To Install and Use the mtr command on Linux

To follow this guide, log in to your Linux server as a root user or a non-root user with sudo privileges. Then, follow the steps below.

1. Install mtr Command on Linux

Installing mtr on your server is straightforward.

On Debian / Ubuntu systems, use the following command:

sudo apt install mtr -y

On CentOS / RHEL, use these commands:

# yum install mtr
# dnf install mtr

Once the installation is complete, proceed to the next step to learn how to use the mtr command on Linux.

2. How To Use mtr on Linux?

When using the mtr network scanner, you need to generate an mtr report to view your traffic data. Mtr reports are directional, so it is necessary to generate reports in both directions to get a complete picture of your network’s performance.

In this section, we’re going to look at:

The basic syntax for the mtr command is as follows:

mtr <option> <hostname>/path

Display Hostnames and Numeric IP address

The mtr command displays hostnames in the traceroute report. To do this, you can use the following command:

mtr [domainName/IP]

For example:

mtr google.com

Also, you can use the -g option to see the numeric IP addresses instead of hostnames:

mtr -g google.com

To see both hostnames and IP addresses, you can use the -b option in the mtr command:

mtr -b google.com

Set a limit for the Number of Pings

You can easily configure the mtr command to set a limit for your pings. The syntax used to do this is as follows:

mtr -c [n] “domainname/IP”

For example:

mtr -c 10 google.com

Enable mtr Report Mode

Instead of looking at the screen all the time, we can let mtr do the job for a while and read the result later with the -r option. For example:

mtr -r google.com

You can specify a ping count for which you want to limit the report through the -c option, and also specify the report filename in which the report will be saved. For example:

mtr -r -c 10 google.com > mtr-report-google

The report is saved in the current user’s home folder by default. You can, however, specify a proper path for the report to be saved in.

Also, you can clear output in the mtr report mode. To do this, you can add the -w option. For example:

mtr -rw -c 10 google.com > mtr-report-google

Specify Time Interval

After learning how to Install mtr Command on Linux, you can slow down the packets you sent with the -i option:

mtr -i [time-in-seconds] “domainName/IP”

For example:

mtr -i 10 google.com

The omitted output is the same as if we don’t use the -i option. The only difference is the slower increase of the value in the Snt column. The default value of -i is 1, so this command is 10 times slower, meaning the interval between sent packets is 10 times longer.

The option is useful when we don’t want to swamp the network with our packets.

TCP and UDP

If you want to use the TCP SYN or the UDP datagrams for requesting mtr instead of the default ICMP ECHO requests, you can do so by using the TCP and UDP flags respectively. Syntax:

$ mtr –tcp “domainName/IP”
$ mtr –udp “domainName/IP”

For example:

$ mtr --tcp google.com
$ mtr --udp google.com

Specify packet size

Through the -s option in the mtr command, you can specify the size, in bytes, of the IP packet for diagnosing network quality.

Syntax:

mtr –r -s [packetsize] “domainName/IP”

For example:

mtr -r -s 50 google.com

Print CSV Output

The CSV output of the mtr report delimits the columns with a “,”. With the csv option, you can customize the mtr command to output the report in a CSV format.

Syntax:

mtr –csv “domainName/IP”

For example:

mtr --csv google.com

Print XML Output

The mtr command can also support the XML format for printing traceroute reports. The XML report is a good option for automated processing of the output and can be printed by specifying the XML option with the mtr command.

Syntax:

mtr –xml “domainName/IP”

For example:

mtr --xml google.com

mtr help and man page

Finally, after reading the guide on how to Install mtr Command on Linux, you can get more options for usage and customization of the mtr command on Linux by reading its help and man page through the following commands:

man mtr
mtr --help

Conclusion

At this point, you have learned how to Install mtr Command on Linux. The purpose of using the mtr command in Linux is to diagnose network issues by combining the functions of traceroute and ping. It provides real-time insights into network latency and packet loss along the route to a destination, helping troubleshoot connectivity problems.

Hope you enjoy it. Please subscribe to us on Facebook and YouTube.

You may also like these articles:

How To Use Ping Command in Linux

How To Install and Use tmux on Linux

Install and Use Linux Screen Command

Alternative Solutions for Network Diagnostics

While MTR is a powerful and versatile tool, there are alternative approaches to network diagnostics on Linux. Here are two different methods:

1. Using traceroute and ping in conjunction with scripting

This approach involves using the traditional traceroute and ping commands, but automating their execution and analysis through scripting (e.g., using Bash or Python). This allows for some level of real-time monitoring and data logging similar to MTR, although it requires more manual effort to set up and analyze.

Explanation:

  • traceroute: This command traces the route packets take to a destination, identifying each hop (router) along the way.
  • ping: This command sends ICMP echo requests to a host to measure round-trip time (RTT) and packet loss.

By combining these commands in a script, you can automate the process of tracing the route and pinging each hop, collecting data over time.

Code Example (Bash):

#!/bin/bash

DESTINATION="google.com"
TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S)
LOG_FILE="traceroute_ping_${DESTINATION}_${TIMESTAMP}.log"

echo "Starting traceroute and ping to $DESTINATION..." > "$LOG_FILE"

traceroute $DESTINATION | while read -r line; do
  echo "$line" >> "$LOG_FILE"
  if [[ "$line" =~ ([0-9]{1,3}.){3}[0-9]{1,3} ]]; then
    IP=$(echo "$line" | awk '{print $2}') # Extract IP Address
    echo "Pinging $IP..." >> "$LOG_FILE"
    ping -c 3 $IP >> "$LOG_FILE"
    echo "" >> "$LOG_FILE"
  fi
done

echo "Traceroute and ping completed. Results saved in $LOG_FILE"

How it works:

  1. The script sets the destination hostname and creates a log file with a timestamp.
  2. It executes traceroute to the specified destination.
  3. The output of traceroute is piped to a while loop, which reads each line.
  4. For each line, the script checks if it contains an IP address using a regular expression.
  5. If an IP address is found, the script extracts the IP address and pings it three times using the ping command.
  6. The output of both traceroute and ping is appended to the log file.

This script provides a basic framework. More advanced scripts can include error handling, data parsing, and visualization of the results. This approach, while not as feature-rich as MTR, offers a readily available and scriptable alternative.

2. Using tcpdump and Wireshark for Packet Analysis

Another alternative is to capture network traffic using tcpdump and then analyze the captured data using Wireshark. This approach provides a more in-depth view of network communication, allowing you to identify latency issues, packet loss, and other network problems.

Explanation:

  • tcpdump: A command-line packet analyzer that captures network traffic based on specified filters.
  • Wireshark: A powerful GUI-based network protocol analyzer that allows you to examine captured packets in detail.

This combination provides a very granular level of analysis, allowing you to inspect the contents of packets and identify the root cause of network issues.

Code Example (tcpdump):

sudo tcpdump -i eth0 -w capture.pcap host google.com

How it works:

  1. sudo tcpdump runs the tcpdump command with root privileges (required to capture network traffic).
  2. -i eth0 specifies the network interface to capture traffic on (replace eth0 with your actual interface).
  3. -w capture.pcap writes the captured packets to a file named capture.pcap.
  4. host google.com filters the captured traffic to only include packets to or from google.com.

After capturing the traffic, you can open the capture.pcap file in Wireshark to analyze the packets. Wireshark provides a rich set of features for filtering, sorting, and examining packets, allowing you to identify latency issues, packet loss, retransmissions, and other network problems. While this method requires more expertise in network protocols, it provides a deeper understanding of network behavior than simply using mtr. It requires a good understanding of network protocols and packet structure.

In summary, while mtr offers a convenient and user-friendly way to diagnose network issues, alternative approaches using traceroute, ping, scripting, tcpdump, and Wireshark can provide valuable insights into network performance and troubleshoot connectivity problems. Choosing the right tool depends on the specific needs and expertise of the user.