Network problems often need packet evidence. tcpdump captures packets on a selected Linux interface to verify the traffic involved in a connection failure.

The tcpdump, ip, and ping commands must already be installed, and live capture requires permission to run tcpdump with sudo. Tcpdump reads packets through libpcap and prints decoded summaries or saves packets for later inspection.

Saved PCAP files can contain internal addresses, payload bytes, cookies, tokens, and timing details. Capture only traffic you are authorized to inspect, keep the peer and capture window narrow, store files where only the right operators can read them, and remove temporary captures after the evidence has been checked.

Steps to capture network traffic with tcpdump in Linux:

  1. List the interfaces tcpdump can capture from.
    $ sudo tcpdump -D
    1.eth0 [Up, Running, Connected]
    2.any (Pseudo-device that captures on all interfaces) [Up, Running]
    3.lo [Up, Running, Loopback]
    ##### snipped #####
    17.dbus-session (D-Bus session bus) [none]

    The controlled test uses lo because both the request and reply stay on loopback; the later capture uses the interface carrying traffic to a peer.
    Related: How to select a capture interface in tcpdump

  2. Start a count-limited capture in one terminal.
    $ sudo tcpdump --interface=lo -nn -c 4 icmp
    tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
    listening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytes

    -nn keeps addresses and ports numeric, and -c 4 stops after four matching packets. The count is not a time limit; without matching traffic, the capture keeps waiting.

  3. Generate matching traffic in a second terminal.
    $ ping -c 2 127.0.0.1
    PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
    64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.650 ms
    64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.071 ms
    
    --- 127.0.0.1 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1057ms
    rtt min/avg/max/mdev = 0.071/0.360/0.650/0.289 ms
  4. Stop the capture with Ctrl+C in its terminal if it is still waiting after the test.

    Ctrl+C ends the capture even when fewer than four packets arrived. With no matching traffic, the summary reports 0 packets captured.

  5. Confirm the matching packets appeared in the tcpdump terminal.
    20:52:51.031325 IP 127.0.0.1 > 127.0.0.1: ICMP echo request, id 12, seq 1, length 64
    20:52:51.031335 IP 127.0.0.1 > 127.0.0.1: ICMP echo reply, id 12, seq 1, length 64
    20:52:52.088229 IP 127.0.0.1 > 127.0.0.1: ICMP echo request, id 12, seq 2, length 64
    20:52:52.088246 IP 127.0.0.1 > 127.0.0.1: ICMP echo reply, id 12, seq 2, length 64
    4 packets captured
    8 packets received by filter
    0 packets dropped by kernel

    Loopback can report more packets received by the filter than packets captured. The 4 packets captured line confirms the -c 4 stop condition. This proves local capture access, not traffic on an Ethernet or Wi-Fi link.
    Related: How to capture ICMP packets with tcpdump

  6. Check the route to an IPv4 peer involved in the network problem.
    $ ip route get 198.51.100.2
    198.51.100.2 dev eth0 src 198.51.100.3 uid 1001
        cache

    For your network, replace 198.51.100.2 with the peer's IPv4 address. The dev value identifies the outgoing interface; eth0 is the interface used here. Interface names and addresses vary.

  7. Create a private capture file in your home directory.
    $ capture_file=$(mktemp "$HOME/packet-capture.XXXXXX")

    mktemp creates a new file owned by your account with no group or other access. The capture_file variable holds its generated path in this terminal.

  8. Start a capture of IPv4 packets to or from the peer in the same terminal.
    $ sudo tcpdump --interface=eth0 -nn -c 4 -w - 'ip and host 198.51.100.2' > "$capture_file"
    tcpdump: listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes

    For your network, substitute the interface from the route lookup and the same peer address. The filter includes the peer's IPv4 traffic across protocols. -w - sends raw packets to standard output, and your unprivileged shell redirects them into the private file without changing its owner. Packet summaries appear only when the file is read.
    Related: How to filter network traffic in tcpdump
    Related: How to save traffic to a PCAP file using tcpdump

  9. Generate traffic to the same peer from the second terminal.
    $ ping -c 2 -W 2 198.51.100.2

    For a service problem, reproduce the affected connection instead of using ping. A peer or firewall may block ICMP replies; outgoing requests alone do not prove that the peer answered.

  10. Stop the saved capture with Ctrl+C in its terminal if it is still waiting after the test.

    The packet count still has no deadline. The stop summary reports the number actually captured; zero means no matching packets reached this capture. A nonzero kernel-drop count means some packets were lost during capture.

  11. Check that the capture file is owned by your account and has no group or other permissions.
    $ ls -l "$capture_file"
    -rw------- 1 user user 480 Nov  5 07:53 /home/user/packet-capture.vjhXMU

    The owner should be your login account; the generated filename and timestamp will differ. The -rw------- mode restricts file access to that owner.

  12. Read the saved packets to confirm the peer traffic was captured.
    $ tcpdump -nn -r "$capture_file" -c 4
    reading from file /home/user/packet-capture.vjhXMU, link-type EN10MB (Ethernet), snapshot length 262144
    07:53:17.057993 IP 198.51.100.3 > 198.51.100.2: ICMP echo request, id 35812, seq 1, length 64
    07:53:17.058077 IP 198.51.100.2 > 198.51.100.3: ICMP echo reply, id 35812, seq 1, length 64
    07:53:18.058639 IP 198.51.100.3 > 198.51.100.2: ICMP echo request, id 35812, seq 2, length 64
    07:53:18.058721 IP 198.51.100.2 > 198.51.100.3: ICMP echo reply, id 35812, seq 2, length 64

    The request and reply lines show both directions of the peer test on eth0. An empty capture prints the file header without packet lines. Reading a saved PCAP needs file permission, not capture privileges.
    Related: How to read a saved PCAP file using tcpdump
    Related: How to open and analyze PCAP files using Wireshark

Share this guide