tcpdump
tcpdump is a command-line packet capture tool.
It listens on a network interface and prints packets that match a filter.
Use it when you need to debug:
- traffic reaching or leaving a machine
- UDP/TCP ports
- source and destination addresses
- DNS, ICMP, ARP, DHCP, and other network protocols
- routing, firewall, or NAT problems
Basic capture:
Meaning:
sudo: packet capture usually needs root permissions-i eth0: capture packets on interfaceeth0
Show available interfaces:
Capture on any interface:
Useful basic options
Common options:
-i <interface>: select network interface-n: do not resolve host names-nn: do not resolve host names or port names-v,-vv,-vvv: show more packet details-c <count>: stop after a number of packets-s 0: capture the full packet, not only the default snapshot size-X: print packet data in hex and ASCII-A: print packet data as ASCII
Example:
This captures 10 packets and then stops.
Save packets to a pcap file
Use -w to write packets to a .pcap file.
Important:
-w capture.pcapsaves packets to a file-s 0captures the full packet- the terminal will not print decoded packets while writing with
-w
Stop the capture with Ctrl+C.
You can later open the file with:
tcpdump- Wireshark
- tshark
Read a pcap file
Use -r to read packets from a saved .pcap file.
Read with more details:
Read and print packet payload:
You can also apply filters while reading:
Packet filters
tcpdump uses BPF filters.
A filter selects which packets to capture or display.
Common filter parts:
- protocol:
tcp,udp,icmp,arp - host:
host 192.168.1.10 - source host:
src host 192.168.1.10 - destination host:
dst host 192.168.1.20 - port:
port 5600 - source port:
src port 5600 - destination port:
dst port 5600 - network:
net 192.168.1.0/24
Examples:
Filter with multiple conditions
Use and, or, and not to combine conditions.
Example: capture UDP traffic on port 5600 from source address
192.168.1.50:
More specific version: source address 192.168.1.50, UDP destination port
5600:
Save the same filtered traffic to a pcap file:
Read only matching packets from the pcap:
Example with or:
Example with not:
This avoids showing SSH traffic while you debug from a remote shell.
Common tcpdump usages to learn
Important topics to know:
- capture by interface with
-i - disable name resolution with
-nn - limit packet count with
-c - save and read pcap files with
-wand-r - capture full packets with
-s 0 - filter by protocol, host, network, and port
- combine filters with
and,or,not - inspect payloads with
-Aand-X - debug DNS with
udp port 53 - debug ping with
icmp - debug ARP with
arp - debug TCP handshakes with
tcp - avoid capturing SSH noise with
not port 22 - open pcap files in Wireshark for visual analysis
iftop bandwidth by port
tcpdump captures packets. For live bandwidth or traffic rate, iftop is often
easier.
iftop shows live traffic between hosts and can use packet filters like
tcpdump.
Install:
Measure traffic for a specific port:
Only UDP port 5600:
Only UDP traffic from source IP 192.168.1.50 to destination port 5600:
Useful options:
-i eth0: listen on interfaceeth0-n: do not resolve hostnames-P: show ports-f '<filter>': apply a packet filter
In the iftop screen:
=>and<=show traffic direction- the rate columns show recent traffic averages
- the columns are commonly
2s,10s, and40saverages
Use iftop when you want a live bandwidth view. Use tcpdump when you need to
save packets, inspect packet contents, or debug protocol details.