tcpdump examples showcase the power of command-line packet analysis for network professionals. This comprehensive guide demonstrates 55 practical commands for capturing and analysing network traffic, from basic packet capture to advanced filtering techniques used by hackers, cybersecurity professionals and network administrators alike.
Table of Contents
Understanding tcpdump Fundamentals
tcpdump serves as the premier command-line packet analyser for Unix-like systems. Network administrators and security professionals rely on these tcpdump examples to capture and inspect network traffic in real-time. Whether youโre troubleshooting connectivity issues, monitoring security threats, or analysing application behaviour, mastering tcpdump examples proves invaluable for technical roles.
The beauty of tcpdump lies in its combination of power and simplicity. Unlike graphical tools like nmap, tcpdump operates entirely from the command line, making it perfect for remote administration and automated monitoring scripts. This lightweight approach allows you to analyse network traffic on systems with minimal resources.
Basic Syntax and Structure
Every tcpdump command follows a consistent pattern that makes learning the tool straightforward:
tcpdump [options] [expression]
The options modify tcpdumpโs behaviour, such as specifying which network interface to monitor or determining the output format. Expressions define what type of traffic to capture, allowing you to specify hostnames, IP addresses, ports, protocols, and other filtering criteria.
Before diving into specific examples, understanding how to identify available network interfaces helps ensure youโre monitoring the correct traffic:
tcpdump -D
This command lists all available network interfaces on your system, displaying both their names and descriptions.
Essential Traffic Capture Techniques
Capturing Traffic on Specific Interfaces
To monitor all traffic on a particular interface, use the -i
flag followed by the interface name:
tcpdump -i eth0
For wireless interfaces, simply substitute the appropriate interface name:
tcpdump -i wlan0
Host-Based Traffic Filtering
Capturing traffic to or from specific hosts forms the foundation of targeted network analysis. Use the host
keyword to monitor all traffic associated with a particular IP address or hostname:
tcpdump host 192.168.1.100
This captures both incoming and outgoing traffic for the specified host. For domain names, tcpdump automatically resolves them to IP addresses:
tcpdump host example.com
Port-Specific Traffic Monitoring
Network services operate on specific ports, making port-based filtering essential for application troubleshooting. Monitor traffic on any port using:
tcpdump port 80
Common ports youโll frequently monitor include:
- Port 80 (HTTP)
- Port 443 (HTTPS)
- Port 22 (SSH)
- Port 21 (FTP)
- Port 25 (SMTP)
- Port 53 (DNS)
Advanced Filtering Capabilities
Combining Multiple Filters
tcpdump examples become more powerful when combining filters using logical operators. The and
, or
, and not
operators enable precise traffic isolation:
tcpdump host 192.168.1.100 and port 80
For more complex scenarios, use parentheses to group conditions:
tcpdump src host 192.168.1.100 and \( port 80 or port 443 \)
Protocol-Based Filtering
Filter traffic by protocol to focus on specific types of network communication:
tcpdump tcp tcpdump udp tcpdump icmp
Directional Traffic Analysis
Understanding traffic flow direction helps identify communication patterns. Use src
and dst
keywords to filter by source or destination:
tcpdump src host 192.168.1.100 tcpdump dst port 443
Network Range Filtering
Monitor entire network segments using CIDR notation:
tcpdump net 192.168.1.0/24
This captures all traffic within the specified network range, useful for monitoring subnet-wide activity.
File Operations and Output Management
Saving Captured Traffic
Preserve network captures for later analysis using the -w
flag:
tcpdump -w capture.pcap -i eth0
Saved capture files can be analysed later using tcpdump or imported into graphical tools like Wireshark for detailed inspection.
Reading Saved Captures
Analyse previously captured traffic using the -r
flag:
tcpdump -r capture.pcap
Controlling Output Verbosity
Adjust the level of detail in tcpdump output using verbosity flags:
-v
: Verbose output-vv
: More verbose output-vvv
: Maximum verbosity
tcpdump -vv -i eth0
55 Practical tcpdump Examples
Here are 55 essential tcpdump examples for various network analysis scenarios:
Basic Interface Monitoring
tcpdump -i eth0
โ Capture all traffic on eth0tcpdump -i wlan0
โ Capture all traffic on wlan0tcpdump -i any
โ Capture traffic on all interfaces
Host-Based Filtering
tcpdump host 192.168.1.100
โ Traffic to/from specific IPtcpdump host example.com
โ Traffic to/from domain nametcpdump not host 192.168.1.100
โ Exclude specific host
Port-Based Filtering
tcpdump port 80
โ HTTP traffictcpdump port 443
โ HTTPS traffictcpdump port 22
โ SSH traffictcpdump port 21
โ FTP traffictcpdump port 25
โ SMTP traffictcpdump port 53
โ DNS traffictcpdump portrange 80-443
โ Traffic in port range
Directional Filtering
tcpdump src host 192.168.1.100
โ Traffic from specific hosttcpdump dst host 192.168.1.100
โ Traffic to specific hosttcpdump src port 80
โ Traffic from port 80tcpdump dst port 443
โ Traffic to port 443tcpdump src portrange 1024-5000
โ Traffic from port range
Protocol Filtering
tcpdump tcp
โ All TCP traffictcpdump udp
โ All UDP traffictcpdump icmp
โ All ICMP traffictcpdump arp
โ All ARP traffic
Network Range Filtering
tcpdump net 192.168.1.0/24
โ Traffic within networktcpdump src net 192.168.1.0/24
โ Traffic from networktcpdump dst net 192.168.1.0/24
โ Traffic to networktcpdump not net 192.168.1.0/24
โ Traffic outside network
Combined Filters
tcpdump dst host 192.168.1.100 and dst port 80
โ Specific host and porttcpdump src host 192.168.1.100 and src port 443
โ Source host and porttcpdump host 192.168.1.100 and \( port 80 or port 443 \)
โ Host with multiple portstcpdump tcp and src net 192.168.1.0/24 and dst port 22
โ Complex combination
Exclusion Filters
tcpdump not icmp
โ All traffic except ICMPtcpdump not port 80
โ All traffic except port 80tcpdump not src host 192.168.1.100
โ Exclude source host
TCP Flag Filtering
tcpdump 'tcp[tcpflags] & tcp-syn != 0'
โ SYN packetstcpdump 'tcp[tcpflags] & tcp-ack != 0'
โ ACK packetstcpdump 'tcp[tcpflags] & tcp-rst != 0'
โ RST packetstcpdump 'tcp[tcpflags] & tcp-fin != 0'
โ FIN packetstcpdump 'tcp[tcpflags] & tcp-urg != 0'
โ URG packetstcpdump 'tcp[tcpflags] & tcp-push != 0'
โ PSH packetstcpdump 'tcp[tcpflags] = 0x01'
โ Only SYN flagtcpdump 'tcp[tcpflags] = 0x00'
โ No flags settcpdump 'tcp[tcpflags] = 0x12'
โ SYN-ACK packetstcpdump 'tcp[tcpflags] = 0x14'
โ RST-ACK packetstcpdump 'tcp[tcpflags] = 0x11'
โ FIN-ACK packetstcpdump 'tcp[tcpflags] = 0x18'
โ PSH-ACK packets
Advanced IP Filtering
tcpdump 'ip[6:2] & 0x1fff != 0'
โ Fragmented packetstcpdump 'ip[8] = 128'
โ Specific TTL valuetcpdump 'ip[1] & 0xfc >> 2 = 46'
โ DSCP valuetcpdump 'ip[1] & 0x03 = 3'
โ ECN bitstcpdump 'ip[9] = 6'
โ TCP protocol packets
TCP Sequence Analysis
tcpdump 'tcp[4:4] = 12345678'
โ Specific sequence numbertcpdump 'tcp[8:4] = 87654321'
โ Specific acknowledgment number
Port Range Filtering
tcpdump 'tcp[0:2] > 1023 and tcp[0:2] < 65536'
โ Source port rangetcpdump 'tcp[2:2] > 1023 and tcp[2:2] < 65536'
โ Destination port range
Additional Examples
tcpdump -n host 192.168.1.100
โ Disable name resolutiontcpdump -s 0 -i eth0
โ Capture full packet lengthtcpdump -c 100 -i eth0
โ Capture only 100 packetstcpdump -A -i eth0
โ Display packet contents in ASCIItcpdump -X -i eth0
โ Display packet contents in hex and ASCIItcpdump -e -i eth0
โ Show link-level header information
Optimising Network Analysis Workflow
tcpdump examples work best when incorporated into a systematic workflow. Consider these best practices:
Performance Considerations: When capturing high-volume traffic, write directly to disk using the -w
flag rather than displaying packets in real-time. This prevents packet loss during intensive capture sessions.
Security Analysis: Combine tcpdump with other tools for comprehensive security monitoring. Export captures to Wireshark for detailed protocol analysis or integrate with intrusion detection systems for automated threat detection.
Troubleshooting Methodology: Start with broad filters and progressively narrow your focus. Begin by capturing all traffic on an interface, then filter by host, port, or protocol as you identify patterns.
Documentation: Always document your capture filters and analysis findings. Include timestamps, filter expressions, and observed behaviours for future reference.
Advanced Techniques for Network Professionals
Berkeley Packet Filter Syntax
Master tcpdump commands at an advanced level by understanding Berkeley Packet Filter (BPF) syntax. This enables precise packet matching based on any field within the protocol headers:
tcpdump 'tcp[20:2] = 0x4745' -A
This example captures HTTP GET requests by matching the ASCII values at specific TCP payload offsets.
Performance Tuning
Optimise capture performance for high-speed networks:
- Use
-B
to increase buffer size - Apply filters at capture time to reduce processing overhead
- Consider using
-n
to disable DNS lookups during capture
Integration with Analysis Tools
Export tcpdump data for advanced analysis:
- Convert to various formats using editcap
- Stream to analysis tools using pipes
- Integrate with log management systems for correlation
Conclusion
Mastering tcpdump examples empowers network professionals to diagnose issues quickly, monitor security threats effectively, and understand network behaviour comprehensively. These 55 practical tcpdump examples provide a solid foundation for network packet analysis, from basic traffic capture to advanced filtering techniques.
Regular practice with tcpdump enhances your ability to troubleshoot complex network issues and identify security anomalies. As networks grow increasingly complex, proficiency with command-line packet analysis tools becomes ever more valuable for maintaining robust and secure network infrastructure.
Remember that tcpdump represents just one tool in the network analystโs toolkit. Combine it with other monitoring and analysis tools just nmap to build a comprehensive understanding of your network environment. Continue exploring advanced filtering techniques and protocol analysis to expand your network troubleshooting capabilities.