• Get application security done the right way! Detect, Protect, Monitor, Accelerate, and more…
  • tcpdump is an amazing command-line tool for network sniffing. It is an industry-standard for capturing and analyzing TCP/IP packets.

    The tcpdump tool can be of great help when resolving networking issues. The packets can be saved to a file and later analyzed. It is a good idea to run this tool occasionally to keep a watch over your network.

    What does the tcpdump output look like?

    tcpdump allows you to examine the headers of the TCP/IP packets. It prints one line for each packet, and command keeps running until you press Ctrl+C to terminate.

    Let’s examine one line from an example output:

    20:58:26.765637 IP 10.0.0.50.80 > 10.0.0.1.53181: Flags [F.], seq 1, ack 2, win 453, options [nop,nop,TS val 3822939 ecr 249100129], length 0

    Each line includes

    • Unix timestamp (20:58:26.765637)
    • protocol (IP)
    • the source hostname or IP, and port number (10.0.0.50.80)
    • destination hostname or IP, and port number (10.0.0.1.53181)
    • TCP Flags (Flags [F.]). Flags indicate the state of the connection. This can include more than one value, like in this example [F.] for FIN-ACK. This field can have the following values :
      • S – SYN. The first step in establishing the connection.
      • F – FIN. Connection termination.
      • . – ACK. Acknowledgment packet received successfully.
      • P – PUSH. Tells the receiver to process packets instead of buffering them.
      • R – RST. Communication stopped.
    • Sequence number of the data in the packet. (seq 1)
    •  Acknowledgement number (ack 2)
    • Window size (win 453). The number of bytes available in the receiving buffer. This is followed by TCP options.
    • Length of the data payload. (length 0)

    Installation

    On Debian based distributions tcpdump can be installed with the APT command :

    # apt install tcpdump -y

    On RPM-based distributions tcpdump can be installed with YUM :

    # yum install tcpdump -y

    Or using DNF if RHEL 8

    # dnf install tcpdump -y

    tcpdump command options

    You need to be root to run tcpdump. It includes many options and filters. Running tcpdump without any options will capture all packets flowing through the default interface.

    To see the list of network interfaces available on the system and on which tcpdump can capture packets.

    # tcpdump -D

    Or

    # Tcpdump --list-interfaces
    1.eth0
    2.nflog (Linux netfilter log (NFLOG) interface)
    3.nfqueue (Linux netfilter queue (NFQUEUE) interface)
    4.eth1
    5.any (Pseudo-device that captures on all interfaces)
    6.lo [Loopback]

    This is especially useful on systems that do not have a command to list interfaces.

    To capture packets flowing through a specific interface, use the -i flag with the interface name. Without the -i interface tcpdump will pick up the first network interface it comes across. 

    # tcpdump -i eth1
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on eth1, link-type EN10MB (Ethernet), capture size 262144 bytes
    01:06:09.278817 IP vagrant-ubuntu-trusty-64 > 10.0.0.51: ICMP echo request, id 4761, seq 1, length 64
    01:06:09.279374 IP 10.0.0.51 > vagrant-ubuntu-trusty-64: ICMP echo reply, id 4761, seq 1, length 64
    01:06:10.281142 IP vagrant-ubuntu-trusty-64 > 10.0.0.51: ICMP echo request, id 4761, seq 2, length 64

    The -v flag increases the information you see about the packets, -vv gives you even more details.

    By default, tcpdump resolves IP addresses to hostnames and also uses service names instead of port numbers. If DNS is broken or you do not want tcpdump to perform name lookups, use the -n option. 

    # tcpdump -n
    listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
    04:19:07.675216 IP 10.0.2.15.22 > 10.0.2.2.50422: Flags [P.], seq 2186733178:2186733278, ack 204106815, win 37232, length 100
    04:19:07.675497 IP 10.0.2.2.50422 > 10.0.2.15.22: Flags [.], ack 100, win 65535, length 0
    04:19:07.675747 IP 10.0.2.15.22 > 10.0.2.2.50422: Flags [P.], seq 100:136, ack 1, win 37232, length 36
    04:19:07.675902 IP 10.0.2.2.50422 > 10.0.2.15.22: Flags [.], ack 136, win 65535, length 0
    04:19:07.676142 IP 10.0.2.15.22 > 10.0.2.2.50422: Flags [P.], seq 136:236, ack 1, win 37232, length 100

    To capture only a set of lines, say 5, use the -c flag:

    #tcpdump -c 5
    04:19:07.675216 IP 10.0.2.15.22 > 10.0.2.2.50422: Flags [P.], seq 2186733178:2186733278, ack 204106815, win 37232, length 100
    04:19:07.675497 IP 10.0.2.2.50422 > 10.0.2.15.22: Flags [.], ack 100, win 65535, length 0
    04:19:07.675747 IP 10.0.2.15.22 > 10.0.2.2.50422: Flags [P.], seq 100:136, ack 1, win 37232, length 36
    04:19:07.675902 IP 10.0.2.2.50422 > 10.0.2.15.22: Flags [.], ack 136, win 65535, length 0
    04:19:07.676142 IP 10.0.2.15.22 > 10.0.2.2.50422: Flags [P.], seq 136:236, ack 1, win 37232, length 100
    5 packets captured

    The default tcpdump output uses Unix timestamps. To capture packets with human-readable timestamp:

    # tcpdump -tttt
    2020-07-06 04:30:12.203638 IP 10.0.2.15.22 > 10.0.2.2.50422: Flags [P.], seq 2186734102:2186734138, ack 204107103, win 37232, length 36
    2020-07-06 04:30:12.203910 IP 10.0.2.2.50422 > 10.0.2.15.22: Flags [.], ack 36, win 65535, length 0
    2020-07-06 04:30:12.204292 IP 10.0.2.15.22 > 10.0.2.2.50422: Flags [P.], seq 36:72, ack 1, win 37232, length 36
    2020-07-06 04:30:12.204524 IP 10.0.2.2.50422 > 10.0.2.15.22: Flags [.], ack 72, win 65535, length 0
    2020-07-06 04:30:12.204658 IP 10.0.2.15.22 > 10.0.2.2.50422: Flags [P.], seq 72:108, ack 1, win 37232, length 36

    tcpdump filter expressions

    Filter expressions select which packet headers will be displayed. If no filters are applied, all packet headers are displayed. Commonly used filters are port, host, src, dst, tcp, udp, icmp.

    port filter

    Use port filter to view packets arriving at a specific port:

    # Tcpdump -i eth1 -c 5 port 80
    23:54:24.978612 IP 10.0.0.1.53971 > 10.0.0.50.80: Flags [SEW], seq 53967733, win 65535, options [mss 1460,nop,wscale 5,nop,nop,TS val 256360128 ecr 0,sackOK,eol], length 0
    23:54:24.978650 IP 10.0.0.50.80 > 10.0.0.1.53971: Flags [S.E], seq 996967790, ack 53967734, win 28960, options [mss 1460,sackOK,TS val 5625522 ecr 256360128,nop,wscale 6], length 0
    23:54:24.978699 IP 10.0.0.1.53972 > 10.0.0.50.80: Flags [SEW], seq 226341105, win 65535, options [mss 1460,nop,wscale 5,nop,nop,TS val 256360128 ecr 0,sackOK,eol], length 0
    23:54:24.978711 IP 10.0.0.50.80 > 10.0.0.1.53972: Flags [S.E], seq 1363851389, ack 226341106, win 28960, options [mss 1460,sackOK,TS val 5625522 ecr 256360128,nop,wscale 6], length 0

    host filter

    To capture all packets arriving at or leaving from the host with IP address of 10.0.2.15:

    # tcpdump host 10.0.2.15
    03:48:06.087509 IP 10.0.2.15.22 > 10.0.2.2.50225: Flags [P.], seq 3862934963:3862934999, ack 65355639, win 37232, length 36
    03:48:06.087806 IP 10.0.2.2.50225 > 10.0.2.15.22: Flags [.], ack 36, win 65535, length 0
    03:48:06.088087 IP 10.0.2.15.22 > 10.0.2.2.50225: Flags [P.], seq 36:72, ack 1, win 37232, length 36
    03:48:06.088274 IP 10.0.2.2.50225 > 10.0.2.15.22: Flags [.], ack 72, win 65535, length 0
    03:48:06.088440 IP 10.0.2.15.22 > 10.0.2.2.50225: Flags [P.], seq 72:108, ack 1, win 37232, length 36

    To capture packets of a specific protocol type, for example, icmp, on eth1 interface:

    # tcpdump -i eth1 icmp
    04:03:47.408545 IP vagrant-ubuntu-trusty-64 > 10.0.0.51: ICMP echo request, id 2812, seq 75, length 64
    04:03:47.408999 IP 10.0.0.51 > vagrant-ubuntu-trusty-64: ICMP echo reply, id 2812, seq 75, length 64
    04:03:48.408697 IP vagrant-ubuntu-trusty-64 > 10.0.0.51: ICMP echo request, id 2812, seq 76, length 64
    04:03:48.409208 IP 10.0.0.51 > vagrant-ubuntu-trusty-64: ICMP echo reply, id 2812, seq 76, length 64
    04:03:49.411287 IP vagrant-ubuntu-trusty-64 > 10.0.0.51: ICMP echo request, id 2812, seq 77, length 64

    Combining filter expressions

    You can combine these filter expressions with AND, OR, and NOT operators. This will enable you to write commands which can isolate packets more precisely:

    Packets from a specific IP and destined for a specific port:

    # tcpdump -n -i eth1 src 10.0.0.1 and dst port 80
    00:18:17.155066 IP 10.0.0.1.54222 > 10.0.0.50.80: Flags [F.], seq 500773341, ack 2116767648, win 4117, options [nop,nop,TS val 257786173 ecr 5979014], length 0
    00:18:17.155104 IP 10.0.0.1.54225 > 10.0.0.50.80: Flags [S], seq 904045691, win 65535, options [mss 1460,nop,wscale 5,nop,nop,TS val 257786173 ecr 0,sackOK,eol], length 0
    00:18:17.157337 IP 10.0.0.1.54221 > 10.0.0.50.80: Flags [P.], seq 4282813257:4282813756, ack 1348066220, win 4111, options [nop,nop,TS val 257786174 ecr 5979015], length 499: HTTP: GET / HTTP/1.1
    00:18:17.157366 IP 10.0.0.1.54225 > 10.0.0.50.80: Flags [.], ack 1306947508, win 4117, options [nop,nop,TS val 257786174 ecr 5983566], length 0

    To capture all packets except ICMP, use the NOT operator:

    # tcpdump -i eth1 not icmp

    Saving packet headers to a file

    Since the output of tcpdump can scroll past the screen quite fast, you can store packet headers to a file with the -w flag. The files to save the output use pcap format and have an extension of .pcap.

    PCAP stands for packet capture. The following command saves 10 lines of output on the eth1 interface to icmp.pcap.

    # tcpdump -i eth1 -c 10 -w icmp.pcap
    tcpdump: listening on eth1, link-type EN10MB (Ethernet), capture size 262144 bytes
    10 packets captured
    10 packets received by filter
    0 packets dropped by kernel

    You can read this file with -r flag:

    tcpdump -r icmp.pcap
    reading from file icmp.pcap, link-type EN10MB (Ethernet)
    05:33:20.852732 IP vagrant-ubuntu-trusty-64 > 10.0.0.51: ICMP echo request, id 3261, seq 33, length 64
    05:33:20.853245 IP 10.0.0.51 > vagrant-ubuntu-trusty-64: ICMP echo reply, id 3261, seq 33, length 64
    05:33:21.852586 IP vagrant-ubuntu-trusty-64 > 10.0.0.51: ICMP echo request, id 3261, seq 34, length 64
    05:33:21.853104 IP 10.0.0.51 > vagrant-ubuntu-trusty-64: ICMP echo reply, id 3261, seq 34, length 64
    05:33:22.852615 IP vagrant-ubuntu-trusty-64 > 10.0.0.51: ICMP echo request, id 3261, seq 35, length 64

    Viewing packet details

    So far we have only seen the packet headers, to view packet contents use -A option. This prints the packet contents in ASCII, which can be of help in network troubleshooting. Also -X flag can be used to display output in hex format. This may not be of much help if the connection is encrypted.

    # tcpdump -c10 -i eth1 -n -A port 80
    23:35:53.109306 IP 10.0.0.1.53916 > 10.0.0.50.80: Flags [P.], seq 2366590408:2366590907, ack 175457677, win 4111, options [nop,nop,TS val 255253117 ecr 5344866], length 499: HTTP: GET / HTTP/1.1
    E..'[email protected]@.%.
    ...
    ..2...P..M.
    uE............
    .6.}.Q.bGET / HTTP/1.1
    Host: 10.0.0.50
    Connection: keep-alive
    Cache-Control: max-age=0
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US,en;q=0.9
    If-Modified-Since: Tue, 04 Mar 2014 11:46:45 GMT

    Conclusion

    tcpdump is easy to set up, and once you understand the output, the various flags, and filters, it can be used to resolve networking issues and securing your network.