basforge

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

    RU

    -nn in tcpdump: always turn off hostname and port resolution

    Without -nn, tcpdump sends a reverse DNS (PTR) lookup for every new IP address and translates port numbers using /etc/services. On a server with slow DNS, output stalls for seconds and the capture fills with extra port 53 traffic.

    What breaks without -nn

    By default, tcpdump tries to make output readable: for every IP address it queries the system resolver for a reverse PTR record, and replaces port numbers with service names (80 becomes http, 443 becomes https, 22 becomes ssh). During troubleshooting this causes three problems at once:

    • Stalled output when DNS is broken. If the resolver is down (often the exact reason you run tcpdump), each line waits for the PTR lookup to time out. Meanwhile packets pile up in the kernel buffer and drop, pushing packets dropped by kernel up.
    • Observer effect. While tcpdump resolves IP addresses, it generates its own outbound UDP packets to port 53. Unless your filter excludes DNS (for example, tcpdump -i eth0), its own PTR queries flood the capture.
    • Harder grepping. When a line prints 203.0.113.10.443, grep ':443\|\.443' matches it cleanly, and a non-standard port like 8080 stays 8080 instead of turning into http-alt from /etc/services.

    Compare the same .pcap file (a connection to local port 80):

    # Without -n: PTR hostname and service name from /etc/services
    tcpdump -r /tmp/cap.pcap
    # 20:32:22.714428 IP localhost.49034 > localhost.http: Flags [S], seq 3950828314, win 65495, ...
    # 20:32:22.714444 IP localhost.http > localhost.49034: Flags [R.], seq 0, ack 3950828315, win 0, length 0
    
    # With -nn: raw IP addresses and numeric ports, zero DNS queries
    tcpdump -nn -r /tmp/cap.pcap
    # 20:32:22.714428 IP 127.0.0.1.49034 > 127.0.0.1.80: Flags [S], seq 3950828314, win 65495, ...
    # 20:32:22.714444 IP 127.0.0.1.80 > 127.0.0.1.49034: Flags [R.], seq 0, ack 3950828315, win 0, length 0

    Why -nn instead of -n

    In current tcpdump 4.99.4, a single -n turns off both IP address resolution and port name translation. In older builds and on some BSD systems, one -n disabled DNS lookups for hosts only, while the second -n disabled port and protocol names. Typing -nn works identically across every tcpdump version and server.

    The one case where -n has no technical effect is writing raw packets to a file with -w and no screen output: tcpdump skips DNS lookups there anyway. Passing -nn with -w is still harmless and protects you if you later drop -w.

    How to check

    Compare output from the same capture with and without -nn by sending a quick probe to port 80 on lo:

    sudo tcpdump -i lo -c 2 -w /tmp/nn-check.pcap 'tcp port 80' &
    sleep 0.5 && nc -z 127.0.0.1 80 2>/dev/null; wait
    tcpdump -r /tmp/nn-check.pcap
    tcpdump -nn -r /tmp/nn-check.pcap
    sudo rm -f /tmp/nn-check.pcap

    The first output shows localhost.http, the second shows 127.0.0.1.80.