Lab 15: Network Diagnostics & Troubleshooting

Time: 30 minutes | Level: Practitioner | Docker: docker run -it --rm ubuntu:22.04 bash


Overview

This lab covers the essential toolkit for diagnosing network problems: testing connectivity with ping, tracing packet paths with traceroute/tracepath, resolving DNS with dig/nslookup, inspecting socket states with ss, testing ports with netcat, and profiling HTTP with curl timing.


Step 1: ping — Testing Connectivity

apt-get update -qq && apt-get install -y iputils-ping

# Basic ping (3 packets)
ping -c 3 8.8.8.8

# Ping with timeout and interval
ping -c 5 -W 2 -i 0.5 8.8.8.8

# Ping by hostname
ping -c 2 google.com

💡 Flags: -c N = send N packets (without it, ping runs forever); -W N = wait N seconds for each reply (timeout); -i N = interval between packets in seconds. RTT (round-trip time) in ms tells you latency. Packet loss % shows reliability. No response could mean: host down, firewall blocking ICMP, or routing failure.

📸 Verified Output:


Step 2: traceroute — Path Discovery

traceroute reveals every router hop between you and a destination.

💡 traceroute works by sending packets with increasing TTL (Time To Live). Each router decrements TTL by 1; when TTL hits 0, the router sends back ICMP "time exceeded" — revealing its IP. * * * means a router didn't respond (filtered) — not necessarily a problem if the final destination responds.

📸 Verified Output:


Step 3: tracepath — MTU Discovery

tracepath is similar to traceroute but also discovers Path MTU (Maximum Transmission Unit).

💡 pmtu (Path MTU) is the largest packet size that can traverse the entire path without fragmentation. The standard Ethernet MTU is 1500 bytes. If you see pmtu 1500 that's normal. A smaller value (e.g., 1400) indicates a link on the path uses a smaller MTU (common with VPNs and tunnels). MTU mismatches cause mysterious connection hangs.

📸 Verified Output:


Step 4: DNS Diagnostics with dig and nslookup

💡 dig output includes: ANSWER SECTION (the results), Query time (DNS server latency), SERVER (which nameserver answered). +short strips everything except the answer. Always check both the answer AND which server responded — wrong server = stale cache or misconfiguration.

📸 Verified Output:


Step 5: /etc/resolv.conf and /etc/hosts

💡 /etc/hosts is checked BEFORE DNS (by default). This makes it useful for: overriding DNS in dev (point domain to local server), blocking sites (point to 127.0.0.1), and quick testing. /etc/resolv.conf lists nameservers (nameserver lines) and search domains (search line for short hostname expansion). Docker auto-generates this file.

📸 Verified Output:


Step 6: Socket States with ss

💡 TCP socket states: LISTEN = waiting for connections; ESTABLISHED = active connection; TIME_WAIT = connection closed, waiting for late packets (normal, lasts ~60s); CLOSE_WAIT = remote closed, local hasn't (possible app bug if many); SYN_SENT = actively connecting. A server with thousands of TIME_WAIT is seeing normal high traffic. Thousands of CLOSE_WAIT may indicate a connection leak.

📸 Verified Output:


Step 7: netcat (nc) — Port Testing

💡 nc -zv = scan mode (zero I/O) + verbose. Exit code 0 = port open; non-zero = closed or filtered. The difference: closed sends TCP RST (fast fail); filtered times out (slow, firewall DROP). Use -w to set timeout and avoid hanging. nc can also create simple TCP servers/clients — great for testing network connectivity between two machines.

📸 Verified Output:


Step 8: Capstone — Network Diagnostic Runbook

Scenario: A web application is unreachable. Run a systematic diagnostic to identify whether the issue is DNS, routing, firewall, or application-level.

💡 Diagnosis decision tree: DNS fails → check /etc/resolv.conf and nameserver. DNS OK, ping fails → routing or firewall issue. Ping OK, port closed → application not running or firewall blocking specific port. Port open, HTTP fails → application error (check app logs). HTTP slow → check timing breakdown for which phase is slow (DNS/TLS/TTFB).

📸 Verified Output (excerpt):


Summary

Tool / Command
Purpose

ping -c N HOST

Test ICMP connectivity, measure RTT

ping -W N

Set per-packet timeout (seconds)

traceroute HOST

Show each router hop to destination

tracepath HOST

Trace + discover Path MTU

dig HOST A +short

DNS A record lookup (brief)

dig @SERVER HOST

Query specific DNS server

dig -x IP

Reverse DNS lookup (PTR record)

nslookup HOST

Interactive/one-shot DNS query

cat /etc/resolv.conf

View DNS server configuration

cat /etc/hosts

View static hostname mappings

getent hosts NAME

Resolve via full NSS chain

ss -tan

Show TCP sockets with state

ss -s

Socket statistics summary

nc -zv HOST PORT

Test if TCP port is open

nc -zuv HOST PORT

Test if UDP port is open

curl -w "fmt" URL

HTTP timing breakdown

ip route show

View routing table

DNS → Ping → Port → HTTP

Systematic diagnostic order

Last updated