Knowledge

ICMP: ping and traceroute

Updated 2026-11-08 · Present

Estimated time: 35 minutes.

Quick summary

Step 1 — Ping sends an ICMP Echo Request — not TCP

Many people mistake ping for a 'TCP connectivity test'. In fact ping only works at L3 (IP), needs no port, and needs no listening process. A successful ping only means the IP is reachable — it says nothing about the service.

When it breaks: ping succeeds but curl fails → the service is not running or a firewall blocks the port. You need to tell them apart: ping tests L3 (ICMP), nc -zv tests L4 (TCP), curl tests L7 (HTTP). Skipping steps usually bypasses the layer that has the problem.

# Basic ping — send 4 ICMP Echo Requests
ping -c 4 8.8.8.8

# Look at the TTL of the reply — TTL decreases with the number of hops
# If the reply TTL=117 then the source TTL was 128, through 11 hops
ping -c 1 -v 8.8.8.8

Observe with:

sudo tcpdump -i any -n icmp  # See ICMP packets: Type 8 (request), Type 0 (reply)
# In the ping output: the reply TTL tells you the hop count on the way back
# Linux original TTL=64: reply TTL=54 → passed through 10 hops

Step 2 — Wireshark — view ICMP Echo Request/Reply

Capturing packets with tcpdump (or Wireshark) shows that ping is really a pair of ICMP packets: an Echo Request (Type 8) leaving your machine and an Echo Reply (Type 0) coming back from the destination. Seeing both directions in the capture helps you identify where ICMP is blocked: a request with no reply means the remote side blocks inbound ICMP, while seeing neither the request nor the reply means it is blocked outbound right at the local firewall. This is how you turn a vague 'ping doesn't work' into a concrete diagnosis.

When it breaks: You capture ICMP but see no reply → ICMP is blocked in one direction. You see the request go out but no reply → the remote side blocks inbound ICMP. You see neither request nor reply → blocked outbound at the local firewall.

# Capture ICMP in Wireshark (Linux)
sudo tcpdump -i any -n icmp

# In another terminal:
ping -c 3 8.8.8.8

# The tcpdump output will look like:
# IP 192.168.x.x > 8.8.8.8: ICMP echo request
# IP 8.8.8.8 > 192.168.x.x: ICMP echo reply

Observe with:

sudo tcpdump -i any -n -v icmp  # -v to see type/code
# To tell them apart:
# 'ICMP echo request' = Type 8 (sent by the host)
# 'ICMP echo reply' = Type 0 (returned by the remote)
# If you only see the request with no reply = the remote blocks ICMP

Step 3 — Traceroute — how the TTL trick works

Understanding the TTL trick means understanding how traceroute 'hacks' the routing mechanism to reveal the path. Every router must report back when it drops a TTL=0 packet — this is an obligation under RFC 792. No extra software is needed, only a feature already built into the IP stack.

When it breaks: An outbound firewall blocks high UDP ports (33434+) → UDP traceroute does not work (all * and the destination is * as well). Switch to ICMP traceroute -I or TCP traceroute. Know all 3 flavors so you can pick the right one when you are blocked.

# Standard traceroute (uses UDP on Linux)
traceroute 8.8.8.8

# Traceroute using ICMP (like Windows tracert)
traceroute -I 8.8.8.8

# Windows:
tracert 8.8.8.8

Observe with:

sudo tcpdump -i any -n icmp  # Capture in parallel while running traceroute
# You will see: ICMP time-exceeded from each intermediate router
# Each router reveals its own IP via the Time Exceeded source address
mtr -n 8.8.8.8  # A better alternative: real-time continuous trace

Step 4 — Reading traceroute output — what does * mean?

A * appears when a router does not return ICMP Time Exceeded — a firewall may be blocking ICMP, or the router is configured not to send it. It does NOT mean the network is broken there. If the following hops respond normally → the path is still working and that router is just 'silent'.

When it breaks: Wrongly concluding 'the network is broken at hop X' when you see * → escalating incorrectly → wasted time. Rule: only conclude 'broken' when all the hops after that hop are * AND the destination does not respond.

# Example output:
# 1  192.168.1.1      1.2 ms   — home router
# 2  10.0.0.1         5.1 ms   — ISP router
# 3  * * *            — router does not return ICMP
# 4  72.14.198.108   18.3 ms   — Google backbone
# 5  8.8.8.8         19.1 ms   — destination

# * = the router does not send Time Exceeded
# It does not mean the path is broken — look at the next hop

Observe with:

traceroute -n 8.8.8.8  # -n: do not resolve hostnames, faster
# After a * hop: if the next hop has a normal RTT → the router is silent, the path is fine
mtr --report -n 8.8.8.8  # Report mode: summarizes loss% and avg RTT for each hop

