dotlinux guide

Using Traceroute and Ping for Network Diagnostics on Linux

In the world of networking, connectivity issues, latency, and packet loss are common headaches. Whether you’re a system administrator, developer, or IT enthusiast, diagnosing these problems efficiently is critical to maintaining reliable network performance. Two foundational tools for network diagnostics on Linux—ping and traceroute—provide invaluable insights into network health, routing paths, and connectivity. Ping checks if a remote host is reachable and measures round-trip latency, while traceroute maps the path packets take from your machine to a target host, identifying bottlenecks along the way. Together, they form the first line of defense for troubleshooting network issues. This blog will demystify these tools, covering their underlying concepts, usage, common practices, and best practices to help you diagnose network problems like a pro.

Table of Contents

What is Ping?

How Ping Works

Ping (Packet Internet Groper) is a utility that tests connectivity between two network devices using the Internet Control Message Protocol (ICMP). It sends small data packets (ICMP Echo Request messages) to a target host and waits for ICMP Echo Reply messages. By measuring the time between sending a request and receiving a reply (round-trip time, RTT), ping helps determine:

  • If a host is reachable.
  • Latency (delay) between your machine and the target.
  • Packet loss (percentage of lost packets).

Basic Ping Usage on Linux

The ping command is preinstalled on nearly all Linux distributions. Its basic syntax is:

ping [options] <target>  

Key Options:

OptionDescription
-c <count>Stop after sending <count> packets (avoids infinite execution).
-i <interval>Set the time (in seconds) between packets (default: 1 second).
-s <size>Specify the size of the data payload (in bytes; default: 56 bytes, total packet size = 64 bytes with headers).
-W <timeout>Wait <timeout> seconds for a reply (default: 1 second).
-M <do/dont/fragment>Control IP fragmentation: do (don’t fragment), dont (allow fragmentation).
-nDisable DNS resolution (show IP addresses instead of hostnames, speeds up output).

Ping Command Examples

Example 1: Basic Connectivity Check

Test if google.com is reachable, sending 4 packets:

ping -c 4 google.com  

Sample Output:

PING google.com (142.250.190.142) 56(84) bytes of data.  
64 bytes from lga34s12-in-f142.1e100.net (142.250.190.142): icmp_seq=1 ttl=118 time=12.3 ms  
64 bytes from lga34s12-in-f142.1e100.net (142.250.190.142): icmp_seq=2 ttl=118 time=11.9 ms  
64 bytes from lga34s12-in-f142.1e100.net (142.250.190.142): icmp_seq=3 ttl=118 time=12.1 ms  
64 bytes from lga34s12-in-f142.1e100.net (142.250.190.142): icmp_seq=4 ttl=118 time=12.0 ms  

--- google.com ping statistics ---  
4 packets transmitted, 4 received, 0% packet loss, time 3005ms  
rtt min/avg/max/mdev = 11.923/12.098/12.311/0.157 ms  

This output shows:

  • 4 packets sent, 4 received (0% loss).
  • RTT statistics: minimum (11.9ms), average (12.1ms), maximum (12.3ms), and variability (mdev).

Example 2: Testing MTU Issues

To diagnose Maximum Transmission Unit (MTU) problems (e.g., “packet too big” errors), send large packets with fragmentation disabled:

ping -s 1472 -M do google.com  
  • -s 1472: 1472 bytes of data (total packet size = 1472 + 28 bytes for IP/ICMP headers = 1500 bytes, the standard MTU for Ethernet).
  • -M do: Disable fragmentation. If the target responds, the MTU is sufficient; if not, reduce the -s value until it works.

What is Traceroute?

How Traceroute Works

While ping tests if a host is reachable, traceroute answers how packets get there. It maps the routing path from your machine to a target host by sending packets with incrementally increasing Time-to-Live (TTL) values.

  • TTL is a packet header field that limits a packet’s lifespan (each router decrements TTL by 1).
  • When TTL reaches 0, the router discards the packet and sends an ICMP Time Exceeded message back to the sender.
  • Traceroute starts with TTL=1 (first hop), then TTL=2 (second hop), etc., recording the IP address and latency of each responding router.

By default, Linux traceroute uses UDP packets (to high, unused ports), while Windows uses ICMP. Traceroute can also use TCP (with tcptraceroute) or ICMP (with the -I flag) if UDP is blocked.

Basic Traceroute Usage on Linux

The traceroute command is included in most Linux distributions (install with sudo apt install traceroute or sudo yum install traceroute if missing). Its basic syntax is:

traceroute [options] <target>  

Key Options:

