Traceroute command to count hop in Linux

In this tutorial, we’ll explain traceroute command to count hop in Linux. Traceroute is a Linux command and it is also known as networking utility using which it shows you the hops count between source and destination of the network packet and also it will show you the path/servers it goes through.

Traceroute command to count hop in LinuxTraceroute command to count hop in Linux

Traceroute command is used in troubleshooting the data loss occurring in the network, as each hop shows the network address of the server/router it passes, and it is easy to find out packet loss at which server or router has the issue.

The Internet is a vast network of interconnected devices, and data travels through it in the form of packets. These packets travel from one device to another, hopping through multiple devices until they reach their final destination. Traceroute is a command-line tool that is used to trace the path taken by packets from their source to their destination. In this blog, we will explore the traceroute command and how it can be used to count the number of hops in Linux.

What is Traceroute?

Traceroute is a command-line tool that is used to trace the path taken by packets from their source to their destination. It works by sending packets with gradually increasing Time To Live (TTL) values. The TTL value is a field in the packet header that specifies how many hops the packet can take before it is discarded. Each time the packet reaches a device, the TTL value is decremented by one. When the TTL value reaches zero, the device discards the packet and sends an ICMP Time Exceeded message back to the source.

Traceroute uses this mechanism to trace the path taken by packets from their source to their destination. It sends packets with gradually increasing TTL values and waits for ICMP Time Exceeded messages from each device in the path. By analyzing these messages, traceroute can determine the IP address of each device in the path and the time taken by each packet to travel from one device to another.

Using Traceroute to Count Hops

Traceroute can be used to count the number of hops between the source and destination devices. Each time a packet is sent, traceroute displays the IP address of the device it reached and the time taken for the packet to travel to that device. By counting the number of devices displayed, we can determine the number of hops between the source and destination.

Now, we will see how to install the traceroute command in Linux and how to troubleshoot the network.

Steps to Install Traceroute command in Linux

No need to install it because the traceroute command is pre-installed on many Linux distributions. If you are not getting the traceroute command then install with below command.

Command to install traceroute in CentOS/RHEL/Oracle Linux/Amazon Linux

# yum install traceroute

Command to install traceroute in Ubuntu/Debian

# apt-get install traceroute

Command to install traceroute in Fedora

# dnf install traceroute

For installing any of the packages you need root access, If you have sudo access then before installing traceroute command use sudo in front of installation command.

The syntax for using traceroute command in Linux

Syntax:

# traceroute [option] destination

To use traceroute to count hops, we need to open a terminal window in Linux and enter the following command:

traceroute <destination IP address or domain name>

For example, to trace the path taken by packets from our local machine to the google.com server, we would enter the following command:

traceroute google.com

This will display the IP addresses of all the devices in the path between our local machine and the google.com server, along with the time taken for each packet to travel from one device to another.

By default, traceroute sends packets with a TTL value of 1 and gradually increases the TTL value by 1 until it reaches the destination or a maximum of 30 hops. We can change the maximum number of hops by using the -m option followed by the maximum number of hops we want to allow. For example, to limit the number of hops to 10, we would enter the following command:

traceroute -m 10 google.com

This will limit the number of hops to 10 and display the IP addresses of the devices in the path between our local machine and the google.com server, along with the time taken for each packet to travel from one device to another.

How to check the route for a Linux server?

To check the trace command for gmail.com, follow below command

$ traceroute gmail.com

Use this command to check tracepath for a website or an IP address.

$ traceroute 8.8.8.8

In Output you will find the following lines:

 The first line shows the destination IP address

Next line & all the rest lines show the hops, and their IP address, and also you can see the response time, etc.

 You can see the “*” for some line, this show no response received for the request.

Configure the number of queries

On each hop you traceroute send 3 queries per packet and 3 round-trip times. You can change the queries per packet using the “–q” option.

$ traceroute -q 4 gmail.com

Disable IP, Hostname mapping

You can disable IP address & hostname mapping by using –n option with traceroute

$ traceroute -n gmail.com

There are many more options you can check this with man pages for traceroute.

Configure TTL for traceroute

The default TTL value for the traceroute is 1, you can change the default TTL value for the traceroute by using “f” Option.

Eg:

$ traceroute -f 5 gmail.com

Configure the response wait time

Response wait time also can be changed using the “w” Option.

$ traceroute -w 0.5 gmail.com

Conclusion

Traceroute is a powerful command-line tool that can be used to trace the path taken by packets from their source to their destination. It uses a mechanism of gradually increasing the TTL value of packets and analyzing the ICMP Time Exceeded messages to determine the IP addresses of the devices in the path and the time taken for packets to travel between them.

Traceroute is a useful tool for troubleshooting network connectivity issues and identifying the devices that packets are passing through. It can also be used to identify network latency and bottlenecks.

Overall, the traceroute command is an essential tool for network administrators and developers. It can help diagnose and solve a wide range of networking problems and provide valuable information about network performance. By using traceroute, we can quickly identify the devices in the path between our local machine and a remote server, and determine the time taken for packets to travel between them.

End of the tutorial, Traceroute command to count hop in Linux.

Thanks for reading this article, you’ll also like to read the below articles.

Basic Linux Storage Scenario

How to execute script or command on reboot or startup in Linux

Steps to Manage logs in Linux using Logrotate

Checklist for Pre and Post Linux Reboot

Linux Network Configuration Command Line