Tcpdump is a tool to dump the traffic on a network. It’s a packet sniffer that able to capture traffic that passes through a machine. It operates on a packet level, meaning that it captures the actual packets that fly in and out of your computer. If your linux server haven’t installed with tcpdump package, you can refer to the previous post on the quick step to install tcpdump. This tcpdump command with examples steps has been tested on Linux CentOS 5/CentOS 6/CentOS 7/RHEL 5/RHEL 6 / RHEL 7.
How to use Tcpdump Command with Examples on Linux
There are a few tcpdump command with examples that i will share with you. -w option will writes the packets into .pcap file. The extension should be always .pcap as it can be read by any network protocol analyzer.
1. To see any available network interface that can be monitor using option -D :
# tcpdump -D 1.eth0 2.usbmon1 (USB bus number 1) 3.usbmon2 (USB bus number 2) 4.any (Pseudo-device that captures on all interfaces) 5.lo
2. View the incoming packets on port 80 in real-time for apache web server, then save it to port80-apache1.pcap. By using this command, you can analyze where packets were coming from or being sent to :
# tcpdump -w port80-apache1.pcap -i eth0 tcp port 80
3. Execute tcpdump command without any additional option, it will capture all the packets flowing through all the interfaces. Just run -i option with tcpdump command as below :
# tcpdump -w filename.pcap -i eth0
4. Capture only N number of packets. This can be done using tcpdump -c command. This example will only capture 3 packet :
# tcpdump -c 3 -i eth0 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes 21:22:18.777243 IP centos62.ehowstuff.local.ssh > 192.168.1.52.pq-lic-mgmt: Flags [.], ack 4148066988, win 17688, options [nop,nop,TS val 790832 ecr 135264], length 0 21:22:18.783396 IP centos62.ehowstuff.local.ssh > 192.168.1.52.pq-lic-mgmt: Flags [P.], seq 0:196, ack 1, win 17688, options [nop,nop,TS val 790838 ecr 135264], length 196 21:22:18.785458 ARP, Request who-has 192.168.1.1 tell centos62.ehowstuff.local, length 28 3 packets captured 15 packets received by filter 0 packets dropped by kernel
5. Read the packets using tcpdump -r for the saved file as per example below :
Capture 3 packet and save it to test.pcap
# tcpdump -w test.pcap -c 3 -i eth0 tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes 3 packets captured 3 packets received by filter 0 packets dropped by kernel
Try to read test.pcap using tcpdump -r command :
# tcpdump -r test.pcap reading from file test.pcap, link-type EN10MB (Ethernet) 21:24:51.199237 IP centos62.ehowstuff.local.ssh > 192.168.1.52.pq-lic-mgmt: Flags [P.], seq 693745553:693745685, ack 4148082568, win 17688, options [nop,nop,TS val 943254 ecr 136793], length 132 21:24:51.201339 IP 192.168.1.52.pq-lic-mgmt > centos62.ehowstuff.local.ssh: Flags [P.], seq 1:53, ack 132, win 17232, options [nop,nop,TS val 136793 ecr 943254], length 52 21:24:51.241386 IP centos62.ehowstuff.local.ssh > 192.168.1.52.pq-lic-mgmt: Flags [.], ack 53, win 17688, options [nop,nop,TS val 943296 ecr 136793], length 0
6. tcpdump allows you to define port range as bellow for capturing packets based on a range of tcp port. Examples below will capture the packet from port 21 until 80.
# tcpdump tcp portrange 21-80
I hope this article gives you some ideas and essential guidance on how to use tcpdump Command with Examples on Linux CentOS 5/CentOS 6/CentOS 7/RHEL 5/RHEL 6 / RHEL 7