Step 5 — Telling the ICMP Destination Unreachable subtypes apart

RFC 792 defines ICMP Destination Unreachable Type 3 with codes that distinguish the reason: code 0 = net unreachable (the routing table has no route), code 1 = host unreachable, code 3 = port unreachable (UDP port closed — traceroute uses this to detect the destination). A firewall that silently DROPs instead of REJECTing with ICMP Unreachable → the UDP client has to wait for a timeout instead of failing immediately.

When it breaks: You see 'ICMP port unreachable' while debugging a UDP service → you are using the wrong port or the service is not running. Unlike TCP: TCP returns an RST when a port is closed, UDP returns ICMP Port Unreachable. When a firewall blocks ICMP → the UDP client waits for a timeout instead of failing immediately.

# UDP traceroute exploits Port Unreachable:
# Send UDP to a high port (33434+), each router returns Time Exceeded
# On reaching the destination: the host returns Port Unreachable (because nothing listens on that port)
# → traceroute knows it has arrived!

# ICMP traceroute (-I) uses Echo Request instead
# When the destination returns an Echo Reply → it finishes

Observe with:

sudo tcpdump -i any -n icmp  # Look for Type 3 Code X
# Code 0=net unreachable, 1=host unreachable, 3=port unreachable
# Test: nc -u 127.0.0.1 9999  # send UDP to a closed port → ICMP port unreachable

Symptoms when it breaks:

Fix:

  1. Allow ICMP type 3 (Destination Unreachable) on the firewall — REQUIRED for PMTU Discovery + UDP error handling

    sudo iptables -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT
    
  2. Verify by using nc to send UDP to a closed port; you should see the unreachable packet come back

    nc -u -w2 127.0.0.1 9999; echo "exit=$?"   # exit != 0 if an ICMP unreachable is received
    

Step 6 — Real-world ping — RTT and packet loss

RTT = the round-trip time from sending the ICMP Echo Request to receiving the Echo Reply, measured in milliseconds. A high RTT at an intermediate hop is not necessarily a bottleneck — routers prioritize forwarding traffic over handling ICMP (rate limiting/deprioritization). ping -s 1472 -M do (DF bit set) failing with 'Frag needed' per RFC 1191 → an MTU mismatch along the path, not a firewall block.

