tcpdump Command in Linux: Packet Capture and Network Analysis
Learn tcpdump command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.
10 min read
Learn tcpdump command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.
- What tcpdump Does and When to Use It
- Availability and Installation Across Linux Distributions
- Command Syntax, Arguments, and Exit Status Codes
Mastering the tcpdump command in linux provides system administrators, security analysts, and network engineers with an indispensable tool for low-level packet capture and network protocol troubleshooting. Originally written in 1988 by Van Jacobson, Craig Leres, and Steven McCanne at Lawrence Berkeley Laboratory, tcpdump intercepts and displays the contents of network frames traversing local network interfaces.
The tcpdump command in Linux is the premier command-line packet analyzer used to capture, filter, and inspect network traffic passing through network interfaces in real time. Leveraging libpcap and Berkeley Packet Filters (BPF), tcpdump intercepts raw Ethernet and IP frames at the kernel level and writes standard .pcap files for offline deep-packet analysis in Wireshark.
Modern cloud and data center operations present complex connectivity challenges: TLS handshake failures, unexpected TCP resets (RST), broadcast storms, DNS resolution timeouts, and asymmetric routing. While application logs record errors after a connection fails, tcpdump captures the raw wire-level packets, allowing engineers to verify whether TCP SYN packets ever reached the network interface, whether firewalls dropped responses, or whether packet fragmentation corrupted payload delivery.
Powered by the libpcap library, tcpdump compiles user-defined filter expressions into high-performance Berkeley Packet Filter (BPF) bytecode executed directly inside the Linux kernel. This architecture ensures minimal CPU overhead during capture, allowing engineers to filter millions of packets in high-throughput production environments safely.
What tcpdump Does and When to Use It
Under the hood, tcpdump creates a raw packet socket using the AF_PACKET address family and the SOCK_RAW socket type (e.g., socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))). This instructs the Linux network device driver to clone incoming and outgoing link-layer frames and pass them to the packet socket before they are processed by the kernel's protocol stack (TCP/IP) or filtered by Netfilter/iptables.
To avoid copying every frame into user space, tcpdump uses the Berkeley Packet Filter (BPF) engine. When you supply a filter expression (such as 'tcp port 80'), libpcap compiles the expression into a compact pseudo-machine bytecode program and attaches it to the raw socket via the SO_ATTACH_FILTER socket option. The kernel evaluates this bytecode in kernel space against incoming packet headers; only matching packets are placed into the packet ring buffer (PACKET_MMAP), minimizing context switching and zero-copy packet loss.
At the operating system level, tcpdump interacts with the Linux kernel networking stack through netlink sockets (AF_NETLINK), raw packet sockets, or standard POSIX resolver APIs. When running tcpdump in cloud instances or Kubernetes worker nodes, combining flags such as -i INTERFACE, -n, -nn, -c COUNT lets you bypass slow reverse DNS lookups and inspect numeric IPv4/IPv6 addresses and socket states directly.
Availability and Installation Across Linux Distributions
The tcpdump utility is packaged in tcpdump across mainstream Linux distributions. It is pre-installed on standard Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Fedora, and Arch Linux systems, while minimal container images may require manual installation:
Before running administrative tasks or automation scripts, verify whether tcpdump is installed and inspect which binary path and version your shell resolves:
type -a tcpdump
tcpdump --version 2>/dev/null || man -f tcpdump
# Illustrative output (reviewed against documentation)
tcpdump is /usr/bin/tcpdump
tcpdump (tcpdump)
# Debian / Ubuntu
sudo apt-get update && sudo apt-get install -y tcpdump
# RHEL / Rocky / AlmaLinux / Fedora
sudo dnf install -y tcpdump
# Arch Linux
sudo pacman -S --noconfirm tcpdump
# Alpine Linux
sudo apk add --no-cache tcpdump
Command Syntax, Arguments, and Exit Status Codes
tcpdump [OPTIONS] [EXPRESSION]
The invocation syntax is structured as: tcpdump [OPTIONS] [EXPRESSION]. Command options control which interface to listen on (-i), display formatting (-n for numeric IPs, -nn for numeric ports, -v for verbosity, -X for ASCII/hex payload dump), and file I/O (-w to write raw packets to a .pcap file, -r to read from a saved file).
The trailing EXPRESSION uses BPF grammar composed of primitives, qualifiers, and logical operators. Type qualifiers (host, net, port), direction qualifiers (src, dst), and protocol qualifiers (ip, tcp, udp, icmp) can be joined using and (&&), or (||), and not (!) to construct targeted capture filters.
0— Successful execution; packet capture terminated normally (via packet count limit or SIGINT).1— Operational error; network interface not found, permission denied, or file access failure.2— BPF filter syntax error; invalid expression grammar or unrecognized protocol keyword.
Important Options and Flags Reference
| Option / Flag | Long Form / Argument | Technical Behavior and Purpose |
|---|---|---|
-i INTERFACE | --interface | Specifies the network interface to capture on (e.g., -i eth0 or -i any to capture across all interfaces). |
-n | -n | Prevents converting IP addresses to hostnames, accelerating processing and avoiding capture loops. |
-nn | -nn | Suppresses both hostname and port number resolution, displaying raw numeric IPs and port numbers. |
-c COUNT | --count | Terminates tcpdump automatically after capturing exactly COUNT matching packets. |
-w FILE | --write | Writes raw, unfiltered captured packets directly to a standard .pcap binary file for Wireshark analysis. |
-r FILE | --read | Reads and parses network packets from a previously captured .pcap file instead of a live network interface. |
-v / -vv / -vvv | --verbose | Increases protocol decoding verbosity, displaying TTL, IP ID, options, flags, and checksum verification. |
-X / -XX | -X | Dumps the packet payload data in both hexadecimal and ASCII characters side-by-side. |
-s LEN | --snapshot-length | Sets the packet capture snapshot length in bytes (use -s 0 to capture entire un-truncated packets). |
For live console troubleshooting, the optimal flag combination is sudo tcpdump -i any -nn -v. Using -i any captures traffic across all physical and virtual interfaces simultaneously, while -nn prevents slow reverse DNS queries from interfering with live terminal output.
When preparing captures for forensic auditing or Wireshark inspection, always use -s 0 -w capture.pcap. Setting snapshot length to zero (-s 0) ensures full packet payloads are captured without truncation, preserving complete TLS certificates, HTTP headers, and diagnostic payloads.
When combining short flags on the command line, POSIX and GNU utilities allow clustering boolean flags behind a single hyphen, provided any option taking an argument appears last. In automated shell scripts, using long-form GNU options where portable makes self-documenting code that is easier to audit during peer review.
Practical Basic and Advanced Usage Examples
1. Capture Packets on a Specific Interface with Numeric Output
Capture live packets on interface eth0, displaying raw numeric IP addresses and port numbers, limited to 5 packets.
sudo tcpdump -i eth0 -nn -c 5
# Illustrative output (reviewed against documentation)
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
20:30:01.102450 IP 192.0.2.10.22 > 198.51.100.45.52341: Flags [P.], seq 1:45, ack 1, win 501, length 44
20:30:01.102512 IP 198.51.100.45.52341 > 192.0.2.10.22: Flags [.], ack 45, win 502, length 0
20:30:01.104210 IP 192.0.2.10.443 > 203.0.113.88.41200: Flags [P.], seq 100:150, ack 20, win 501, length 50
20:30:01.104320 IP 203.0.113.88.41200 > 192.0.2.10.443: Flags [.], ack 150, win 501, length 0
20:30:01.105100 IP 192.0.2.10.22 > 198.51.100.45.52341: Flags [P.], seq 45:89, ack 1, win 501, length 44
5 packets captured
5 packets received by filter
0 packets dropped by kernel
Each output line displays a microsecond timestamp, source IP.port, destination IP.port, TCP flags (P for Push, . for ACK), sequence numbers, and payload length.
2. Filter Traffic by Specific Host IP and Port
Target all incoming and outgoing traffic associated with an application backend on port 8080.
sudo tcpdump -i any -nn host 192.0.2.10 and port 8080
# Illustrative output (reviewed against documentation)
listening on any, link-type LINUX_SLL2 (Linux cooked v2), snapshot length 262144 bytes
20:31:10.452100 IP 198.51.100.12.48210 > 192.0.2.10.8080: Flags [S], seq 18420102, win 64240, length 0
20:31:10.452180 IP 192.0.2.10.8080 > 198.51.100.12.48210: Flags [S.], seq 9482104, ack 18420103, win 65160, length 0
The BPF filter restricts packet capture to frames involving IP 192.0.2.10 on port 8080, clearly showing the initial SYN and SYN-ACK handshake.
3. Capture and Inspect HTTP GET/POST Requests in ASCII
Display unencrypted HTTP request text payloads directly on the command line using the -A option.
sudo tcpdump -i eth0 -nn -A -s 0 -c 2 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12:2]&0xf0)>>2)) != 0)'
# Illustrative output (reviewed against documentation)
20:32:00.124500 IP 198.51.100.12.51240 > 192.0.2.10.80: Flags [P.], seq 1:85, ack 1, win 502, length 84
GET /api/v1/health HTTP/1.1..
Host: 192.0.2.10..
User-Agent: curl/7.88.1..
Accept: */*....
The -A option decodes packet payloads as printable ASCII characters, displaying the HTTP GET method, URI path, and request headers.
4. Capture DNS Queries and Responses
Monitor Domain Name System resolution requests and responses across all network interfaces on port 53.
sudo tcpdump -i any -nn -c 4 port 53
# Illustrative output (reviewed against documentation)
20:33:05.100200 IP 192.0.2.10.54120 > 8.8.8.8.53: 14210+ A? example.com. (29)
20:33:05.115400 IP 8.8.8.8.53 > 192.0.2.10.54120: 14210 1/0/0 A 93.184.216.34 (45)
20:33:05.115800 IP 192.0.2.10.54121 > 8.8.8.8.53: 14211+ AAAA? example.com. (29)
20:33:05.131200 IP 8.8.8.8.53 > 192.0.2.10.54121: 14211 1/0/0 AAAA 2606:2800:220:1:248:1893:25c8:1946 (57)
The capture details the DNS transaction IDs (14210, 14211), query record types (A and AAAA), and the answering IP addresses.
5. Save Packet Capture to Standard PCAP File
Write 1000 full packets to a .pcap file on disk for offline analysis with Wireshark or Zeek.
sudo tcpdump -i eth0 -nn -s 0 -w /tmp/production_traffic.pcap -c 1000
# Illustrative output (reviewed against documentation)
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
1000 packets captured
1000 packets received by filter
0 packets dropped by kernel
The -w option redirects raw binary packets to /tmp/production_traffic.pcap, preserving exact packet headers and payloads.
6. Read and Filter an Existing PCAP File
Analyze an archived packet capture file to isolate TCP connection resets (RST flag).
tcpdump -nn -r /tmp/production_traffic.pcap 'tcp[tcpflags] & tcp-rst != 0'
# Illustrative output (reviewed against documentation)
reading from file /tmp/production_traffic.pcap, link-type EN10MB (Ethernet), snapshot length 262144 bytes
20:30:12.842100 IP 192.0.2.10.80 > 198.51.100.12.51240: Flags [R.], seq 0, ack 18420103, win 0, length 0
The -r option reads the PCAP archive without needing root permissions, applying BPF bitmask filtering to surface RST packets.
Real-World Workflows, Pipelines, and Shell Automation
Investigating Intermittent TCP Connection Resets (RST)
Diagnose production connection drops where clients receive unexpected 'Connection reset by peer' errors by capturing TCP RST flags along with preceding packet sequence numbers.
#!/usr/bin/env bash
set -euo pipefail
INTERFACE="eth0"
TARGET_PORT=443
LOG_FILE="/tmp/tcp_resets.pcap"
echo "=== Capturing TCP Resets on ${INTERFACE}:${TARGET_PORT} ==="
echo "Press Ctrl+C to terminate capture when reset is observed..."
# Capture TCP RST packets or SYN packets to analyze connection lifecycles
sudo tcpdump -i "${INTERFACE}" -nn -s 0 -w "${LOG_FILE}" \
"tcp port ${TARGET_PORT} and (tcp[tcpflags] & (tcp-rst|tcp-syn) != 0)"
# Illustrative output (reviewed against documentation)
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
^C
42 packets captured
42 packets received by filter
0 packets dropped by kernel
This production workflow isolates connection handshakes and resets, allowing engineers to verify whether resets originate from local application daemons, firewalls, or remote clients.
Circular Buffer Rolling Capture for Intermittent Incidents
Continuously capture network traffic into rotating PCAP files to troubleshoot intermittent network anomalies without filling up server disk storage.
#!/usr/bin/env bash
set -euo pipefail
CAPTURE_DIR="/var/log/packet_captures"
mkdir -p "${CAPTURE_DIR}"
# -C 100 rotates files when they reach 100 MB
# -W 5 maintains a circular ring buffer of 5 files (maximum 500 MB total)
echo "=== Starting Circular Buffer Packet Capture ==="
sudo tcpdump -i eth0 -nn -s 0 -C 100 -W 5 -w "${CAPTURE_DIR}/rolling_trace.pcap" -z gzip &
PID=$!
echo "tcpdump running in background with PID ${PID}."
echo "Inspect captures in ${CAPTURE_DIR}."
# Illustrative output (reviewed against documentation)
=== Starting Circular Buffer Packet Capture ===
tcpdump running in background with PID 18420.
Inspect captures in /var/log/packet_captures.
The -C (file size threshold) and -W (maximum file count) flags create a continuous rolling circular buffer that automatically compresses older traces with gzip (-z).
Pipelines, Redirection, Quoting, and Shell Expansion
When embedding tcpdump inside shell pipelines (|), conditional chains (&& and ||), or command substitutions ($(...)), the calling shell performs tilde expansion, variable interpolation, and globbing before invoking the binary. Always wrap paths, patterns, and variables in quotes so whitespace and wildcard characters (*, ?) are not expanded prematurely, and use set -euo pipefail in unattended scripts.
Permissions, Security Implications, and Safe Execution
Capturing traffic on high-bandwidth network interfaces (such as 10 Gbps or 40 Gbps links) without strict BPF filters can saturate server disk I/O, fill storage volumes in minutes, and exhaust system memory through kernel packet drop backlogs.
Running tcpdump requires administrative root privileges or the CAP_NET_RAW and CAP_NET_ADMIN Linux capabilities to bind raw sockets and place network interfaces into promiscuous mode. Unprivileged users cannot capture network packets.
Packet captures can intercept unencrypted passwords, API tokens, session cookies, and sensitive personal data traversing the network. Always store .pcap files with restricted permissions (chmod 600) and delete packet archives once troubleshooting is complete.
GNU/Linux, BSD, and BusyBox Implementation Differences
The tcpdump command is universally available across Linux distributions, FreeBSD, OpenBSD, NetBSD, and macOS, maintaining consistent command-line options and BPF filter syntax due to shared libpcap dependencies.
The special interface name 'any' (-i any), which captures across all network devices simultaneously, is a Linux-specific feature enabled by the Linux cooked-mode capture encapsulation (SLL/SLL2). On BSD and macOS systems, tcpdump must be bound to a specific physical interface (e.g., -i en0).
Troubleshooting Common Errors and Diagnostics
tcpdump: eth0: You don't have permission to capture on that device
Root Cause: The command was invoked by an unprivileged user lacking raw socket and packet capture capabilities.
Resolution: Execute tcpdump with sudo or assign the CAP_NET_RAW capability to the tcpdump binary.
sudo tcpdump -i eth0
packets dropped by kernel in tcpdump summary
Root Cause: Packet capture throughput exceeded the kernel's socket buffer processing capacity, causing the kernel to drop frames.
Resolution: Increase the socket buffer size using the -B option (in KiB) or supply stricter BPF filter expressions.
sudo tcpdump -i eth0 -B 4096 -nn port 443
tcpdump: syntax error in filter expression
Root Cause: The BPF filter expression contains unescaped special characters, misplaced parentheses, or unrecognized keywords.
Resolution: Enclose complex BPF filter expressions in single quotes to prevent shell evaluation.
sudo tcpdump -i eth0 'tcp port 80 and (src host 192.0.2.10)'
Alternatives, Related Commands, and Related Tutorials
- How to Check Open Ports in Linux: nmap, netcat, and Bash — Practical syntax, flags, and examples for
nc. - ss Command in Linux: Display Socket Statistics and Listening Ports — Practical syntax, flags, and examples for
ss. - netstat Command in Linux: Network Connections, Routing, and Statistics — Practical syntax, flags, and examples for
netstat. - Netcat (nc) Command in Linux: Port Scanning, Transfers, and Debugging — Practical syntax, flags, and examples for
nc. - 25 Best Linux Commands Every Sysadmin Should Know — Core command-line utilities for daily Linux server administration.
- How to Find a File in Linux from the Command Line — Practical file search patterns across Linux directories.
- Linux Commands Knowledge Hub (#linux-commands) — Browse all Linux command-line tutorials and system administration references.
Authoritative References and Official Documentation

The Linux Command Line, 2nd Edition: A Complete Introduction
Frequently Asked Questions
Quick answers to common questions and configurations
Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
Rajesh Ray
I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Create a 3D Button in HTML and CSS: Easy Steps
How to Install Python and Pip on Ubuntu 24.04 LTS
How to Install Nginx on Ubuntu 24.04
How to Install Python on Ubuntu 24.04 LTS
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides


Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!