Before You Start
- Your own server only: capture traffic only on servers you run, or that you have written permission to inspect. Capturing other people's traffic without permission can break the law and your provider's terms.
- Captures hold private data: unencrypted traffic, such as plain HTTP, FTP, or mail without TLS, shows passwords, cookies, and personal data in clear text. Treat every capture as sensitive.
- Root rights: tcpdump needs root, through sudo, to open an interface.
Install and Run a First Capture
sudo apt install tcpdump # Ubuntu, Debian
sudo dnf install tcpdump # AlmaLinux, Rocky Linux
List the interfaces, then capture ten packets of HTTPS traffic on one of them:
sudo tcpdump -D
sudo tcpdump -i eth0 -nn -c 10 port 443
Interface names differ; ip a shows yours, and -i any listens on all of them. Without -c, stop the capture with Ctrl+C; tcpdump then prints how many packets it captured and dropped.
Options You Will Use Most
| Option | What it does |
|---|---|
-i eth0 | Interface to listen on |
-n | No names for addresses or ports |
-nn | Older spelling, same effect |
-c 100 | Stop after 100 packets |
-w file | Save raw packets to a file |
-r file | Read a saved file |
-A | Print the payload as text |
-v | More detail per packet |
Current tcpdump releases treat -n as covering ports too; older ones needed -nn for that, which still works. Use one of them on a server: a reverse lookup for every address slows the output and creates DNS traffic of its own. -A is useful for plain HTTP headers; on HTTPS it shows only encrypted bytes.
Filter by Host, Port, and Protocol
A filter at the end of the command keeps only matching packets. Combine terms with and, or, and not:
sudo tcpdump -nn -i eth0 host 203.0.113.50
sudo tcpdump -nn -i eth0 src 203.0.113.50 and port 80
sudo tcpdump -nn -i eth0 udp port 53
sudo tcpdump -nn -i eth0 icmp
sudo tcpdump -nn -i eth0 'tcp port 443 and not host 203.0.113.50'
Put a filter in single quotes when it contains parentheses or several words, so the shell passes it on unchanged. net 203.0.113.0/24 matches a whole subnet.
Leave Your SSH Session Out
If you capture everything while logged in over SSH, tcpdump records its own output on its way to you, which creates more packets to record. Exclude SSH:
sudo tcpdump -nn -i eth0 not port 22
If SSH runs on another port, use that number; see port 22.
Save a Capture and Keep It Safe
When tcpdump runs as root, it switches to its own unprivileged tcpdump user before it opens the output file (the -Z option; Debian and Ubuntu enable this by default). A directory only root can write to, such as one under /root, then fails with "Permission denied". Create a directory that only root and the tcpdump user can use:
sudo install -d -m 700 -o tcpdump -g tcpdump /var/tmp/captures
sudo tcpdump -nn -i eth0 -c 1000 -w /var/tmp/captures/web.pcap port 80
sudo tcpdump -nn -r /var/tmp/captures/web.pcap
Keep capture files in a directory like this, never in a web root or a shared folder. The pcap format opens in Wireshark on your own computer; copy it there over an encrypted connection such as scp. Check what a file contains before you send it to anyone, and delete it once the problem is solved. On Ubuntu, an AppArmor profile also limits where tcpdump may write; file names ending in .pcap, as above, are allowed.
Read a TCP Handshake
IP 198.51.100.20.51514 > 203.0.113.10.443: Flags [S], seq 1000, ...
IP 203.0.113.10.443 > 198.51.100.20.51514: Flags [S.], seq 5000, ack 1001, ...
IP 198.51.100.20.51514 > 203.0.113.10.443: Flags [.], ack 1, ...
Each line starts with a time stamp (left out here), then source address and port, destination, and TCP flags. After the first packets, tcpdump prints sequence and acknowledgment numbers relative to the start of the connection, which is why the last line reads ack 1:
- [S]: SYN, the client asks to connect.
- [S.]: SYN-ACK, the server accepts. The dot stands for ACK.
- [.]: ACK, the connection is open.
- [P.]: data pushed to the application.
- [F.]: one side closes the connection.
- [R]: reset; often nothing listens on the port.
tcpdump sees incoming packets before the server's firewall does. SYNs that arrive with no reply usually mean the local firewall drops them; SYNs that never arrive are blocked before the server. See how to check open ports in Linux and TCP vs UDP.