basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    ManualLinuxtcpdump

    Troubleshooting failures with tcpdump: five practical scenarios

    How three or four lines of tcpdump separate a firewall DROP from a closed port, expose an MTU black hole, catch broken DNS, verify TLS SNI, and capture traffic inside a Docker container without installing tools into the image.

    Failure map by dump pattern

    Five common outages leave completely different traces in tcpdump. Switch scenarios to see where the chain breaks and which line to look for in the output:

    1. Client✓ sends SYN
    2. NIC / AF_PACKET✓ SYN in dump
    3. netfilter input✕ DROP
    sudo tcpdump -nn -i eth0 'host 198.51.100.20 and tcp port 443'
    20:10:01.102114 IP 198.51.100.20.51422 > 203.0.113.10.443: Flags [S], seq 1042100, length 0
    20:10:02.118090 IP 198.51.100.20.51422 > 203.0.113.10.443: Flags [S], seq 1042100, length 0
    20:10:04.134211 IP 198.51.100.20.51422 > 203.0.113.10.443: Flags [S], seq 1042100, length 0
    Cause

    Identical SYN packets with the same seq arrive at 1 s, 2 s, 4 s intervals with no reply: the incoming frame reached AF_PACKET, then the firewall dropped it silently (tcpdump sees packets before the firewall).

    Check with

    sudo nft list ruleset — look for a climbing drop counter in input or forward.

    1. Client✓ sends SYN
    2. Firewall✓ accept
    3. Socket :8080✕ nobody listening
    4. Kernel✕ sends RST
    sudo tcpdump -nn -i eth0 'host 198.51.100.20 and tcp port 8080'
    20:11:15.441002 IP 198.51.100.20.49034 > 203.0.113.10.8080: Flags [S], seq 3950828314, length 0
    20:11:15.441018 IP 203.0.113.10.8080 > 198.51.100.20.49034: Flags [R.], seq 0, ack 3950828315, win 0, length 0
    Cause

    Within a fraction of a millisecond the server replied with Flags [R.]: the port is closed, the service only binds 127.0.0.1 (0.0.0.0 vs 127.0.0.1), or the firewall rule is REJECT (DROP vs REJECT).

    Check with

    sudo ss -tulnp 'sport = :8080' — verify whether the service is running and which IP it binds.

    1. TCP handshake✓ small packets
    2. TLS / data 1448 B✕ never arrives
    3. ICMP Frag Needed✕ blocked in path
    sudo tcpdump -nn -v -i eth0 'host 198.51.100.20 and (tcp port 443 or icmp)'
    20:12:00.100012 IP (flags [DF], length 60) 198.51.100.20.52100 > 203.0.113.10.443: Flags [S], options [mss 1460]
    20:12:00.100040 IP (flags [DF], length 60) 203.0.113.10.443 > 198.51.100.20.52100: Flags [S.], options [mss 1460]
    20:12:00.145200 IP (flags [DF], length 1500) 203.0.113.10.443 > 198.51.100.20.52100: Flags [.], seq 1:1449, length 1448
    20:12:00.351004 IP (flags [DF], length 1500) 203.0.113.10.443 > 198.51.100.20.52100: Flags [.], seq 1:1449, length 1448
    Cause

    The tiny SYN/SYN-ACK gets through, while the first full 1500-byte segment with DF repeats with the same seq 1:1449 and gets no ACK — a tunnel along the path has a lower MTU and ICMP Type 3 Code 4 is blocked (MTU blackhole).

    Check with

    ping -M do -c 1 -s 1472 198.51.100.20 (lower -s until replies return) and MSS clamping on the router.

    1. App✓ A/AAAA query
    2. Resolver :53✕ ServFail / silence
    sudo tcpdump -nn -i any 'port 53'
    20:13:05.001200 eth0 Out IP 10.0.0.11.44102 > 1.1.1.1.53: 51204+ A? api.example.org. (33)
    20:13:05.024810 eth0 In  IP 1.1.1.1.53 > 10.0.0.11.44102: 51204 ServFail 0/0/0 (33)
    Cause

    The DNS reply returns ServFail (or NXDomain, or repeated queries get no answer at all) — the client never gets to open an IP connection (DNS and dig).

    Check with

    dig @1.1.1.1 api.example.org compared against system dig api.example.org and resolvectl status.

    1. TCP :443✓ SYN-ACK + ACK
    2. ClientHello (SNI)✓ sent
    3. Proxy / server✕ Alert / RST
    sudo tcpdump -nn -i eth0 'host 198.51.100.20 and tcp port 443'
    20:14:10.010100 IP 198.51.100.20.53010 > 203.0.113.10.443: Flags [P.], seq 1:518, ack 1, length 517
    20:14:10.011420 IP 203.0.113.10.443 > 198.51.100.20.53010: Flags [P.], seq 1:8, ack 518, length 7
    20:14:10.011455 IP 203.0.113.10.443 > 198.51.100.20.53010: Flags [F.], seq 8, ack 518, length 0
    Cause

    Right after the 517-byte ClientHello, the server returned 7 bytes (a TLS Alert) and closed with FIN — mismatched SNI, TLS version, or cipher list (HTTP and TLS).

    Check with

    openssl s_client -connect 203.0.113.10:443 -servername example.org </dev/null and proxy logs.

    Step by step: taking and reading diagnostic dumps

    1. Check basic reachability: SYN, SYN-ACK, RST

      When a service “doesn’t respond”, skip capturing the whole stream — grab only handshakes and resets for the affected client and port. Adding -ttt highlights the gaps between retries right away:

      sudo tcpdump -nn -ttt -i eth0 'host 198.51.100.20 and tcp port 443 and (tcp[tcpflags] & (tcp-syn|tcp-rst|tcp-fin) != 0)'
      • Only Flags [S] arrives every 1, 2, 4 seconds — the local firewall (nftables/ufw) is dropping packets before the application sees them.
      • Flags [R.] goes out immediately — the port is closed or a REJECT rule fired.
      • Flags [S.] goes out, yet the client resends Flags [S] — the reply never makes it back to the client (check the return route and any upstream firewall).
    2. Catch MTU issues and mid-connection stalls

      When connections open fine and then freeze during data transfer, add -v (to see IP length and the DF flag) and include icmp (IPv4) or icmp6 (IPv6) in the filter:

      sudo tcpdump -nn -v -i eth0 'host 198.51.100.20 and (tcp port 443 or icmp)'

      Look for two signs:

      1. The same seq 1:1449 range repeating with IP length 1500 and flags [DF] — large segments don’t fit through the link and the sender never received notice to shrink them.
      2. ICMP ... unreachable - need to frag (mtu 1420) — a router properly reported the smaller MTU, so if 1500-byte packets still keep leaving afterward, inbound ICMP is being dropped on your server.
    3. Trace DNS queries and lookup timeouts

      tcpdump decodes DNS headers out of the box: query ID, record type (A?, AAAA?, PTR?, SRV?), response code (NXDomain, ServFail, Refused), and returned IPs. Capture on -i any so you see both calls to local 127.0.0.53 (systemd-resolved) and upstream queries leaving eth0:

      sudo tcpdump -nn -i any 'port 53'
      # 20:13:05.000110 lo   In  IP 127.0.0.1.38410 > 127.0.0.53.53: 18291+ A? example.org. (29)
      # 20:13:05.000420 eth0 Out IP 10.0.0.11.44102 > 1.1.1.1.53: 51204+ A? example.org. (29)
      # 20:13:05.019105 eth0 In  IP 1.1.1.1.53 > 10.0.0.11.44102: 51204 1/0/0 A 203.0.113.10 (45)
      # 20:13:05.019240 lo   In  IP 127.0.0.53.53 > 127.0.0.1.38410: 18291 1/0/0 A 203.0.113.10 (45)

      If queries hit 127.0.0.53 while eth0 stays silent, check the systemd-resolved config or cache. If queries leave eth0 with no reply, outbound port 53 is blocked or the upstream resolver is unreachable.

    4. Verify which SNI leaves in TLS ClientHello

      When you see a certificate error (TRAEFIK DEFAULT CERT or wrong cert), check whether the client sends the expected domain in the SNI extension. The hostname sits in plaintext inside ClientHello:

      # Capture only ClientHello packets on port 443 and print readable strings:
      sudo tcpdump -nn -A -i eth0 -c 3 'tcp port 443 and (tcp[((tcp[12:1] & 0xf0) >> 2):1] = 0x16) and (tcp[(((tcp[12:1] & 0xf0) >> 2) + 5):1] = 0x01)'

      Among the non-printable bytes of -A output, look for the plaintext hostname (for example example.org). If it is missing or shows an old domain, the fault is on the client side (connecting by raw IP without --resolve, or a stale reverse-proxy config).

    5. Capture inside a Docker container without installing tools

      Minimal images like alpine, distroless, and scratch don’t include tcpdump, and there is no need to install packages inside a running container. Run the host’s tcpdump inside the container’s network namespace (netns) via nsenter (Linux network stack):

      # Find the PID of the container's main process:
      PID=$(docker inspect -f '{{.State.Pid}}' traefik)
      
      # Run host tcpdump inside the container's network namespace:
      sudo nsenter -t "$PID" -n tcpdump -nn -i any 'tcp port 8080'

      This shows traffic strictly from the container’s point of view — after host DNAT and before host SNAT, free of noise from other services on the server. To compare before and after NAT, open two terminals: one on the host’s eth0, the other through nsenter inside the container.

    If it doesn’t work

    External packets for a container arrive on eth0, but the br-* bridge stays silent

    Cause
    The packet was dropped in the firewall forward chain (after DNAT in prerouting), or net.ipv4.ip_forward = 1 is disabled.
    Fix
    Check sysctl net.ipv4.ip_forward and drop counters in the forward chain (sudo nft list ruleset, Docker ports bypass the input chain).

    eth0 shows incoming SYN and outgoing SYN-ACK, yet the client still times out and keeps resending SYN

    Cause
    The returning SYN-ACK is lost on its way back to the client — usually because of asymmetric routing, a wrong gateway, or a firewall at the provider or client side.
    Fix
    Check ip route get <client_IP> on the server and run a simultaneous capture on the client (or intermediate router): if SYN-ACK leaves eth0 but never reaches the client, the break is on the return path.

    tcpdump -nn -i eth0 'tcp port 443' shows only encrypted packets, so you cannot tell which domain the client asked for

    Cause
    Everything after the handshake is TLS-encrypted, but the hostname (SNI) travels in plaintext inside the first ClientHello packet.
    Fix
    Capture ClientHello with -A or -X, or extract it from a saved .pcap via tshark -r cap.pcap -Y 'tls.handshake.type == 1' -T fields -e tls.handshake.extensions_server_name.

    nsenter -t <PID> -n tcpdump … against a container reports No such device for eth0

    Cause
    The container runs with network_mode: host or none, or it restarted and got a new PID.
    Fix
    Re-read the PID with docker inspect -f '{{.State.Pid}}' <container> and list interfaces via sudo nsenter -t <PID> -n ip -br link.

    How to check

    Run a short capture on eth0 or lo against a port you know is closed and confirm you get a Flags [S] followed immediately by Flags [R.]:

    sudo tcpdump -nn -ttt -i lo -c 2 'tcp port 8089' &
    sleep 0.5 && nc -zv -w 1 127.0.0.1 8089 2>/dev/null; wait

    The -ttt delta between the Flags [S] line and the Flags [R.] line should be a fraction of a millisecond — the textbook signature of a closed port with no firewall DROP.