basforge

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

    RU

    Network diagnostics on a Linux server: layer by layer from interface to TLS

    When a site, API, or database stops responding, a bottom-up check across TCP/IP layers separates a broken route from DNS, a closed port, a firewall rule, or a certificate error in two minutes. Walk six steps with standard Linux tools — no guesswork and no blind restarts.

    Where to start: symptom map

    You don’t need to start at step 1 every time: the client error tells you which layers already succeeded.

    Client symptom Where to start
    Network is unreachable or No route to host Steps 1–2: interface, ARP, and ip route get
    Could not resolve host or Temporary failure in name resolution Step 3: dig vs @1.1.1.1 and resolvectl status
    Instant Connection refused Step 4: sudo ss -tulnp on the server (0.0.0.0 vs 127.0.0.1)
    Connection timed out after 10–30 seconds Steps 5–6: nftables rules, route, and tcpdump (DROP vs REJECT)
    SSL certificate problem or 502 Bad Gateway Step 6: curl -svkI, openssl s_client, and the backend socket
    Short commands work, large responses hang Step 2: MTU probe with ping -M do -s … (MTU blackhole)

    Switch scenarios below to see how the same sequence of commands eliminates working layers and narrows the fault to a single point:

    1. L2/L3 · ip route✓ 10.0.0.1
    2. DNS · dig✕ SERVFAIL
    dig +noall +answer +comments example.org
    # ;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 19412
    dig @1.1.1.1 +short example.org
    # 203.0.113.10
    Cause

    Name resolution succeeds through 1.1.1.1, while the local resolver returns SERVFAIL — the fault sits in system DNS, outside routing and the remote server.

    Check with

    resolvectl status and journalctl -u systemd-resolved -n 30

    1. L2/L3 · ip route✓ 10.0.0.1
    2. DNS · dig✓ 203.0.113.10
    3. L4 · nftables✕ DROP :443
    nc -zv -w 3 203.0.113.10 443
    # nc: connect to 203.0.113.10 port 443 (tcp) timed out: Operation now in progress
    sudo tcpdump -nn -i eth0 'host 198.51.100.20 and tcp port 443'
    # IP 198.51.100.20.51422 > 203.0.113.10.443: Flags [S], length 0  (no reply)
    Cause

    SYN packets reach the server’s NIC, then disappear in the firewall’s input or forward chain — neither SYN-ACK nor RST comes back.

    Check with

    sudo nft list ruleset (check drop rules and counters in input/forward)

    1. L2/L3 · ip route✓ reached host
    2. L4 · nftables✓ accept :8080
    3. Socket · ss✕ 127.0.0.1:8080
    4. Client✕ RST · refused
    nc -zv -w 2 203.0.113.10 8080
    # nc: connect to 203.0.113.10 port 8080 (tcp) failed: Connection refused
    sudo ss -tulnp 'sport = :8080'
    # tcp LISTEN 0 4096 127.0.0.1:8080 0.0.0.0:* users:(("docker-proxy",pid=1821,fd=7))
    Cause

    The service is running, yet listens only on the 127.0.0.1 loopback interface. Nothing listens on the external IP, so the kernel immediately sends RST.

    Check with

    sudo ss -tulnp — look at Local Address:Port

    1. L2/L3 · route✓ 18 ms
    2. DNS · dig✓ 203.0.113.10
    3. L4 · TCP :443✓ SYN-ACK
    4. L7 · TLS✕ wrong cert
    curl -svI https://example.org 2>&1 | grep -E 'Connected|subject:|SSL certificate'
    # * Connected to example.org (203.0.113.10) port 443
    # *  subject: CN=TRAEFIK DEFAULT CERT
    # * SSL certificate problem: unable to verify the first certificate
    Cause

    Network, DNS, and TCP are fine. The proxy on 443 matched no router for the sent SNI, or failed to renew its Let’s Encrypt certificate.

    Check with

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

    Step by step

    1. Check interface, address, and neighbor (L2–L3)

      Verify that the NIC is UP, carries the expected IP and mask (IPv4, masks, and CIDR), and resolves the gateway over ARP (MAC, switch, ARP, and VLAN):

      ip -br link
      # lo               UNKNOWN        00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
      # eth0             UP             bc:24:11:af:27:2b <BROADCAST,MULTICAST,UP,LOWER_UP>
      
      ip -4 -br addr
      # lo               UNKNOWN        127.0.0.1/8
      # eth0             UP             10.0.0.11/24
      
      ip neigh show dev eth0
      # 10.0.0.1 lladdr e2:a1:e8:41:67:85 REACHABLE

      NO-CARRIER or DOWN points to a physical cable, VLAN, or /etc/netplan/ config issue. INCOMPLETE or FAILED on the gateway means the server receives no ARP reply from the local segment.

    2. Check route, packet loss, and MTU (L3)

      Ask the kernel which interface and source IP it picks to reach the target (Routing and LPM):

      ip route get 1.1.1.1
      # 1.1.1.1 via 10.0.0.1 dev eth0 src 10.0.0.11 uid 1000

      If a Docker bridge br-… shows up here instead of eth0, a container subnet overlaps the target’s address space (Docker subnet clashes with home network).

      Next, trace hop-by-hop loss, and if sessions stall mid-transfer, test the maximum unfragmented MTU:

      mtr -n -r -c 10 1.1.1.1
      # HOST: web1                        Loss%   Snt   Last   Avg  Best  Wrst StDev
      #   1.|-- 10.0.0.1                   0.0%    10    0.2   0.2   0.2   0.3   0.0
      #   ...
      
      ping -M do -c 1 -W 1 -s 1472 203.0.113.10

      1472 payload bytes plus 20 bytes of IPv4 and 8 bytes of ICMP equal 1500 bytes. If 1472 fails while 1464 or 1392 replies, a PPPoE link or VPN tunnel on the path lowers the MTU (MTU blackhole).

    3. Separate DNS from network connectivity (L7 · DNS)

      Compare the system resolver against an independent public server (DNS and dig):

      dig +noall +answer +stats example.org A
      # example.org.          59   IN   A   203.0.113.10
      # ;; Query time: 4 msec
      # ;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
      
      dig @1.1.1.1 +short example.org A
      # 203.0.113.10

      If @1.1.1.1 returns an address while plain dig times out or returns SERVFAIL, inspect systemd-resolved:

      resolvectl status

      Check AAAA records too: when a domain publishes IPv6 and the server has broken IPv6 routing, curl and apt try IPv6 first and burn seconds before falling back to IPv4 (IPv6 in practice).

    4. Check the port and listening process (L4)

      From the client, probe the specific TCP port directly instead of running ping (ping is not a port check):

      nc -zv -w 3 203.0.113.10 443
      # Connection to 203.0.113.10 443 port [tcp/https] succeeded!

      On the server, check which process owns the port, which address it binds to, and what state sockets are in (Ports and sockets, TCP: flags and states):

      sudo ss -tulnp 'sport = :443 or sport = :8080'
      # Netid State  Recv-Q Send-Q Local Address:Port Peer Address:Port Process
      # tcp   LISTEN 0      4096      127.0.0.1:8080       0.0.0.0:*   users:(("docker-proxy",pid=1821,fd=7))
      
      ss -s
      # Total: 220
      # TCP:   40 (estab 5, closed 24, orphaned 0, timewait 7)

      Three facts jump out right away: whether the process is running, whether it bound to 127.0.0.1 instead of 0.0.0.0, and whether sockets are piling up in CLOSE-WAIT (an application leak) or filling Recv-Q (the service cannot keep up with reading).

    5. Check the firewall and container network namespace

      If the process listens on 0.0.0.0 or the right interface IP and the client still times out, check netfilter rules in both hooks — input (host services) and forward (containers published with -p, Linux network stack):

      sudo nft list ruleset

      When a distroless or scratch container lacks ss and ip, enter its network namespace from the host via nsenter:

      PID=$(docker inspect -f '{{.State.Pid}}' app)
      sudo nsenter -t "$PID" -n ss -tulnp
      sudo nsenter -t "$PID" -n ip route

      The command runs host binaries while inspecting the sockets, interfaces, and routes inside the container.

    6. Check TLS, HTTP, and capture traffic when stuck

      Measure the response, inspect the certificate, and time each phase (HTTP and TLS):

      curl -sS -o /dev/null \
        -w 'dns=%{time_namelookup}s tcp=%{time_connect}s tls=%{time_appconnect}s ttfb=%{time_starttransfer}s total=%{time_total}s code=%{http_code}\n' \
        https://example.org
      # dns=0.004s tcp=0.022s tls=0.064s ttfb=0.118s total=0.121s code=200
      
      openssl s_client -connect example.org:443 -servername example.org </dev/null 2>/dev/null \
        | openssl x509 -noout -subject -issuer -dates
      # subject=CN = example.org
      # issuer=C = US, O = Let's Encrypt, CN = E6
      # notBefore=Nov  1 00:00:00 2026 GMT
      # notAfter=Jan 30 00:00:00 2027 GMT

      If the service sits behind a reverse proxy or CDN and you want to hit the origin directly without editing DNS, override the IP for one request with --resolve (keeping the right Host header and SNI):

      curl --resolve example.org:443:203.0.113.10 -svI https://example.org

      When logs stay silent and you need to know whether packets arrive at all, capture on the server with tcpdump and -nn (skipping reverse DNS and port-name lookups):

      sudo tcpdump -nn -i any 'host 198.51.100.20 and tcp port 443'

      If incoming Flags [S] appears without Flags [S.] or Flags [R.] going back, the local firewall stopped the packet (AF_PACKET sits ahead of netfilter, Linux network stack).

    How to read curl -w timings

    Values printed by curl -w are cumulative from command start, so the duration of each stage is the difference from the previous number:

    curl -sS -o /dev/null -w '...' https://example.org · Pick a segment
    When tcp takes 18 ms and ttfb takes 3.5 seconds, the network is healthy: the backend or a database query is slow.

    If it doesn’t work

    ping 1.1.1.1 replies, but curl https://example.org fails with Could not resolve host

    Cause
    L3 routing works, but the DNS resolver is failing (systemd-resolved is down, UDP/TCP 53 is blocked, or /etc/resolv.conf points to the wrong server).
    Fix
    Compare dig example.org against dig @1.1.1.1 example.org, then inspect the system resolver with resolvectl status.

    nc -zv 203.0.113.10 443 hangs until timeout, while sudo tcpdump -nn on the server shows incoming Flags [S]

    Cause
    tcpdump taps incoming traffic via AF_PACKET before netfilter chains — the packet landed in the dump, then the firewall silently discarded it (DROP).
    Fix
    Check rules and packet counters with sudo nft list ruleset: look for drop in the input hook (host services) and forward hook (Docker containers, Docker ports bypass the input chain).

    On the server itself curl http://127.0.0.1:8080 returns 200 OK, while another machine gets an instant Connection refused

    Cause
    The process is bound to 127.0.0.1:8080 instead of 0.0.0.0:8080 (or the interface IP), or Docker published the port only on loopback.
    Fix
    Check the Local Address:Port column in sudo ss -tulnp and update the service bind address (0.0.0.0 vs 127.0.0.1).

    mtr shows 50% packet loss on an intermediate router, while the final hop reports 0.0%

    Cause
    The intermediate router rate-limits ICMP Time Exceeded replies from its CPU, while forwarding transit traffic without loss.
    Fix
    Read loss at the final hop, and from the first hop where loss stays non-zero all the way to the destination (Routing and LPM).

    The TCP handshake and a short GET /health succeed, but loading a full page or running ls -la over SSH hangs

    Cause
    An MTU blackhole: a VPN or PPPoE tunnel on the path has an MTU below 1500, and a firewall drops ICMP Fragmentation Needed.
    Fix
    Find the largest unfragmented payload with ping -M do -c 1 -s 1472 <IP> and enable MSS clamping on the router (MTU blackhole).

    How to check

    Run a quick self-check on the server: an open local port, a known closed port, DNS resolution, and HTTPS timings.

    nc -zv -w 2 127.0.0.1 22
    # Connection to 127.0.0.1 22 port [tcp/ssh] succeeded!
    
    nc -zv -w 2 127.0.0.1 9999
    # nc: connect to 127.0.0.1 port 9999 (tcp) failed: Connection refused
    
    dig +short example.org A
    curl -sS -o /dev/null -w 'dns=%{time_namelookup}s tcp=%{time_connect}s tls=%{time_appconnect}s ttfb=%{time_starttransfer}s code=%{http_code}\n' https://example.org

    The open port returns succeeded!, the closed one returns an immediate Connection refused, and curl -w prints non-zero timings for each phase with code=200.