When it breaks: ping -s 1472 fails but ping -s 100 is OK → an MTU mismatch along the path (common with a VPN or tunnel). Large packets need fragmentation or PMTUD. The telltale signs: the DF (Don't Fragment) bit is set + an ICMP 'Fragmentation needed' from a router.

# Ping continuously, look at the min/avg/max RTT
ping 8.8.8.8
# Ctrl+C to stop, then read the summary

# Test with a larger packet (detects MTU issues)
ping -s 1400 8.8.8.8

# If ping with a large size fails but a small size is OK
# → there may be an MTU mismatch along the path

Observe with:

ping -c 10 8.8.8.8  # See packet loss % and rtt min/avg/max/mdev
mtr --report -c 20 8.8.8.8  # Measures jitter and loss more reliably
ping -s 1472 -M do 8.8.8.8  # MTU test: -M do = Don't Fragment, fails if MTU < 1500

Symptoms when it breaks:

Fix:

  1. Lower the interface MTU when the VPN/tunnel inserts headers (IPsec, WireGuard, GRE) → avoids fragmentation at distant routers

    sudo ip link set dev eth0 mtu 1400
    
  2. For TCP: clamp the MSS down to MTU-40 so the protocol does not have to depend on PMTUD

    sudo iptables -t mangle -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
    

Scripting it: Cron entry: */5 * * * * /usr/local/bin/net-probe.sh 8.8.8.8 → alert when loss > 5% or RTT avg > 100ms.

#!/usr/bin/env bash
# Monitor RTT + loss; alert if a threshold is exceeded. Run via cron */5.
TARGET="${1:-8.8.8.8}"
STATS=$(ping -c 20 -W 2 "$TARGET" | tail -2)
LOSS=$(echo "$STATS" | grep -oP '\d+(?=% packet loss)')
AVG=$(echo "$STATS" | grep -oP 'rtt [^=]+=\K[\d.]+(?=/[\d.]+/[\d.]+/[\d.]+)' | cut -d/ -f2)
LOSS=${LOSS:-100}; AVG=${AVG:-9999}
if (( LOSS > 5 )) || (( $(echo "$AVG > 100" | bc -l) )); then
  echo "ALERT: $TARGET loss=$LOSS%, rtt=${AVG}ms" | logger -t net-probe
fi

Step 7 — Observing ping with tcpdump

Run tcpdump -i any -n icmp and then ping -c 1 8.8.8.8 in another terminal. Map the tcpdump columns ↔ the IP/ICMP header fields (timestamp / src→dst / type echo request or reply / id+seq).

Observe with:

Match the id and seq columns between the request and the reply — they must pair up one to one

Common misconceptions

Monitoring based on ping alone → constant false alarms with cloud VPSes. The correct check: curl -sf http://host/health (L7) or nc -zv host port (L4) to confirm the service is really down.

Wrongly concluding 'the network is broken at hop X' when you see * → incorrect escalation, raising the incident to the wrong place. Only conclude it is broken when all the hops after that hop are * AND the destination does not respond.

Misreading TTL leads to misreading traceroute output and getting the hop count wrong when debugging. For example: reply TTL=54 from a Linux host → 64-54=10 hops on the way back.

Escalating a network problem to a router that is not the real bottleneck is the wrong direction. A real bottleneck: the high RTT starts at hop X and stays high for all the following hops, not just a single hop.

Misdiagnosing it as a firewall → opening an ICMP rule does not solve the problem. The correct fix: lower the interface MTU (sudo ip link set dev eth0 mtu 1400) or clamp the TCP MSS to avoid fragmentation.

libpcap requires an explicit interface; any is a Linux-specific pseudo-device and may not exist on macOS/BSD

Loopback traffic goes through the lo interface (a kernel virtual interface), not through a physical NIC

Try it at home

1. See ICMP Echo Request/Reply with your own eyes — confirm that ping uses no port and there is no TCP handshake

# Terminal 1: capture ICMP
sudo tcpdump -i any -n -v icmp

# Terminal 2: send pings
ping -c 3 8.8.8.8

# Observe: Type 8 (request) and Type 0 (reply)

Observation: # In tcpdump: 'ICMP echo request' and 'ICMP echo reply'

2. See the whole path hop by hop — understand the traceroute TTL trick in practice

# Linux (UDP mode)
traceroute -n 8.8.8.8

# Linux (ICMP mode - like Windows)
traceroute -I -n 8.8.8.8

# Windows
tracert 8.8.8.8

Observation: # Compare UDP vs ICMP mode: is the number of * different? mtr -n --report 8.8.8.8 # See packet loss % for each hop — more useful than a one-shot traceroute

3. Prove that ping OK ≠ service OK — a firewall can allow ICMP but block TCP

# Ping succeeds:
ping -c 3 8.8.8.8

# Check TCP ports (needs telnet or nc)
nc -vz 8.8.8.8 80   # Google HTTP
nc -vz 8.8.8.8 22   # Google SSH (usually blocked)

# Compare the results: which port is open vs closed

Observation: # ping OK + nc port fails = a firewall blocks that TCP port

4. Capture ICMP Time Exceeded from traceroute — see each router send an ICMP when the TTL expires

# Terminal 1: capture all ICMP
sudo tcpdump -i any -n icmp

# Terminal 2: run traceroute
traceroute -n -m 5 8.8.8.8

# Observe: ICMP time-exceeded from each intermediate router

Observation: # In tcpdump: 'ICMP time exceeded in-transit' from the IP of each router

5. Test MTU path discovery — detect an MTU mismatch using the ping packet size

# Ping with different packet sizes
ping -c 3 -s 1400 8.8.8.8   # 1400 bytes
ping -c 3 -s 1450 8.8.8.8   # 1450 bytes
ping -c 3 -s 1472 8.8.8.8   # Max Ethernet (1472 + 28 hdr = 1500)

# If a large size fails: 'Frag needed' = an MTU issue along the path

Observation: # If 1400 is OK but 1472 fails: sudo tcpdump -i any icmp # Look for 'ICMP frag needed' — it tells you the path's maximum MTU ping -s 1472 -M do 8.8.8.8 # -M do = Don't Fragment bit, fails more clearly if the MTU is low

6. Capture every ICMP packet on every interface, without resolving DNS (-n)

tcpdump -i any -n icmp

Observation: Run ping 8.8.8.8 in another terminal — watch the echo request/reply pair

7. The -X flag prints the payload as hex+ASCII, so you can see the ping data with its timestamp pattern

tcpdump -i any -n -X icmp

Observation: Compare the payload bytes between the request and the reply — the content is copied verbatim

8. Combined BPF filter: only ICMP to/from 8.8.8.8 (less noise)

tcpdump -i any -n 'icmp and host 8.8.8.8'

Observation: Verify that request → reply run as a proper pair, with the source/destination IPs swapped

Review questions

Question 1. A ping to the server 8.8.8.8 succeeds but curl reports 'connection refused port 80'. What is the cause?

  1. ICMP is blocked at the firewall
  2. Ping OK means L3 is working — but nothing is listening on TCP port 80, or a firewall blocks it (L4)
  3. DNS resolution failed
  4. The packet TTL is too low

Answer: Ping OK means L3 is working — but nothing is listening on TCP port 80, or a firewall blocks it (L4). Ping uses ICMP at L3 — it needs no port and no listening process. Ping OK only means the IP is reachable. 'Connection refused' = the TCP SYN arrived but no process is listening on port 80. The two are completely independent.

Question 2. How does traceroute work to learn the IP of each router along the path?

  1. Traceroute asks each router's routing table
  2. It sends packets with an increasing TTL (1,2,3...) — each router drops the packet when TTL=0 and sends an ICMP Time Exceeded carrying its own IP
  3. It uses a DNS reverse lookup for each hop
  4. It sends a TCP SYN to every IP in the subnet range

Answer: It sends packets with an increasing TTL (1,2,3...) — each router drops the packet when TTL=0 and sends an ICMP Time Exceeded carrying its own IP. This is the TTL trick: TTL=1 → the packet dies at hop 1, and hop 1 sends an ICMP Time Exceeded with its source IP. TTL=2 → it dies at hop 2, and you get hop 2's IP. And so on until the destination. Every router is required to send Time Exceeded under RFC 792.

Question 3. In traceroute output you see 'hop 5: * * *' followed by 'hop 6: 72.14.x.x (18ms)'. What does this mean?

  1. The network is broken at hop 5, and hop 6 is reached by another path
  2. Hop 5 timed out completely, and the connection was dropped
  3. The router at hop 5 does not send ICMP Time Exceeded (firewall/config) but still forwards packets — hop 6 proves the path is still working
  4. The TTL was miscalculated, and hop 5 does not exist

Answer: The router at hop 5 does not send ICMP Time Exceeded (firewall/config) but still forwards packets — hop 6 proves the path is still working. A * means the router at that hop does not return ICMP Time Exceeded — a firewall may be blocking ICMP or the router may be configured to rate-limit. BUT it still forwards packets (because hop 6 and the ones after it respond normally). The path is still working.

Question 4. Which layer of the OSI model does ICMP sit at?

  1. L4 Transport — because it is used to test connectivity
  2. L3 Network — ICMP is encapsulated in an IP packet (protocol number 1)
  3. L2 Data Link — because routers process ICMP
  4. L7 Application — because ping is an application

Answer: L3 Network — ICMP is encapsulated in an IP packet (protocol number 1). ICMP is an L3 protocol: it lives in the IP payload with IP protocol number = 1. Routers (L3 devices) process ICMP. It has no ports (L4) and no MAC (L2). Ping is merely an application that uses ICMP — it does not make ICMP an L7 protocol.

Question 5. Why does UDP traceroute stop when it receives an 'ICMP Port Unreachable' from the destination?

  1. Because port unreachable means the server is busy
  2. Because UDP traceroute sends to a high port (33434+) on which the destination host has no service listening — the host returns Port Unreachable, and traceroute understands it has arrived
  3. Because port 33434 is a special port of the traceroute protocol
  4. Because ICMP Port Unreachable has TTL=0

Answer: Because UDP traceroute sends to a high port (33434+) on which the destination host has no service listening — the host returns Port Unreachable, and traceroute understands it has arrived. UDP traceroute uses a clever design: it sends UDP to a very high port (33434+) to make sure no service is listening. When the packet reaches the destination, the host returns ICMP Type 3 Code 3 (Port Unreachable). Traceroute receiving this = it has reached the destination. Intermediate hops return Time Exceeded instead.

Question 6. The TTL value in a ping reply is 117. What does this tell you?

  1. The packet has passed through 117 routers
  2. The server's initial TTL is configured as 117
  3. If the server's default TTL=128, the packet has passed through 128-117=11 hops
  4. TTL carries no information about the hop count

Answer: If the server's default TTL=128, the packet has passed through 128-117=11 hops. Operating systems have fixed default TTLs: Linux=64, Windows=128, Cisco=255. When you receive a reply with TTL=117 and know the server runs Windows (original TTL=128), you can compute 128-117=11 hops. This is a way to estimate distance without traceroute.

Question 7. Which BPF filter captures BOTH ICMP echo request and echo reply?

  1. icmp
  2. icmp[icmptype] == 8
  3. icmp[icmptype] == 0
  4. host 8.8.8.8 and tcp

Answer: icmp. icmp does not filter by type → it captures all ICMP packets (request type 8, reply type 0, and other types such as unreachable).

Flashcards