Saturday, April 20, 2024
HomeA-Z Commands16 Practical Examples of Traceroute Command in Linux

16 Practical Examples of Traceroute Command in Linux

The internet is a vast network system full of databases, servers, routers, and other web structures. When you type a web address and hit the enter button on your web browser, it routes through your internet connection to the host address via the web. In the middle of the destination, when your packet starts the journey from the computer and reaches the destination server, it travels a lot inside the web. Many Linux, Windows, and Mac tools allow you to track every step of your internet packet. The traceroute command is one of the most used tools that you can use on Linux to track every step, live status, connection strength, latency, and many more regarding your internet packets.

Ping vs. Traceroute Command on Linux


No doubt, Ping is the most used and easy network command that displays whether the destination address is live or not. The difference between the Ping and the traceroute command is Ping only detects if the server or the address is up or not just by sending a packet and analyzing the signal.

Whereas the traceroute command can do a lot more network troubleshooting and determine the path of the network to achieve the destination address. The traceroute command can also predict the amount of time to load a webpage, detect the routers on the web address, the hostnames, the DNS server names, and many more. 

When we run the Ping command on a network system, the command automatically sends four packets of data to the destination site; and if one of them reaches the site, then the command determines that the site is connectable, and it determines how many seconds it took to establish the connection.

Here is a command format of the Ping command for Linux: 

ping <IP address>
ping <domain name>

The traceroute command tells us more about the destination and how many milliseconds it will take to reach the site. Unlike the Ping command, the traceroute command tells us the Ip addresses of each device and calculates the round trip time of data from every router instead of measuring the only final destination. It also counts the total number of hops on the path.

Here is a command format of the traceroute command for Linux: 

traceroute <ip_address>

Difference Between Traceroute and Mtr and Tracert on Linux


I’m sure you’ve already heard about the traceroute command, mtr command, and the tracert command for tracking the data in a networking model. In Linux and Mac, we can use the traceroute command, wherein the windows version of the traceroute command is the tracert command. Basically, the traceroute and the tracert are the same commands.

Now, we will see the difference between the traceroute command and the mtr command. The full form of the MTR is Matt’s TraceRoute, which is a modified command of the traceroute command. In traceroute, packets are tracked as they travel over an IP network to a specific host.

Example of a traceroute command:

$ traceroute 1.1.1.1
traceroute to 1.1.1.1 (1.1.1.1), 30 hops max, 60 byte packets
1  192.168.1.1 (192.168.1.1)  0.265 ms  0.248 ms  0.239 ms
2  * * *
3  X-X-X-X.X.X.pl (X.X.X.X)  21.871 ms  22.061 ms  25.072 ms
(more data here)
10  one.one.one.one (1.1.1.1)  24.072 ms  22.439 ms  21.497 ms

The MTR is a single network diagnostic tool combining traceroute and ping functions. The MTR prints some of the traceroute outputs along with the Ping results. 

Example of an MTR command:

$ mtr 1.1.1.1 -c 5 --report
Start: 2019-08-09T15:13:28+0200
HOST: blackhole                   Loss%   Snt   Last   Avg  Best  Wrst StDev
  1.|-- 192.168.1.1                0.0%     5    0.2   0.1   0.1   0.2   0.0
  2.|-- X-X-X-X.X.X                0.0%     5    9.7   9.0   8.4   9.7   0.5
  3.|-- X-X-X-X.X.X                0.0%     5    9.6   8.6   6.2   9.8   1.5
  (more data here)
  10.|-- one.one.one.one            0.0%     5   12.8  13.4  10.7  18.9   3.3

How Does Traceroute Command Work on Linux?


The packet from our system to the host address travels through the router, access points, and network interfaces. To determine the actual path of your data packet, the traceroute command uses the IP header, Internet Control Message Protocol (ICMP),  and the TTL or time-to-live tools on a network system.

Tracking the internet packet might be less hassle when you use a very small LAN connection on your system. When entering a wide area network, you need the traceroute command to read the data header for measuring the length, source and destination, and the protocol.

How Does Traceroute Command Work on Linux

The traceroute command uses the TCP protocol to establish the connection. And the 8- bit TTL value also provides us with the TTL counter numbers that determine the total number of encounters between the data packet and the destination via the routers. The packet gets a time-out error when the TTL value is one. And the ICMP sends a message from the source to the package’s destination that ensures the time out.

Traceroute Syntaxes on Linux


Here, we will see the basic syntaxes of the traceroute command in Linux. Understanding the syntaxes will definitely help you to understand all the examples of the traceroute command that we will list below.

Here is the command format:

