# Network troubleshooting: follow the packet

Test from DNS to the application, compare refused and timed-out connections, and capture packets at a known boundary.

Course: [Networking for DevOps](https://devops.rajeev.pro/courses/networking), lesson 12. About 25 minutes, beginner. Source: https://devops.rajeev.pro/courses/networking/12-troubleshooting

## What you will learn

- Follow a request one network layer at a time
- Use tcpdump with a narrow interface and port filter
- Distinguish a refused connection from a timeout

> **Lab status.** **Verified lab:** two temporary Nginx containers and an Alpine client produced a successful HTTP response, a refused connection, a firewall-drop timeout, and a real tcpdump capture. The output is saved in the lab folder. The script removes its containers and Docker network on exit. <br />**Cloud lab:** none. No AWS resources were created.

When a page stops loading, restarting the service is the first reaction. It can hide the evidence without fixing
the path. A disciplined check follows the request from name lookup to the listening process, and stops where the
first check fails. A packet capture confirms what crossed one interface. In this lesson, you will compare common
TCP errors and use a short capture to narrow the fault.

## A layer-by-layer method

> **Figure.** A troubleshooting path checks DNS, route, TCP, TLS and HTTP, then application logs. Each successful check moves the investigation to the next boundary.

Write down the source, destination, port, protocol, and time. Then test in order:

1. **DNS:** does the client resolve the intended name?
2. **Route:** does the machine have a next hop for that address?
3. **TCP:** does the destination port accept a connection?
4. **TLS and HTTP:** is the certificate valid, and what status does the server return?
5. **Application:** do logs show the request and its dependency calls?

Each check proves only that boundary. A successful DNS lookup does not prove the route works. A successful TCP
connection does not prove the application can serve the request.

## Packet capture with tcpdump

> **Figure.** Capture a request on the client interface, check whether it crosses the network path, and capture on the server interface to see if it arrives or a reply leaves.

`tcpdump` prints packets seen on one interface. Choose the interface and filter narrowly:

```sh
tcpdump -i eth0 -nn -c 4 'tcp port 80'
```

`-i` selects an interface, `-nn` keeps addresses and ports numeric, and `-c` stops after a packet count. A client
capture can show a SYN leaving. A server capture can show whether it arrived and whether a reply left. A capture
does not reveal packets on interfaces you did not inspect, and payloads may contain sensitive data. Avoid writing
captures to shared locations.

## Real broken scenarios

> **Figure.** Three TCP outcomes: a fast reset suggests no listener or an active reject; no reply suggests a route or filter drop; a reset during a session points to the application or a middlebox.

The lab produced different responses for a closed port and a dropped port:

```text
$ nc -zvw 2 server 8081 (closed port)
nc: connect to server (172.19.0.2) port 8081 (tcp) failed: Connection refused
$ nc -zvw 2 server 8082 (firewall drop)
nc: connect to server (172.19.0.2) port 8082 (tcp) timed out: Operation in progress
```

Refused means something returned a reset; check the listener, bind address, or an explicit reject rule. A timeout
means no answer arrived by the deadline; check routes and silent drops. Neither message alone identifies the exact
device. Compare captures at both ends before changing firewall rules.

## Mission: Find the first failing boundary

Run the disposable lab: make one working HTTP request, compare a closed port with a filtered port, then capture the TCP handshake. The cleanup trap removes every container and network.

- You need: Docker
- You'll have: A real HTTP response, distinct TCP errors, and a four-packet capture

```sh
bash devops-academy-tools/networking-lab/12-capture.sh
```

Read the output from top to bottom. Notice that the closed port resets quickly, while the firewall drop returns no
answer before the timeout.

## Try it

- Write down the hostname, destination IP, port, and time for one failed request.
- Test DNS, route, TCP, TLS or HTTP in that order; stop at the first failure.
- Capture a short request on the client or server interface with `tcpdump`.
- Run the mini mission and compare a refused port with a firewall drop.

## Quiz

1. A TCP connection is refused immediately. What should you check first?
   - **Whether the application listens on that address and port** (answer)
   - The remote site's DNS TTL
   - The client's disk space
   - The HTTP response body
   Why: An immediate reset usually means the host responded but no listener accepted the port, or a device actively rejected it.

2. A TCP connection times out with no response. Which evidence helps locate the drop?
   - **Compare packet captures at both ends** (answer)
   - Change the DNS TTL only
   - Restart the client repeatedly
   - Disable all firewalls
   Why: A capture on each side shows whether the request arrives and whether a reply leaves.

3. Why should a packet capture use a narrow filter?
   - It hides all network errors
   - **It reduces noise and limits exposure of unrelated traffic** (answer)
   - It makes packets encrypted
   - It forces the server to reply
   Why: Interface, host, port, and protocol filters keep a capture focused and reduce the amount of sensitive traffic recorded.

## Resources

- [tcpdump(8)](https://man7.org/linux/man-pages/man8/tcpdump.8.html) (Linux man-pages)
- [traceroute(8)](https://man7.org/linux/man-pages/man8/traceroute.8.html) (Linux man-pages)
- [ip-route(8)](https://man7.org/linux/man-pages/man8/ip-route.8.html) (Linux man-pages)
