The tcpdump utility is one of the popular command line tools for network monitoring in Linux. It helps to debug network traffic and audit it. As the name suggests, tcpdump intercepts TCP packets (Transmission Control Protocol) and displays or saves to a file a data set suitable for analysis, dump. Such network traffic analyzers are called sniffers.

Despite its relative simplicity and undemanding nature, this program has a wide range of features. Tcpdump can help detect malicious traffic, detect an unauthorized entry attempt, determine a possible data leak or launch of a DDoS attack, and much more. Separately, we note that tcpdump can be run on remote servers where there is no graphical shell.

Installing tcpdump

The tcpdump program is included in all major Linux distributions, which makes the installation process as easy as possible.

In the Debian/Ubuntu family, it is enough to execute the command in the terminal:

# apt update && apt install tcpdump

In CentOS/Fedora, the installation command looks like this:

# dnf install tcpdump

tcpdump syntax

The general syntax of the tcpdump command looks like this:

# tcpdump [-i network_interface name] [parameters] [filters]

In the simplest case, you can intercept all network traffic by executing the tcpdump command without parameters on behalf of the superuser. However, if we want to analyze packets passing through a specific network interface (adapter), we can use the -i option. The name of the available interfaces can be found with the command “tcpdump -D”:

if you omit the optional -i parameter, the default interface will be used (as a rule, this is the first adapter in the list). Therefore, it is better to always specify the interface explicitly, for example, “tcpdump -i ens3”. As a result of executing this command, we will get a list of packet headers in something like this form:

If you need more detailed output, you can add the -v, -vv, or -vvv parameter to the command. This will allow you to get, for example, the lifetime, identifier, total length, and other package parameters.

Other useful parameters:

-q — output a minimum of information.
-n — do not use host names and services. By default, tcpdump tries to convert (resolve) IP addresses to domain names, and port numbers to service names. The -nn option will also disable port conversion. This feature can be used to reduce network traffic during network debugging by eliminating additional calls to the Domain Name System (DNS).
-a — Convert broadcast addresses to domain names.
-tttt — use the output of timestamps with a date (by default, only the Unix timestamp is output).
-t — do not display the timestamp in each row.
-c — limit the set of lines in the dump to the specified number.
-w — write the output to a file. We will discuss this possibility in detail later.
-Z is the user on whose behalf the file is being written (by default, saving takes place on behalf of the tcpdump user).

You can find out more about all the parameters on the tcpdump page in the official documentation or by calling the help page with the “man tcpdump” command.

Packet capture with tcpdump

In this screenshot, we can see that tcpdump intercepted 5 TCP/IP packets passing through the ens3 network interface and displayed each in a separate line. Please note that the capture will continue until we press Ctrl+C or the number of lines specified in the -c parameter is reached.

Understanding tcpdump output

For example, let’s look at one of the tcpdump output lines.:

09:10:56.157002 IP debian.48464 > example.com.http: Flags [S], seq 4254890747, win 64240, options [mss 1460,sackOK,TS val 1993676504 ecr 0,nop,wscale 7], length 0

In this line we see:

  1. The Unix timestamp is 09:10:56.157002 (the exact time when the packet was captured). Note that when using the -tttt parameter, the capture date will also be displayed.
  2. The protocol is IP. An http request to the web page was executed in this line. The utility can also output packets transmitted, for example, over udp, icmp, ip6, and so on.
  3. The IP address or hostname and port number of the packet sender is debian.48464 (here tcpdump converted the local IP address on 10.0.2.15 to the name “debian”).
  4. The host name or IP address and port number of the packet recipient — firstvds.ru .http (http in this case means a request to port 80).
  5. The TCP flag is Flags [S] (S is short for SYN, which means the beginning of a connection). There can be several flags, for example [F.] means closing the connection (FIN) and confirming the received packet (dot).
  6. The absolute sequence number of the data in the packet is seq 4254890747.
  7. The window size is win 64240 (this is just the number of bytes in the receive buffer).
  8. TCP — options parameters […].
  9. The payload length is length 0. The payload is considered to be the data transmitted in the packet. Since in this case a connection start request (SYN) is executed, there is no data in the packet.

Our example shows a successful access to a web page, there are no problems with packet transmission.

Packet filtering

In the examples above, we did not use filters, so tcpdump displayed the headers of all packets that passed through the selected network interface. It really looks like a dump. But if we know what specific information we want to get, we can easily clear the output of unnecessary garbage using additional parameters. To do this, the utility provides convenient filtering tools.:

According to the protocol. You can separately monitor tcp, udp, icmp, ip, ip6, arp and other packets by specifying the protocol name in the command. For example, “tcpdump -i ens3 tcp” will output only TCP packets.:

By IP address, host name, or physical (MAC) address. To display information about only one host, you need to specify its IP address or name after the keyword “host”, for example, “tcpdump -i ens3 host example.com “:

By port or port range. For example, to intercept packets arriving only on port 80 (http), you need to add the “port 80” parameter:

By sender (src) or recipient (dst). For example, “tcpdump -i ens3 dst example.com “:

Of course, if we need more precise packet isolation, filters can be combined. The logical operators AND (&&), OR (||) and NOT (!) are used for this.

For example, “tcpdump -i ens3 src 10.0.2.15 and dst port 80” will only output calls from the local IP address to any host on port 80.:

To exclude unnecessary protocols, for example, ICMP, you can add the “not icmp” parameter:

# tcpdump -i ens3 not icmp

If you need more complex filtering rules, you can use parentheses to prioritize them. You can also use additional filters.: 

net for filtering by subnet, 

less or greater to limit the size of packets, and so on.

When composing complex filters, it is recommended to enclose the resulting expression in quotation marks, for example:

# tcpdump -i ens3 "port 80 and (dst 10.0.2.3 or dst 10.0.2.15)"

The tcpdump utility provides a lot of various parameters, filters and their use cases, we have considered only a few basic combinations. We recommend that you read the full reference in the official documentation on tcpdump filters.

Checking the package contents

So far, we’ve only seen the packet headers in the above screenshots. To display their contents, add the -X (output in hexadecimal format) or -A (in ASCII format) parameter to the command, for example, “tcpdump -i ens3 -A src example.com and port 80”:

Please note that you will not be able to analyze the contents of the secure connection (https) packet in this way. In this example, the host responds to an insecure request to port 80 by forwarding (301 Permanently Moved) to a secure address (Location: https://example.com /).

Saving to a file

The data received by the tcpdump command can be saved to a binary file in the format .pcap for subsequent analysis by adding the -w parameter to the command:

# tcpdump -i <interface> -c <number of lines> -w <file path .pcap>

This can be convenient, for example, to record information on a schedule, at night, or when the terminal is unavailable.

Usage example

Let’s do a small security audit and find out which network resources the Linux server accesses immediately after switching on, when the terminal is still unavailable. Of course, it would be possible to view network traffic using a sniffer installed on a separate node in the local network. But to do this, you need, firstly, to have such a node, and secondly— to configure connection forwarding (proxying). It is not always easy to do this with improvised means, tcpdump makes it much easier to solve the problem.

For research, let’s take several popular Linux distributions: Debian/Ubuntu, CentOS/Fedora and AlmaLinux. In each operating system, we will perform a minimal installation and disable all unnecessary services and auto-updates.

To automatically run the tcpdump command after enabling the network interface, create a service file /etc/systemd/system/tcp-dump.service (unit) with something like this content:

[Unit]
Description=TCP dump
Requires=network-online.target
After=network-online.target
[Service]
ExecStart=/usr/bin/tcpdump -i ens3 -c 1000 -w /tmp/tcpdump.pcap
[Install]
WantedBy=multi-user.target

Please note that working with service files requires superuser (root) rights.

It is also worth considering that the path to the tcpdump executable file may be different, it is better to check this point with the “which tcpdump” command:

After saving the file, activate the service with the systemctl enable tcp-dump.service command and reboot the system. Now you can decrypt the received tcpdump.pcap file using the same tcpdump with the -r parameter or in a sniffer with the Wireshark GUI. Let’s take a look at the results (for brevity, duplicate addresses are excluded from the output).

1. Debian

Here we see technical calls to network nodes using IGMP (Internet Group Management Protocol, Internet Group Management Protocol), mDNS (Multicast DNS, Multicast DNS protocol), UDP (User Datagram Protocol, User datagram Protocol) and an external NTP (Network Time Protocol, Network Time Protocol) request.

2. Fedora

In general, the set of services is about the same, but note that the system additionally performs location determination (by requesting a geolocation service beacondb.net ), and also makes more requests to NTP servers located in different countries.

3. AlmaLinux

AlmaLinux is a distribution based on Red Hat Enterprise Linux (RHEL) as a response to the termination of support for the CentOS distribution by Red Hat. As you can see, this operating system behaves much more modestly and performs external requests only to regional network resources.

From the point of view of data confidentiality, it is worth noting that contacting servers or workstations to foreign network services is one of the reasons for conducting traffic audits and checking hardware settings.

Of course, tcpdump’s capabilities are not limited to such simple examples. With this program, you can track mail and virus traffic, separate “white” requests from cyber attacks, and perform many other useful actions.