traceroute [options]  host_Address [pathlength]
  • -f, –first-hop=NUM: Sets the distance between the first hop and the next hop.
  • -g, –gateways=GATES: In open routing, it displays a list of gateways.
  • -I, –ICMP: As a probe, ICMP ECHO is specified.
  • -m, –max-hop=NUM: Sets the number of hops; the default is 64.
  • -M, –type=METHOD: Traceroutes are conducted with either ICMP or UDP; the default method is UDP.
  • -p, –port=PORT: It defines the network port; the default is 33434.
  • -q, –tries=NUM: NUM probe packets will be forwarded per hop. 
  • –resolve-hostnames: You can use this syntax to fix the hostnames.
  • -t, –tos=NUM: the TOS or the type of service is defined in the NUM
  • -w, –wait=NUM: Defines the waiting time in seconds. 
  • -? –help: Prints the help and manual guides for the traceroute command on Linux. 
  • –usage: It shows the usage in a brief text. 
  • -V, –version: Prints the current version of the traceroute command on Linux.

Install the Traceroute Command in Linux


The installation process of the traceroute command in Linux distributions is easy. In Windows and Mac systems, the traceroute tool comes pre-installed. In Linux, you need to install it first. Here I’ve listed installation commands for Ubuntu/Debian, RedHat, Fedora, and Arch Linux systems. 

Install the traceroute command on Arch Linux.

sudo pacman -Sy traceroute

Get the traceroute command on Ubuntu/Debian Linux. 

sudo apt-get install traceroute

install traceroute on Linux

Install the traceroute tool on Fedora and Red Hat Linux systems.

# yum install traceroute -y

After the installation finishes, you can check the traceroute version to ensure that the tool is installed successfully on your Linux machine. 

$ traceroute --version

traceroute version check

Examples of the Traceroute Command in Linux


We will now see some useful and practical real-life examples of the traceroute command that you should learn. The traceroute command works smoothly with all major Linux distributions on both desktop and server.

1. Run Traceroute to Get an Overview 


The below-mentioned traceroute command shows us the IP address, the total number of hops in the entire path, the total duration to establish the connection, and the packers’ size. 

traceroute ubuntupit.com

Run Traceroute to get an overview

2. Hiding Device Names


If you want to hide the name of the network devices on the output of the traceroute command, please add the  -n sign on the command. 

traceroute -n  ubuntupit.com

3. Setting the Traceroute Timeout Value


Despite having a default time-out value of the traceroute command, you can manually set the time-out value on your own. The below-mentioned traceroute command shows how to set the timeout value to 7 seconds in Linux. 

traceroute -w 7.0 ubuntupit.com

Run Traceroute to get an overview

4. Setting the Number of Tests


To set the number of tests or the number of queries per hop for one execution of the traceroute command on Linux, you can use the below command. 

traceroute -q 1 ubuntupit.com

Setting the Number of Tests

5. Setting the Initial TTL Value


Using the -f flag syntax on the traceroute command will allow you to set the TTL value or the time-to-live tool value on Linux. 

traceroute -f 11 ubuntupit.com

6. Store the Result in a text File


You can export the results of the traceroute command into a text file through the below-provided command on Linux. 

traceroute google.com > results.txt

Store the result in a txt file

7. Trace the Route Using IPv6


If your internet connection has an IPv6 setup, you can use the below-mentioned traceroute command to trace the connection. 

traceroute -6 ipv6.google.com

8. Trace the Route Using Ipv4


Similarly, you can also define an IPv4 connection through the below-mentioned traceroute command on Linux. 

traceroute 4 google.com 

9. Specify the Interface (NIC) to Use


If your Linux system has more than one network interface card (NIC), you can assign one of your NICs to use the traceroute command with the -i flag mentioning the name of the NIC. 

traceroute -i enp8s0 google.com

specify NIC Traceroute command in Linux

If you need to know the name of your network interface card, you can execute the ifconfig command. 

$ ifconfig

10. Set the Response Wait Time


To change the default response wait time of the traceroute command, you can use a -w flag on the traceroute command on Linux. This little tweak would give your data pack to travel a bit more to reach the destination address. 

traceroute -w 1 google.com

11. Disable IP Address and Hostname Mapping


To disable the IP address and hide the host mapping on Linux, please use a n sign on the traceroute command. This will allow the user not to print the host IP address and the hostname. 

traceroute n google.com 

12. Set the Maximum Number of hop


By default, the average number of hops of the traceroute command is 30. You can change the hop value on the traceroute command by adding the hop value with a -m flag on the command. 

traceroute -m 7 google.com 

Maximum Number of hop Traceroute command in Linux

13. Do Not Fragment the Packet


