traceroute
What it is
A network diagnostic tool that traces the path packets take from your computer to a destination host, showing each hop along the way.
Installation
Linux
sudo apt update && sudo apt install traceroute
# or
sudo yum install traceroute
macOS
traceroute is usually pre-installed. If not:
brew install traceroute
Windows
Windows has a built-in equivalent called tracert. To install traceroute on Windows, you might consider using the Windows Subsystem for Linux (WSL).
Core Concepts
- Hops: Each router or network device that a packet passes through on its way to the destination is considered a hop.
traceroutedisplays the IP address and, if possible, the hostname of each hop. - Time To Live (TTL):
tracerouteworks by sending packets with incrementally increasing TTL values. The first packet has a TTL of 1, causing it to expire at the first router. The router sends back an ICMP "Time Exceeded" message.tracerouterecords the round-trip time for this message. It then sends a packet with TTL 2, which expires at the second router, and so on. This process continues until the packet reaches the destination. - ICMP Time Exceeded: This is the message sent by routers when a packet’s TTL expires before reaching its destination.
tracerouterelies on these messages to identify hops. - ICMP Destination Unreachable: This message is sent by the final destination host when it receives a packet but cannot deliver it to the intended port (e.g., if no process is listening on that UDP port).
tracerouteuses this to know it has reached the destination.
Commands / Usage
Basic Tracing
-
Trace to a hostname:
traceroute google.comTraces the route to
google.comusing default settings (UDP probes). -
Trace to an IP address:
traceroute 8.8.8.8Traces the route to the IP address
8.8.8.8.
Probe Type Selection
-
Use ICMP Echo (like ping):
traceroute -I google.comUses ICMP Echo Request packets instead of UDP probes. This is often blocked by firewalls.
-
Use TCP SYN:
traceroute -T -p 80 google.comUses TCP SYN packets to port 80. Useful for tracing routes to web servers and can sometimes bypass firewalls that block UDP/ICMP.
Packet Configuration
-
Set maximum number of hops:
traceroute -m 15 google.comStops tracing after 15 hops, even if the destination is not reached.
-
Set the initial TTL:
traceroute -f 5 google.comStarts the trace with a TTL of 5, skipping the first 4 hops.
-
Set the packet size:
traceroute -s 64 google.comSends packets with a payload size of 64 bytes.
-
Set the number of probes per hop:
traceroute -q 3 google.comSends 3 probe packets for each hop to get more consistent latency measurements.
-
Set the wait time for a response:
traceroute -w 2 google.comWaits a maximum of 2 seconds for a response from each hop.
Network Interface and Source Address
-
Specify the outgoing network interface:
traceroute -i eth0 google.comForces
tracerouteto use theeth0network interface. -
Specify the source IP address:
traceroute -s 192.168.1.100 google.comUses
192.168.1.100as the source IP address for probes.
Hostname Resolution
- Do not resolve hostnames:
Does not attempt to resolve IP addresses to hostnames, showing only IPs. Speeds up the process and can help diagnose DNS issues.traceroute -n google.com
Verbosity and Debugging
-
Increase verbosity:
traceroute -v google.comShows more detailed output during the trace.
-
Set the probe port (for UDP probes):
traceroute -p 33434 google.comSets the starting UDP destination port. The port number is incremented for each hop.
Common Patterns
-
Diagnosing slow connections:
traceroute -n -m 30 example.com | grep '192.168.'Traces to
example.comwithout hostname resolution, stops at 30 hops, and filters for hops within your local network range to see where delays might be occurring internally. -
Checking connectivity to a specific port (using TCP):
traceroute -T -p 443 www.example.comTraces the route to
www.example.comusing TCP SYN packets to port 443 (HTTPS), useful for diagnosing issues with secure web connections. -
Comparing routes with different probe types:
traceroute -I google.com & traceroute -T -p 80 google.comRuns an ICMP-based trace and a TCP-based trace concurrently to see if different network paths are taken or if one type of probe is blocked.
-
Saving output to a file:
traceroute google.com > traceroute_google.txtSaves the entire output of the
traceroutecommand to a file namedtraceroute_google.txt. -
Finding the first non-responsive hop:
traceroute google.com | grep '*'Filters the output to show only lines with asterisks, indicating hops that did not respond within the timeout. This can point to network congestion or firewall issues.
Gotchas
- Firewalls: Many routers and hosts are configured to drop ICMP or UDP packets used by
traceroute. This can result in asterisks (* * *) appearing in the output, even for hops that are functioning correctly. Using TCP probes (-T) can sometimes circumvent this. - Asymmetric Routing: The path packets take to a destination might be different from the path packets take back.
tracerouteonly shows the path from your machine to the destination. - Load Balancing: Some routers, especially at higher levels, may employ load balancing across multiple paths. This can cause the IP addresses reported for a specific hop number to vary between different runs of
traceroute, or even between probes within the same run if not all probes are sent to the same load balancer. - Intermittent Failures: Network conditions can change rapidly. A hop that appears problematic (e.g., high latency, packet loss) on one run might be fine on the next. Running
traceroutemultiple times can help identify consistent issues. - TTL Exceeded vs. Destination Unreachable:
tracerouterelies on ICMP "Time Exceeded" messages from intermediate routers. When it reaches the destination, it typically receives an ICMP "Destination Unreachable" message (often for a specific port, if using UDP probes). If the destination host or intermediate firewalls block these ICMP messages,traceroutemight not be able to determine the full path or confirm arrival. - Hostname Resolution Delays: If hostname resolution is slow or fails, the
-nflag (numeric output) can significantly speed up the process and isolate network path issues from DNS issues.