Skip to content

What is tcpdump Linux command?

Tcpdump is a powerful command-line packet analyzer tool used to capture and analyze network traffic in Linux. It allows users to inspect individual packets and diagnose network issues effectively. With tcpdump, users can monitor network activity, detect anomalies, and troubleshoot network problems efficiently.

tcpdump Syntax:

Terminal window
tcpdump [options] [expression]

tcpdump Options:

OptionDescription
-i interfaceListen on the specified network interface
-c countExit after receiving count packets
-r fileRead packets from specified file
-w fileWrite packets to a file
-nDo not resolve hostnames
-nnDo not resolve hostnames or port names
-qQuick (quiet) output
-vVerbose output
-XXShow packet header in hex and ASCII
-s snaplenCapture snaplen bytes of data
-SPrint absolute sequence numbers
-ePrint the link-level header
-APrint packets in ASCII
-ttttPrint timestamps with microsecond resolution
-ttPrint timestamps with second resolution

Parameters:

ParameterDescription
optionsSpecifies various command options to use
expressionDefines the filter expressions for capturing

tcpdump Command Usage Examples:

Capture Incoming Traffic on Specific Interface

Terminal window
tcpdump -i eth0

Captures incoming traffic on the specified network interface “eth0”.

Write Captured Traffic to a File

Terminal window
tcpdump -i eth0 -w captured_traffic.pcap

Captures network traffic on “eth0” and writes it to a file named “captured_traffic.pcap”.

Display Captured Traffic in ASCII

Terminal window
tcpdump -A -i eth0

Captures and displays network traffic on interface “eth0” in ASCII format.

Filter Captured Traffic by IP Address

Terminal window
tcpdump -i eth0 host 192.168.1.10

Captures and displays traffic on interface “eth0” involving the IP address 192.168.1.10.

Monitor Specific Port Traffic

Terminal window
tcpdump -i eth0 port 80

Captures and displays traffic on interface “eth0” involving port 80.

How do I use tcpdump in Linux?

To use the tcpdump command in bash, execute the following command:

Terminal window
tcpdump

How can I capture packets for a specific network interface with tcpdump?

To capture packets for a specific network interface using tcpdump, use the -i flag followed by the interface name. For example:

Terminal window
tcpdump -i eth0

How can I filter tcpdump results by IP address?

To filter tcpdump results by IP address, use the host filter option followed by the IP address. For example, to capture packets for a specific IP address:

Terminal window
tcpdump host 192.168.1.1

How do I save tcpdump output to a file for later analysis?

To save tcpdump output to a file, use the -w flag followed by the file path. For example, to save the captured packets to a file named “capture.pcap”:

Terminal window
tcpdump -w capture.pcap

How can I view the contents of a saved tcpdump file?

To view the contents of a saved tcpdump file, use the -r flag followed by the file path. For example, to read the contents of a file named “capture.pcap”:

Terminal window
tcpdump -r capture.pcap

How can I capture only a specific number of packets with tcpdump?

To capture only a specific number of packets using tcpdump, use the -c flag followed by the number of packets to capture. For example, to capture 50 packets:

Terminal window
tcpdump -c 50

How do I capture packets for a specific port with tcpdump?

To capture packets for a specific port using tcpdump, use the port filter option followed by the port number. For example, to capture packets for port 80 (HTTP):

Terminal window
tcpdump port 80

Applications of the tcpdump command

  • Troubleshooting network connectivity issues
  • Monitoring and analyzing network traffic
  • Capturing packets for security and performance analysis
  • Network debugging and testing
  • Intrusion detection and prevention
  • Packet sniffing and protocol analysis
  • Investigating network-related issues