If you do not want to fragment the data pack on your Linux system, please use the -F flag on your traceroute command. 

$ traceroute -F google.com

14. Route the Packet Through the Gate


If you want to route the traceroute command through your desired gateway, you can use the -g sign along with the command to add your gateway to the Linux command.  

$ traceroute -g 192.168.1.6 google.com

15. Set the Destination Port to Use


By default, the traceroute command uses the network port 33434. If you need to change the network port, you can use the -p sign on the traceroute command on Linux. 

$ traceroute  -p 20292 google.com

Set destination port Traceroute command in Linux

16. Getting Help


If you need more help and manuals for the traceroute command, please execute the below-mentioned commands on the shell. The manuals will definitely help you to get started with the tool. 

traceroute --help  
man traceroute 

man Traceroute command in Linux

Issues You might Face in Using the Traceroute Command


While using the traceroute command in Linux, you might face some issues if you are new. Here we have enlisted a few most faced issues for the traceroute command by different users from the Linux community.

1. Issue with the -n Flag in Traceroute Command on Linux


Earlier, we have seen that you can use an n flag with the traceroute command in Linux to hide the device name. If you face any issue while using the n flag on your shell and it gives you an invalid option, you must ensure that you have the correct traceroute tool installed on your system. You can also try executing the below-mentioned commands for alternative installation on your system. 

Here is the sample output. 

$ sudo traceroute -n . Not working
$ traceroute: invalid option -- 'n'

Commands to get the traceroute tool fixed on a Linux machine. 

$ update-alternatives --display traceroute
$ apt-get install traceroute
$ update-alternatives --configure traceroute

2.  Packages Cannot Be Authenticated: Traceroute Command Issue


If the traceroute tool cannot be authenticated in your Linux system, please be sure that you are the sudo user and have the proper permission to run this command on your system. After ensuring that this tool is installed properly, you can now update your package repository with the below-mentioned sudo update command.   

sudo apt-get install traceroute
sudo apt-get update

You can then execute your desired traceroute command, and hopefully, your issues will be solved. 

3. Newer Versions of Traceroute Might Have Issues


In some cases, in some older devices, the latest version of the traceroute command may show some issues. For instance, if you want to run the traceroute command with the -A flag to define the path of the command, you might face an issue. 

Sample Error Output:

~$ traceroute -A 8.8.8.8
traceroute -m 30 -q 1 -w 3 -A 8.8.8.8
traceroute: invalid option -- 'A'
Try 'traceroute --help' or 'traceroute --usage' for more information.
 -A     Perform AS path lookups in routing registries and print  results
          directly after the corresponding addresses.   

This issue often gets solved automatically after a repository update. If you still face this issue, please restart your device, execute the command properly, and ensure that your command does not have any typing mistakes. 

~$ traceroute -A 8.8.8.8 
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets

4. Traceroute Installation Error in Linux


If you face any issue while installing the traceroute command on your Linux machine, please ensure that the path you are assigning to install is available for package installation. You must also ensure that you have the superuser privilege to install that tool. In most cases, the users keep trying to install the traceroute command inside a locked directory and get a permission denied error. 

Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable  to lock the administration  directory (/var/lib/dpkg/), are you root?

You can execute any of the below-mentioned command sets to solve this issue. Don’t forget to fix the missing repository and install the traceroute command on Linux.  

sudo apt-get install --fix-missing  
sudo apt-get update && sudo apt-get install traceroute 

Get the universe repository and install the traceroute command. 

sudo add-apt-repository universe
sudo apt-get update
sudo apt-get install inetutils-traceroute

5. Remove Brackets () Around IP Address on Traceroute in Linux


Sometimes you might get the output IP address inside the first bracket or in the parentheses in the terminal shell.  

Here is a sample output:

traceroute -m2 8.8.8.8 |grep .net |awk '{print $3}'
(207.225.112.2)

To remove the brackets from the output, please execute the below-mentioned command. 

$ traceroute -m 2 8.8.8.8 | awk '/net/{gsub(/\(|\)/,"");print $3}'             
Or
$ traceroute -m 2 8.8.8.8 | awk '/.net/{print $3}'  |  tr -d '()'
207.225.112.2

Final Words


Using the traceroute tool for network troubleshooting can make your task easier and more efficient. It might take a few days for the newbie users to get started with this command, but once you’re used to this tool, you’ll find how much you enjoy using it on Linux.

In the entire post, we have described a lot of things about the traceroute command in Linux. I’ve tried to illustrate the traceroute command as simply as possible. I hope this post has been useful for you; if yes, please share this post with your friends and the Linux community. You can also write down your opinions about this post in the comment section.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

You May Like It!

Trending Now