OptionDescription
-IUse ICMP Echo Requests (like ping) instead of UDP.
-TUse TCP SYN packets (useful if ICMP/UDP is blocked).
-p <port>Set the destination port (for UDP/TCP).
-nDisable DNS resolution (show IPs only, faster output).
-m <max_ttl>Set the maximum TTL (default: 30 hops).
-q <queries>Number of probes per hop (default: 3).

Traceroute Command Examples

Example 1: Basic Path Tracing

Trace the route to google.com:

traceroute google.com  

Sample Output:

traceroute to google.com (142.250.190.142), 30 hops max, 60 byte packets  
 1  router.local (192.168.1.1)  1.234 ms  1.123 ms  1.098 ms  
 2  10.0.0.1 (10.0.0.1)  5.456 ms  5.345 ms  5.234 ms  
 3  203.0.113.1 (203.0.113.1)  12.345 ms  12.456 ms  12.567 ms  
 4  * * * (no response from this hop)  
 5  198.51.100.1 (198.51.100.1)  25.678 ms  25.567 ms  25.456 ms  
 ...  
12  lga34s12-in-f142.1e100.net (142.250.190.142)  30.123 ms  29.876 ms  30.012 ms  

This output shows:

  • Hop number, router IP/hostname, and latency (3 probes per hop).
  • * * * indicates a router that did not respond (common if ICMP/UDP is blocked).

Example 2: Traceroute with ICMP (Avoid UDP Blocks)

If UDP is blocked by firewalls, use ICMP with -I:

traceroute -I -n google.com  
  • -n skips DNS lookups, speeding up results.

Example 3: TCP Traceroute (For Firewall-Protected Targets)

To trace using TCP (e.g., to a web server on port 80):

traceroute -T -p 80 google.com  

This sends TCP SYN packets to port 80, which is more likely to pass through firewalls than UDP.

Common Use Cases

1. Troubleshooting Connectivity

If ping <target> fails, use traceroute to identify where the path breaks:

  • If the first hop (router) fails: Local network issue (e.g., Wi-Fi disconnected, cable unplugged).
  • If hops in the middle fail: ISP or intermediate router issue.
  • If the final hop fails: Remote host is down or blocking ICMP.

2. Identifying Latency Bottlenecks

Traceroute reveals which hop introduces high latency. For example:

 5  198.51.100.1 (198.51.100.1)  150.0 ms  152.0 ms  148.0 ms  <-- High latency here!  

This indicates the 5th hop (likely an ISP router) is causing delays.

3. Detecting Packet Loss

If ping shows packet loss, use traceroute to find which hop is dropping packets. Consistent * * * in traceroute suggests a problematic router.

4. Verifying Routing Paths

Traceroute confirms if traffic follows the expected route (e.g., “Is traffic routed through the London or New York data center?”).

Best Practices

1. Use -n for Faster Results

Disable DNS resolution with -n (for both ping and traceroute) to avoid delays from slow DNS lookups:

ping -n -c 4 142.250.190.142  
traceroute -n google.com  

2. Limit Packet Count with -c

Avoid infinite ping runs by specifying -c <count>. For example, ping -c 10 <target> sends 10 packets and exits.

3. Test with Different Packet Sizes

Large packets may reveal MTU issues. Use ping -s <size> -M do to test fragmentation limits.

4. Combine Ping and Traceroute

  • Use traceroute to map the path, then ping individual hops to isolate latency/loss:
    ping -c 5 198.51.100.1  # Test latency to hop 5  

5. Test IPv4 and IPv6

Use ping6 and traceroute6 (or traceroute -6) to diagnose IPv6-specific issues:

ping6 -c 4 ipv6.google.com  
traceroute6 ipv6.google.com  

6. Use Non-Default Protocols When Needed

If ICMP is blocked (common in enterprise networks), use TCP/UDP traceroute:

traceroute -T -p 443 google.com  # TCP to port 443 (HTTPS)  

7. Document Results

Save outputs with timestamps for comparison (e.g., “Was latency high at 9 AM but normal at 3 PM?“):

ping -c 10 google.com > ping_$(date +%F_%H%M).txt  

Conclusion

Ping and traceroute are indispensable tools for Linux network diagnostics. Ping verifies reachability and measures latency, while traceroute maps routing paths and identifies bottlenecks. By mastering their options (e.g., -n, -I, -T) and combining them strategically, you can quickly diagnose connectivity issues, latency, and packet loss.

Remember: Network diagnostics is iterative. Use these tools to narrow down the root cause, then validate fixes with follow-up tests. With practice, you’ll transform raw ping and traceroute outputs into actionable insights for maintaining robust network performance.

References