Tcpdump
Tcpdump is a command-line packet capture and analysis tool for Unix-like systems.
It is useful for remote troubleshooting, incident response, network forensics, and quick packet inspection when a graphical tool like Wireshark is not available.
Warning
Capture traffic only on systems and networks where you have permission.
Overview
Tcpdump captures packets from a network interface and prints packet summaries to the terminal or saves packets to a .pcap file.
Common uses:
- Capture traffic on a server over SSH
- Troubleshoot connectivity
- Save packets for Wireshark analysis
- Inspect cleartext protocols
- Identify suspicious connections
- Filter traffic by host, port, protocol, or size
- Investigate scans, beaconing, and unusual traffic
Tcpdump uses the libpcap library and often requires root privileges.
Installation
Check if tcpdump is installed:
Install on Debian/Ubuntu:
Check version:
Finding Interfaces
List available capture interfaces:
Alternative Linux commands:
Common interfaces:
The any interface captures from all interfaces, but it may not support all capture features.
Basic Capture
Capture on a specific interface:
Capture on all interfaces:
Stop capture:
Essential Options
| Option | Purpose |
|---|---|
-i INTERFACE |
Capture on an interface |
-D |
List interfaces |
-c COUNT |
Capture a specific number of packets |
-n |
Disable DNS name resolution |
-nn |
Disable DNS and port name resolution |
-v |
Verbose output |
-vv / -vvv |
More verbose output |
-e |
Show Ethernet headers / MAC addresses |
-A |
Show packet payload in ASCII |
-X |
Show packet payload in hex and ASCII |
-w FILE.pcap |
Write packets to file |
-r FILE.pcap |
Read packets from file |
Recommended Starting Commands
Quick capture with clean output:
Capture 10 packets:
Verbose triage:
Deep inspection with MAC addresses and payload:
Why Use -n and -nn
Use -n to prevent DNS lookups.
Use -nn to prevent both DNS and port name resolution.
This is usually better because it:
- Speeds up output
- Reduces extra DNS traffic
- Shows real IP addresses
- Shows real port numbers
- Avoids misleading name resolution
Writing to PCAP
Save traffic to a file:
Read a saved capture:
Read with no name resolution:
Read and show payload:
Warning
Packet captures can grow quickly. Monitor disk space during long captures.
Tcpdump Output Anatomy
Example line:
11:13:59.149599 IP 172.16.146.2.42454 > 54.77.251.34.443: Flags [P.], seq 1:38, ack 37, win 501, length 37
| Part | Meaning |
|---|---|
11:13:59.149599 |
Timestamp |
IP |
Protocol |
172.16.146.2.42454 |
Source IP and port |
> |
Direction |
54.77.251.34.443 |
Destination IP and port |
Flags [P.] |
TCP flags |
seq / ack |
TCP sequence and acknowledgment |
win |
TCP window size |
length |
Payload length |
TCP Flags
Common TCP flags:
| Flag | Meaning |
|---|---|
[S] |
SYN |
[S.] |
SYN-ACK |
[.] |
ACK |
[P.] |
PSH-ACK |
[F.] |
FIN-ACK |
[R] |
RST |
[R.] |
RST-ACK |
Example:
Basic Filters
Tcpdump filters help reduce noise.
Filter by Host
Traffic involving a host:
Source only:
Destination only:
Filter by Network
Source network:
Destination network:
Filter by Port
Traffic involving a port:
Source port:
Destination port:
Port range:
Filter by Protocol
sudo tcpdump -i eth0 tcp
sudo tcpdump -i eth0 udp
sudo tcpdump -i eth0 icmp
sudo tcpdump -i eth0 arp
Protocol numbers:
| Protocol | Number |
|---|---|
| ICMP | 1 |
| TCP | 6 |
| UDP | 17 |
Example:
Logical Operators
Tcpdump supports logical operators.
| Operator | Meaning |
|---|---|
and / && |
Both conditions |
or / || |
Either condition |
not / ! |
Exclude condition |
Examples:
Useful Filter Examples
SSH traffic:
NTP traffic:
HTTPS traffic for a host:
Non-TCP traffic:
Traffic involving a host over TCP:
Packet Size Filters
Packet size can help identify anomalies.
Packets smaller than a size:
Packets larger than a size:
Possible uses:
- Small control packets
- Large file transfers
- Possible data exfiltration
- Unusual payload sizes
Output Formatting
| Option | Description | Use Case |
|---|---|---|
-q |
Quick output | High-level overview |
-e |
Link-level header | MAC address analysis |
-A |
ASCII payload | Cleartext protocols |
-xx |
Hex output | Raw packet analysis |
-X |
Hex and ASCII | Payload inspection |
Examples:
Cleartext Inspection
Show ASCII payloads:
Search a PCAP for text using line buffering:
Use -l when piping to tools like grep so output is line-buffered.
Header Byte Filtering
Tcpdump can inspect protocol header bytes.
Syntax:
| Part | Meaning |
|---|---|
proto |
Protocol such as tcp, udp, ip, icmp, ether |
expr |
Byte offset |
size |
Number of bytes, usually 1, 2, or 4 |
Example: multicast Ethernet packets:
Example: IP packets with options:
TCP Flag Filtering
Only SYN packets:
Packets with at least SYN set:
Packets with SYN or ACK set:
Byte-offset method for SYN packets:
Available TCP flag names:
Practical Incident Workflow
- Identify the interface.
- Capture a small sample.
- Disable name resolution.
- Filter by host, port, or protocol.
- Save to PCAP if deeper analysis is needed.
- Open the PCAP in Wireshark.
- Document commands and findings.
Example:
sudo tcpdump -D
sudo tcpdump -i eth0 -nn -c 20
sudo tcpdump -i eth0 -nn host 192.168.1.25 -w suspicious-host.pcap
Common Troubleshooting
| Problem | Possible Cause |
|---|---|
| Permission denied | Need sudo |
| Too much output | Add filters or -c |
| Slow output | Use -n or -nn |
| Empty capture | Wrong interface or no matching traffic |
| Huge PCAP | Filter traffic or limit capture time |
| Cannot see other hosts | Switched network, no span/tap, or wrong segment |
Quick Reference
| Goal | Command |
|---|---|
| List interfaces | sudo tcpdump -D |
| Capture on interface | sudo tcpdump -i eth0 |
| No DNS/port names | sudo tcpdump -i eth0 -nn |
| Capture count | sudo tcpdump -i eth0 -c 10 |
| Write PCAP | sudo tcpdump -i eth0 -w capture.pcap |
| Read PCAP | tcpdump -r capture.pcap |
| Filter host | tcpdump host 192.168.1.5 |
| Filter port | tcpdump port 443 |
| Filter protocol | tcpdump tcp |
| Show payload | tcpdump -X |
| Show MAC addresses | tcpdump -e |
Notes to Remember
- Tcpdump is ideal for CLI packet capture.
- Use
-nnfor faster, cleaner output. - Use
-wto save PCAPs for later analysis. - Use filters to reduce noise.
- Use
-Aor-Xfor payload inspection. - Use
-ewhen MAC addresses matter. - Tcpdump filters can be applied during capture or when reading a PCAP.