Linux traceroute command


The traceroute command in Linux is a network diagnostic tool used to trace the path that packets take from your computer to a destination (usually a domain or IP address). It shows the route (i.e., intermediate hops) and measures the round-trip time for each hop along the way. This tool is useful for troubleshooting network connectivity issues, identifying routing problems, or determining the path traffic takes across the internet.

Basic Syntax of traceroute:

traceroute [options] [destination]
  • [destination]: The domain name or IP address of the target you want to trace the route to.
  • [options]: Various options to modify the behavior of the command.

How traceroute Works:

traceroute works by sending specially crafted ICMP (Internet Control Message Protocol) or UDP (User Datagram Protocol) packets with incrementally increasing time-to-live (TTL) values. TTL is a field in the IP header that limits the lifespan of a packet. When the TTL value is exceeded, the router discards the packet and sends back an ICMP "Time Exceeded" message. By sending packets with increasing TTL values, traceroute determines the sequence of routers (or hops) along the way to the destination.


Commonly Used traceroute Commands and Options:

  1. Basic Traceroute:

    • This command traces the path from your machine to the specified destination (domain name or IP address).
    traceroute [destination]
    • Example:
      traceroute google.com
      Sample Output:
      traceroute to google.com (142.250.190.14), 30 hops max, 60 byte packets 1 192.168.1.1 (192.168.1.1) 1.520 ms 1.125 ms 1.003 ms 2 10.0.0.1 (10.0.0.1) 4.137 ms 4.134 ms 4.042 ms 3 172.217.4.2 (172.217.4.2) 10.352 ms 10.333 ms 10.417 ms 4 142.250.190.14 (142.250.190.14) 15.852 ms 15.837 ms 15.838 ms
    • Explanation:
      • 1, 2, 3, 4: These represent each hop along the route to the destination.
      • IP addresses: The IP addresses of the routers at each hop.
      • Round-trip times (ms): The time it took for the packet to travel to each hop and back (three measurements are shown).
  2. Traceroute with Maximum Hops:

    • You can specify the maximum number of hops (max_hops) using the -m option. By default, traceroute uses 30 hops.
    traceroute -m [max_hops] [destination]
    • Example:
      traceroute -m 20 google.com
      Sample Output:
      traceroute to google.com (142.250.190.14), 20 hops max, 60 byte packets 1 192.168.1.1 (192.168.1.1) 1.520 ms 1.137 ms 1.074 ms 2 10.0.0.1 (10.0.0.1) 3.200 ms 3.184 ms 3.081 ms 3 172.217.4.2 (172.217.4.2) 8.210 ms 8.191 ms 8.307 ms 4 142.250.190.14 (142.250.190.14) 14.880 ms 14.865 ms 14.981 ms
  3. Traceroute with UDP Packets:

    • By default, traceroute uses UDP packets. If you want to explicitly specify it, you can use the -U option:
    traceroute -U [destination]
  4. Traceroute with ICMP Echo Requests:

    • traceroute can also use ICMP Echo Requests (the same as ping) instead of UDP packets. This is done with the -I option.
    traceroute -I [destination]
    • Example:
      traceroute -I google.com
      Sample Output:
      traceroute to google.com (142.250.190.14), 30 hops max, 60 byte packets 1 192.168.1.1 (192.168.1.1) 1.562 ms 1.531 ms 1.495 ms 2 10.0.0.1 (10.0.0.1) 4.107 ms 4.089 ms 4.136 ms 3 172.217.4.2 (172.217.4.2) 10.556 ms 10.437 ms 10.467 ms 4 142.250.190.14 (142.250.190.14) 15.915 ms 15.824 ms 15.857 ms
  5. Traceroute with Timeout:

    • You can set the timeout for waiting for a response from each hop using the -w option (in seconds).
    traceroute -w [timeout] [destination]
    • Example:
      traceroute -w 2 google.com
  6. Traceroute with Output Format (Display with Numeric IPs):

    • If you want to avoid DNS lookups for hostnames and display only numeric IPs for each hop, use the -n option.
    traceroute -n google.com

    Sample Output:

    traceroute to google.com (142.250.190.14), 30 hops max, 60 byte packets 1 192.168.1.1 (192.168.1.1) 1.520 ms 1.125 ms 1.003 ms 2 10.0.0.1 (10.0.0.1) 4.137 ms 4.134 ms 4.042 ms 3 172.217.4.2 (172.217.4.2) 10.352 ms 10.333 ms 10.417 ms 4 142.250.190.14 (142.250.190.14) 15.852 ms 15.837 ms 15.838 ms

Explanation of Traceroute Output:

A typical output from traceroute might look like this:

traceroute to google.com (142.250.190.14), 30 hops max, 60 byte packets 1 192.168.1.1 (192.168.1.1) 1.520 ms 1.125 ms 1.003 ms 2 10.0.0.1 (10.0.0.1) 4.137 ms 4.134 ms 4.042 ms 3 172.217.4.2 (172.217.4.2) 10.352 ms 10.333 ms 10.417 ms 4 142.250.190.14 (142.250.190.14) 15.852 ms 15.837 ms 15.838 ms

Explanation:

  • First column (1, 2, 3, 4): The hop number, which indicates the order in which each router was reached.
  • IP addresses: The IP address of the router at each hop.
  • Round-trip times: The time it takes for a packet to go from your machine to the router and back (three measurements are shown).

If a hop doesn't respond, you'll see * symbols indicating a timeout. This can happen if a router is configured to block ICMP packets or if there is network congestion.


Example Scenarios:

  1. Traceroute to a domain:

    traceroute google.com
    • Output shows each hop and its respective round-trip times.
  2. Traceroute to a domain with limited hops:

    traceroute -m 15 google.com
    • Output will show the route with a maximum of 15 hops.
  3. Traceroute to a domain using ICMP:

    traceroute -I google.com
    • Uses ICMP Echo Requests instead of UDP packets to trace the route.

Summary of Common traceroute Options:

OptionDescription
-m [max_hops]Set the maximum number of hops to trace.
-w [timeout]Set the timeout for each probe in seconds.
-nDisplay numeric IP addresses, avoiding DNS lookups.
-IUse ICMP Echo Request (ping-like) instead of UDP.
-UUse UDP packets instead of ICMP.

Conclusion:

The traceroute command is a powerful tool for visualizing the path packets take across a network to reach a destination. It helps network administrators and users diagnose network performance issues, such as packet loss or routing problems, by revealing the sequence of routers and their response times. It is a valuable tool for network troubleshooting and analysis.