Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

tcpdump: watch network traffic

39 min read
Improve this page

A customer says the service is down. The application logs show nothing, and Connection refused or timed out do not tell you who blocked the connection. tcpdump displays packets as they pass on a network interface: you see whether the connection request left, whether it arrived, and what answered.

This page has you read a sample capture file, with no special privileges, then capture live. For the difference between TCP and UDP, see TCP vs UDP (in French); for the method that comes before a capture (interface, address, route, DNS), see the network diagnosis methodology (in French) and network diagnosis on Linux. To test a port by hand, see netcat; to scan a network, nmap.

What you will learn

  • Read back a capture with -r and read an output line
  • Filter by host, port and protocol, with and, or, not and the quotes
  • Recognize the [S], [S.], [.], [P.], [F.] and [R.] flags
  • Tell a closed port (an [R.] comes back) from a filtered port ([S] packets with no answer)
  • Choose the interface, capture with -c, save with -w
  • Display the content with -A or -X, and know why a capture is sensitive data

Prerequisites

You know how to open a terminal and move around with cd. You know that a TCP connection opens with a request, an answer and a confirmation, and what a port is (TCP vs UDP, in French). Reading back a capture file needs no privileges. Capturing live needs sudo, python3, curl and nc; on AlmaLinux, nc is ncat, whose messages differ (see netcat).

Authorization: capture only on your own machines, or on a network whose owner has given you written agreement. In France, intercepting electronic correspondence in bad faith, or installing a device designed to do so, is punishable by one year of imprisonment and a fine of 45,000 euros (article 226-15 of the Criminal Code, in French). The framework is summarized in the legal framework for security tests (in French).

Install tcpdump

tcpdump is already installed on the three systems tested for this page: Ubuntu 26.04 (version 4.99.6), Debian 13 (4.99.5) and AlmaLinux 10.3 (4.99.4). The live capture outputs come from Ubuntu 26.04 with 4.99.6, the reading of capture.pcap from 4.99.4. The tcpdump --version command displays the tcpdump version, then that of libpcap, the library that reads the packets.

Terminal window
tcpdump --version
Output, Ubuntu 26.04
tcpdump version 4.99.6
libpcap version 1.10.6 (64-bit time_t, with TPACKET_V3)
[...]

If the shell answers command not found, install the package: sudo apt install tcpdump on Debian and Ubuntu, sudo dnf install tcpdump on AlmaLinux and Rocky. The latest stable version is 4.99.7, of 21 September 2026. Your version number may be newer than these.

Create the practice folder

A real capture contains traffic that you cannot replay on demand. This page therefore provides a small capture.pcap file of five packets, which the command below recreates: base64 -d decodes the text, gunzip decompresses it, > writes it to the file. Its addresses are reserved for documentation. The web folder is used by the live captures further down: an 8-byte page and an 8 MB file.

Terminal window
mkdir ~/tcpdump-lab && cd ~/tcpdump-lab
echo H4sIAAAAAAACA7tyeNNCJgYWBhj4/5+BgRFIL9jIlg3im0ExE4jDBCYZORhcGRg0GBgdGBzY/HoOMDBxHTNOYZ3jwBAAlE0BKQlg+vVBexcDw0KiTQGZADKJIQBoDgPjF6CK1AChXx80tzIwLCLWlCagCSJgtziC3XIC7hZvBobFpPvIiXE3UFIHZopmKwPDEpJ9xLh7jhMkbBl1A0SAphQxMAAAIAu72HYBAAA= | base64 -d | gunzip > capture.pcap
mkdir web && printf 'bonjour\n' > web/index.html && head -c 8000000 /dev/urandom > web/big.bin

mkdir stops if the folder already exists, which avoids overwriting your files. The final cleanup deletes only this folder. The commands that name capture.pcap on its own run from ~/tcpdump-lab: in each new terminal, start with cd ~/tcpdump-lab. Reading back a file with -n makes no DNS request: without -n, tcpdump may resolve the addresses.

The tcpdump command in detail

A tcpdump command reads in this order: the options, the source (a file with -r, an interface with -i), then the filter in single quotes. Each part of the line below is numbered and explained.

Anatomy of the command

Syntaxtcpdump [options] 'filter'

  1. tcpdump the programIt reads packets and prints them.
  2. -n no namesoptionalAddresses and ports as numbers.
  3. -t no timeoptionalRemoves the timestamp from each line.
  4. -r capture.pcap the sourceA file here; -i for an interface.
  5. 'port 80' the filteroptionalThe packets to keep.

Output3 packets

Read back a capture file

-r reads a capture file instead of watching the network, and needs no privileges. You can read the same file back as many times as you like, with a different filter each time: this is what lets you capture once and analyze afterwards. -c 2 stops after two packets, cut -d, -f1 keeps only the start of each line for readability.

Terminal window
tcpdump -n -t -c 2 -r capture.pcap 2> /dev/null | cut -d, -f1
Output
IP 192.0.2.10.40000 > 198.51.100.5.80: Flags [S]
IP 198.51.100.5.80 > 192.0.2.10.40000: Flags [S.]

Two packets, two lines. 2> /dev/null hides the information message that tcpdump writes on the error output (the file read and its format); it stays displayed if you do not hide it.

Turn off name resolution

By default, tcpdump replaces a port number with the service name (443 becomes https), and may query DNS for each address. -n turns off the conversion of addresses and ports into names. With tcpdump 4.99, -nn gives the same result as -n; it remains in common use. On a live capture, resolution slows the display and can block it when DNS does not answer: set -n by default.

To see the difference, read back the packet that goes from 192.0.2.10 to port 443, without -n, and look for the word https:

Terminal window
tcpdump -t -r capture.pcap 'src 192.0.2.10 and dst port 443' 2> /dev/null | grep -o https
Output
https

Without -n, port 443 is displayed as https. -t, used in the following examples, removes the time from each line: the text becomes comparable from one run to the next.

How to read an output line

A tcpdump line says who talks to whom, with which flag, and how many bytes. Here are the packets of port 80 in the capture, in full:

Terminal window
tcpdump -n -t -r capture.pcap 'port 80'
Output
reading from file capture.pcap, link-type EN10MB (Ethernet), snapshot length 65535
IP 192.0.2.10.40000 > 198.51.100.5.80: Flags [S], seq 100, win 64240, length 0
IP 198.51.100.5.80 > 192.0.2.10.40000: Flags [S.], seq 500, ack 101, win 64240, length 0
IP 192.0.2.20.40001 > 198.51.100.5.80: Flags [S], seq 200, win 64240, length 0

The first line comes from tcpdump: the file read and the capture size per packet. After that, each line reads from left to right:

PartMeaning
IPThe packet is IP (v4 here)
192.0.2.10.40000Source address, a dot, source port
>Direction of the packet, from source to destination
198.51.100.5.80Destination address, a dot, destination port
Flags [S]TCP flags (see the next section)
seq 100, ack 101, win 64240Sequence and acknowledgment numbers, advertised window size
length 0Bytes of data in this packet, 0 for a control packet

The essential TCP flags

Flags say which step the conversation is at. A flag is displayed between square brackets, and the dot . means ACK, the acknowledgment.

DisplayNameMeaning
[S]SYNRequest to open a connection
[S.]SYN-ACKThe server accepts the request
[.]ACKAcknowledgment, last step of the opening
[P.]PUSH, ACKData to deliver right away
[F.]FIN, ACKRequest for a normal close
[R] or [R.]RSTAbrupt close or refusal

In the previous output, the first packet is an [S] from the client to port 80, the second an [S.] from the server: the handshake begins. The [.] that ends it is not in this five-packet file. Remember these three situations: an [S] followed by an [S.] (accepted), an [S] followed by an [R.] (refused), an [S] with nothing behind it (a silence is not a refusal).

Filter by host, port and protocol

A filter keeps the packets that match and discards the others without displaying anything. It goes in the last argument. The basic keywords are these:

FilterKeeps the packets whose
host 192.0.2.10source or destination is this address
src 198.51.100.5, dst 198.51.100.5source, or destination, is this address
net 192.0.2.0/24address is in this subnet
port 80source or destination port is 80
dst port 443, portrange 8000-8080destination port is 443, or a port is in the range
tcp, udp, icmp, arpprotocol is this one

src and dst set the direction. Take the packets sent by the server 198.51.100.5:

Terminal window
tcpdump -n -t -r capture.pcap 'src 198.51.100.5' 2> /dev/null | cut -d, -f1
Output
IP 198.51.100.5.80 > 192.0.2.10.40000: Flags [S.]
IP 198.51.100.5.443 > 192.0.2.10.40002: Flags [R.]

Two packets: the server's answers. The [S] requests, whose source is a client, are discarded without any message.

Combine filters

and, or and not combine conditions: host 192.0.2.10 and port 80 keeps what concerns this address and this port. not port 80 keeps everything else. Always put the filter in single quotes: otherwise the shell interprets the parentheses and characters such as [, & or ! before tcpdump sees them. pcap-filter also accepts &&, || and !, as long as you protect them with the same quotes. A condition is grouped with parentheses: 'host 192.0.2.10 and (port 80 or port 443)'. Without parentheses, and and or have the same priority and read from left to right; the exercise below shows what that changes.

How tcpdump picks packets, step by step

Predict before you look at the map. How many packets does the filter host 192.0.2.10 display out of the five packets of the capture? How many remain with host 192.0.2.10 and port 80, and which ones disappear when you add port 80? Then check: tick host then port in the options, follow the five packets one by one and watch the screen on the right.

Mental model

How tcpdump chooses packets

read a packetapply the filterprint it or move on for each packet of the capture

$ tcpdump -n -t -r capture.pcap 2> /dev/null | cut -d, -f1

  1. The packet passes the filter: tcpdump prints it.
  2. The packet passes the filter: tcpdump prints it.
  3. The packet passes the filter: tcpdump prints it.
  4. The packet passes the filter: tcpdump prints it.
  5. The packet passes the filter: tcpdump prints it.
  6. 5 packets printed out of 5.

What the animation shows

  1. A filter looks at both directions: port 80 targets the source and the destination.
  2. An answer is one more packet: the request [S], then [S.] or [R.].
  3. A packet left out leaves no trace. A filter that is too narrow hides the failure.
  4. `[R.]` means refusal: most often, nothing listens on that port.

Golden rules

Put the filter in single quotes

The shell does not touch its parentheses:

tcpdump -n -r capture.pcap 'port 80'

Record, then read again

You can then filter as many times as needed:

tcpdump -n -r capture.pcap

Essential vocabulary

Filter · which packets

src
A single directiontcpdump -n -t -r capture.pcap 'src 198.51.100.5' 2> /dev/null | cut -d, -f1 → 2 answers
and
Both conditionstcpdump -n -t -r capture.pcap 'host 192.0.2.10 and port 80' 2> /dev/null | cut -d, -f1 → 2 packets
tcpflags
Refusals onlytcpdump -n -t -r capture.pcap 'tcp[tcpflags] & tcp-rst != 0' 2> /dev/null | cut -d, -f1 → the [R.]
prot 80
A mistake in the filtertcpdump -n -t -r capture.pcap 'prot 80' 2> /dev/null; echo $? → code 1

Read · what is printed

-c 2
The first twotcpdump -n -t -c 2 -r capture.pcap 2> /dev/null | cut -d, -f1 → 2 lines
-n
The names of the portstcpdump -t -r capture.pcap 'src 192.0.2.10 and dst port 443' 2> /dev/null | grep -o https → https instead of 443
--count
Counttcpdump --count -r capture.pcap 'port 443' 2> /dev/null → 2 packets
-r
A missing capturetcpdump -r absent.pcap 2> /dev/null; echo $? → code 1

Advanced filters on TCP flags

A filter can read the flags themselves: tcp[tcpflags] is the flags byte of the TCP header, tcp-rst and tcp-syn are the values of those bits. tcp[tcpflags] & tcp-rst != 0 keeps the packets whose RST bit is set, whatever the other flags; case B below uses it. The quotes are mandatory, because of the brackets and the &.

In the same way, tcp[13] == 2 designates byte number 13 of the TCP header, the one that holds the flags, and its value 2 is a SYN alone, without ACK: the opening requests, without the answers. The filter does not match a SYN that also carries the ECE or CWR bits (ECN negotiation), and the pcap-filter manual states that these accesses to the TCP header do not work on IPv6 packets.

Terminal window
tcpdump -n -t -r capture.pcap 'tcp[13] == 2' 2> /dev/null | cut -d, -f1
Output
IP 192.0.2.10.40000 > 198.51.100.5.80: Flags [S]
IP 192.0.2.20.40001 > 198.51.100.5.80: Flags [S]
IP 192.0.2.10.40002 > 198.51.100.5.443: Flags [S]

Three connection requests, from two hosts. The server's [S.] and [R.] are discarded: their flags byte is not 2.

Limit the capture

-c N stops after N packets. On a live capture, this is what gives the terminal back. --count, used with -r, displays no packet and only gives their number: handy to know whether a filter keeps anything before reading.

Terminal window
tcpdump --count -r capture.pcap 'port 443' 2> /dev/null
Output
2 packets

By default tcpdump 4.99 keeps 262144 bytes of each packet, which is enough to keep the full content. -s 96 keeps only the first 96 bytes of each packet: the capture is lighter, but it is not a protection, as the section on packet content shows.

The filter that reads from left to right Exercise 1 of 2 · ~7 min · terminal

A colleague wants the packets from host 192.0.2.20 to ports 80 or 443 in capture.pcap. They write 'host 192.0.2.20 and port 80 or port 443' and get a number that looks too large. No error is displayed. You have to compare their command with the version with parentheses.

Count with and without parentheses

Run tcpdump --count -r capture.pcap with your colleague's filter in single quotes, then with 'host 192.0.2.20 and (port 80 or port 443)', and compare the two numbers. Then display the packets of the first command (-n -t, cut -d, -f1) to spot the ones that do not come from 192.0.2.20.

I have tried: show the solution
Terminal window
tcpdump --count -r capture.pcap 'host 192.0.2.20 and port 80 or port 443' 2> /dev/null
tcpdump --count -r capture.pcap 'host 192.0.2.20 and (port 80 or port 443)' 2> /dev/null
tcpdump -n -t -r capture.pcap 'host 192.0.2.20 and port 80 or port 443' 2> /dev/null | cut -d, -f1
Outputs, in order
3 packets
1 packet
IP 192.0.2.20.40001 > 198.51.100.5.80: Flags [S]
IP 192.0.2.10.40002 > 198.51.100.5.443: Flags [S]
IP 198.51.100.5.443 > 192.0.2.10.40002: Flags [R.]

Without parentheses, and and or have the same priority and read from left to right: the filter means (host 192.0.2.20 and port 80) or port 443. It therefore keeps the host's [S], but also every packet of port 443, whichever host it comes from. With parentheses, only the intended combination remains. A valid but badly built filter produces no error: always compare the number of packets before and after.

List the available network interfaces

A capture started on the wrong interface stays empty, and you hunt for a failure that does not exist. tcpdump -D lists the interfaces tcpdump can open, with their state. any captures on all interfaces at once, lo is the loopback interface (the machine's traffic to itself).

Terminal window
tcpdump -D
Output, Ubuntu 26.04, tcpdump 4.99.6
1.enp5s0 [Up, Running, Connected]
2.any (Pseudo-device that captures on all interfaces) [Up, Running]
3.lo [Up, Running, Loopback]
[...]

The same command with or without sudo gives the same list. The network interface of this machine is called enp5s0; yours will have another name. The following entries (bluetooth-monitor, nflog, dbus-system...) are not network cards. To find out which interface the traffic to an address leaves by, ask the routing table with ip route get; the ip command explains its output. Here ip route get 198.51.100.5 answers 198.51.100.5 via (gateway) dev enp5s0 src (IP) uid 1001: the word after dev gives the interface to pass to -i. For a container, the traffic goes through a virtual bridge whose name depends on the network created: read it with tcpdump -D rather than guessing it.

Capture live

A live capture creates a capture socket, which requires the CAP_NET_RAW capability: use sudo. Promiscuous mode is not the reason for this privilege, since capturing on any works without it. With -i, you choose the interface, with -c the number of packets that gives the terminal back. The captures on this page watch a small web server on the loopback interface, which you start once, in Terminal 1, and leave running until the cleanup. Open three terminals numbered 1 to 3: the server, the capture, then the traffic.

Terminal window
python3 -m http.server 8080 --bind 127.0.0.1 --directory ~/tcpdump-lab/web

In Terminal 2, capture the first ten packets of port 8080:

Terminal window
sudo tcpdump -n -t -i lo -c 10 'tcp port 8080'

In Terminal 3, send a request, which displays bonjour:

Terminal window
curl -s http://127.0.0.1:8080/
Output of Terminal 2, Ubuntu 26.04, tcpdump 4.99.6
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytes
IP 127.0.0.1.38506 > 127.0.0.1.8080: Flags [S], seq 3783019138, win 65495, options [mss 65495,sackOK,TS val 1513048606 ecr 0,nop,wscale 8], length 0
IP 127.0.0.1.8080 > 127.0.0.1.38506: Flags [S.], seq 1217845714, ack 3783019139, win 65483, options [mss 65495,sackOK,TS val 2157035347 ecr 1513048606,nop,wscale 8], length 0
IP 127.0.0.1.38506 > 127.0.0.1.8080: Flags [.], ack 1, win 256, options [nop,nop,TS val 1513048606 ecr 2157035347], length 0
IP 127.0.0.1.38506 > 127.0.0.1.8080: Flags [P.], seq 1:79, ack 1, win 256, options [nop,nop,TS val 1513048606 ecr 2157035347], length 78: HTTP: GET / HTTP/1.1
[...]
IP 127.0.0.1.8080 > 127.0.0.1.38506: Flags [F.], seq 193, ack 79, win 256, options [nop,nop,TS val 2157035350 ecr 1513048609], length 0
10 packets captured
24 packets received by filter
0 packets dropped by kernel

Read the first four lines: [S], [S.], [.] are the handshake, the next [P.] carries the GET / HTTP/1.1 request. tcpdump also displays the first line of the application protocol it recognizes. A complete curl makes 12 packets: -c 10 stops the capture before the last two.

The three final counters say how many packets tcpdump received and processed (captured, 10), how many the system counted as received by the filter (received by filter, 24) and how many the kernel dropped for lack of room in its buffer (dropped by kernel). The manual states that the meaning of received by filter depends on the system: depending on the case, it counts the packets before or after the filter, read by tcpdump or not yet. A non-zero dropped counter means the capture is incomplete.

Save to a pcap file

-w writes the packets to a file in pcap format, which tcpdump -r and Wireshark can read, instead of displaying them. Filters apply to writing too. On Ubuntu 26.04, Debian 13 and AlmaLinux 10.3, a tcpdump started with sudo writes the file under the tcpdump user, not under yours: AlmaLinux announces it with dropped privs to tcpdump. -Z $USER makes it write under your account. Terminal 2:

Terminal window
sudo tcpdump -n -i lo -c 10 -Z $USER -w ~/tcpdump-lab/direct.pcap 'tcp port 8080'

Terminal 3:

Terminal window
curl -s http://127.0.0.1:8080/ > /dev/null
Output of Terminal 2
tcpdump: listening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytes
10 packets captured
38 packets received by filter
0 packets dropped by kernel

The file is binary: read it back with tcpdump -r, never with cat. It contains everything that traveled, so remove the access of other accounts with chmod 600. The -rw------- mode leaves access to your account only (the user is ansible on the test machine, yours will be different). Terminal 2, read it back:

Terminal window
chmod 600 ~/tcpdump-lab/direct.pcap
ls -l ~/tcpdump-lab/direct.pcap
tcpdump -n -t -r ~/tcpdump-lab/direct.pcap | head -3
Output
-rw------- 1 ansible ansible 1130 Nov 25 15:36 /home/ansible/tcpdump-lab/direct.pcap
IP 127.0.0.1.54352 > 127.0.0.1.8080: Flags [S], seq 2088170451, win 65495, options [mss 65495,sackOK,TS val 1694247743 ecr 0,nop,wscale 8], length 0
[...]

Read a capture to confirm or rule out a failure hypothesis

A capture is useless if you do not know what you are looking for. Proceed in this order: formulate a hypothesis ("the server is stopped", "a firewall blocks", "the port is closed"), capture the traffic of the port concerned only, then read the flags of the first exchange to decide. Cases A and B are read first on capture.pcap, then live; case C needs a live capture.

Case A: "the server does not answer"

A user says the page does not load, so the server must be down. On capture.pcap, which comes from the server, the filter 'host 192.0.2.10 and port 80' keeps only two packets, an [S] then an [S.]: the server received the request and answered, so the hypothesis "the server is stopped" does not hold. This capture does not prove that the client received the answer, because it does not contain the final [.] and comes from the server: an answer lost on the way would not show. The complete handshake is read in the live capture of Terminal 2, made above: [S], [S.], [.], then GET / HTTP/1.1 and HTTP/1.0 200 OK. When this sequence is complete, the service is reachable and the problem lies outside the network: content served, proxy, TLS, DNS on the client side.

Case B: "a firewall blocks the port"

Another port fails with Connection refused, and the reflex is to blame the firewall. On capture.pcap, look for the refusal packets:

Terminal window
tcpdump -n -t -r capture.pcap 'tcp[tcpflags] & tcp-rst != 0' 2> /dev/null | cut -d, -f1
Output
IP 198.51.100.5.443 > 192.0.2.10.40002: Flags [R.]

The same refusal live on the loopback interface: port 9999 has no service. Terminal 2, with -ttt for the time gap between packets:

Terminal window
sudo tcpdump -n -ttt -i lo -c 2 'tcp port 9999'

Terminal 3:

Terminal window
nc -zv 127.0.0.1 9999
Output of Terminal 2
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytes
00:00:00.000000 IP 127.0.0.1.59110 > 127.0.0.1.9999: Flags [S], seq 4288212374, win 65495, options [mss 65495,sackOK,TS val 3265399529 ecr 0,nop,wscale 8], length 0
00:00:00.000008 IP 127.0.0.1.9999 > 127.0.0.1.59110: Flags [R.], seq 0, ack 4288212375, win 0, length 0
2 packets captured
4 packets received by filter
0 packets dropped by kernel

nc answers nc: connect to 127.0.0.1 port 9999 (tcp) failed: Connection refused. Only two packets: the [S], then an [R.] 8 microseconds later. An [R.] is an active refusal: the machine received the request and rejects it. A firewall set to drop does not answer, it makes the packet disappear: the hypothesis "a firewall in drop blocks this port" is therefore the only one this capture rules out. Most often, nothing listens on the port, or the service listens on another address. A firewall rule reject with tcp reset gives the same capture (recorded on port 9998 of lo: one [S], one [R.]), and sudo ss -tlnp on the server tells you whether a service listens.

Case C: "the port is closed"

Last case, a port that times out instead of refusing. In the port 80 capture seen above, an [S] came from another address without an [S.] following it. A single packet is not enough to conclude: a client retries its request if nothing comes back. To see it, capture live a connection to an address that does not answer. 192.0.2.1 is reserved for documentation, nobody answers there. The filter adds or icmp: a router may refuse with an ICMP message, which the capture must show. Terminal 2:

Terminal window
sudo tcpdump -n -ttt -i any -c 5 '(host 192.0.2.1 and port 80) or icmp'

Terminal 3:

Terminal window
nc -zv -w 5 192.0.2.1 80
Output of Terminal 2, Ubuntu 26.04, tcpdump 4.99.6
tcpdump: WARNING: any: That device doesn't support promiscuous mode
(Promiscuous mode not supported on the "any" device)
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on any, link-type LINUX_SLL2 (Linux cooked v2), snapshot length 262144 bytes
00:00:00.000000 enp5s0 Out IP (IP).45274 > 192.0.2.1.80: Flags [S], seq 897936934, win 64240, options [mss 1460,sackOK,TS val 3140229399 ecr 0,nop,wscale 8], length 0
00:00:01.006878 enp5s0 Out IP (IP).45274 > 192.0.2.1.80: Flags [S], seq 897936934, win 64240, options [mss 1460,sackOK,TS val 3140230406 ecr 0,nop,wscale 8], length 0
[...]
00:00:01.024002 enp5s0 Out IP (IP).45274 > 192.0.2.1.80: Flags [S], seq 897936934, win 64240, options [mss 1460,sackOK,TS val 3140233478 ecr 0,nop,wscale 8], length 0
5 packets captured
7 packets received by filter
0 packets dropped by kernel

nc answers nc: connect to 192.0.2.1 port 80 (tcp) timed out: Operation now in progress; the capture ended in 4 seconds. Look at the seq and the time gap: five identical [S] (same sequence number 897936934), about one second apart, and no answer, neither [S.] nor [R.]. No ICMP message came back in this run. The pace of the [S] packets depends on the client's kernel.

A closed port would have answered right away, as in case B: the hypothesis "closed port" is ruled out. The packets are dropped somewhere or do not arrive: firewall in drop, routing, or capture made on the wrong side. nmap calls a port in this situation filtered.

On any, tcpdump cannot put the interface in promiscuous mode, hence the harmless warning of the first two lines. Each line gains an interface column (enp5s0) and the direction of the packet relative to this machine, Out (outgoing) or In (incoming).

What the capture showsWhat it indicatesWhere to look
[S], [S.], [.]The connection opens: the service answersApplication side: HTTP error, TLS, slowness
[S], then [R.] right awayActive refusal by the machine: most often nothing listens (a reject with tcp reset gives the same capture)Service stopped, or bound to another address; reject rule
[S] repeated with no answerPackets dropped (drop), routing, or capture on the wrong sideFirewall, route, side of the capture (capture on the server side too)
Nothing, not even the [S]The traffic does not go through this interfaceWrong interface, or filter too narrow
An open port, a closed port Exercise 2 of 2 · ~10 min · terminal

The server of Terminal 1 listens on port 8080. You have to record what happens when nc tests this port then port 9997, read the file back and give a verdict for each. You need three terminals, as above.

Capture to a file

Terminal 2: start a capture on lo with the filter 'tcp port 8080 or tcp port 9997', which stops after 8 packets and writes ~/tcpdump-lab/ex.pcap under your account.

Test, read back and count

Terminal 3: run nc -zv 127.0.0.1 8080 then nc -zv 127.0.0.1 9997, and read their messages. Terminal 2: read ex.pcap back with -n -t, then count with --count the packets of the RST filter from case B.

I have tried: show the solution

Terminal 2:

Terminal window
sudo tcpdump -n -i lo -c 8 -Z $USER -w ~/tcpdump-lab/ex.pcap 'tcp port 8080 or tcp port 9997'

Terminal 3 (nc displays Connection to 127.0.0.1 8080 port [tcp/http-alt] succeeded!, then nc: connect to 127.0.0.1 port 9997 (tcp) failed: Connection refused):

Terminal window
nc -zv 127.0.0.1 8080; nc -zv 127.0.0.1 9997

Terminal 2, once the capture is finished:

Terminal window
tcpdump -n -t -r ~/tcpdump-lab/ex.pcap
Output of the read back (Terminal 2)
reading from file /home/ansible/tcpdump-lab/ex.pcap, link-type EN10MB (Ethernet), snapshot length 262144
IP 127.0.0.1.41916 > 127.0.0.1.8080: Flags [S], seq 3508296909, win 65495, options [mss 65495,sackOK,TS val 3438521258 ecr 0,nop,wscale 8], length 0
IP 127.0.0.1.8080 > 127.0.0.1.41916: Flags [S.], seq 1660766030, ack 3508296910, win 65483, options [mss 65495,sackOK,TS val 1691912114 ecr 3438521258,nop,wscale 8], length 0
IP 127.0.0.1.41916 > 127.0.0.1.8080: Flags [.], ack 1, win 256, options [nop,nop,TS val 3438521258 ecr 1691912114], length 0
[...]
IP 127.0.0.1.54760 > 127.0.0.1.9997: Flags [S], seq 881530149, win 65495, options [mss 65495,sackOK,TS val 1218639766 ecr 0,nop,wscale 8], length 0
IP 127.0.0.1.9997 > 127.0.0.1.54760: Flags [R.], seq 0, ack 881530150, win 0, length 0

Port 8080 shows the handshake then the close ([F.]), port 9997 an [S] followed by an [R.]: active refusal, nothing listens. tcpdump -n -r ~/tcpdump-lab/ex.pcap --count 'tcp[tcpflags] & tcp-rst != 0' returns 1 packet: a single refusal, the one of port 9997. The file holds 8 packets, the 6 of a successful nc -zv and the 2 of a refusal: this is what makes -c 8 give the terminal back.

Show packet contents, and why it is sensitive

-A displays the data of each packet as text, -X in hexadecimal with the text beside it. On unencrypted traffic such as HTTP, you read the request in clear text. Terminal 2:

Terminal window
sudo tcpdump -n -i lo -A -c 8 'tcp port 8080'

Terminal 3, with a demonstration login and password:

Terminal window
curl -s -u demo:demo http://127.0.0.1:8080/ > /dev/null
Output of Terminal 2, excerpt of the fourth packet
15:36:48.000334 IP 127.0.0.1.54150 > 127.0.0.1.8080: Flags [P.], seq 1:114, ack 1, win 256, options [nop,nop,TS val 930590255 ecr 1757660206], length 113: HTTP: GET / HTTP/1.1
[...]
GET / HTTP/1.1
Host: 127.0.0.1:8080
Authorization: Basic ZGVtbzpkZW1v
User-Agent: curl/8.18.0
Accept: */*

The request is readable as is, with the Authorization header. This value is not encrypted: Basic encodes the text demo:demo in base64, which base64 -d decodes in one command. -X gives the same content in another form: each 0x0030: line displays an offset, 16 bytes in hexadecimal, then the same bytes as text.

A capture is therefore personal data and a potential secret: it can contain logins, tokens, people's IP addresses, the content of their messages.

Redo the same capture with -s 96 (Terminal 2: sudo tcpdump -n -i lo -s 96 -A -c 8 'tcp port 8080', same curl in Terminal 3): the request stops after Host: 127.0.0. and the Authorization header disappears. The response body, however, stays readable: the bonjour line is displayed in full, since a short body fits under 96 bytes. This is not a protection. Three rules instead: capture as little as possible (a precise filter, -c), restrict the file permissions (chmod 600) and delete it as soon as the analysis is done. Encrypted traffic (HTTPS) cannot be read with -A, you see unreadable bytes. Never share a raw capture with a third party without having read it through.

Automatic file rotation

A long capture fills the disk. -C N closes the current file and opens a new one when it exceeds N million bytes (1,000,000 bytes, not 1,048,576); tcpdump checks the size before writing each packet, so a file exceeds N slightly. -W N limits the number of files: with -C, according to the manual, tcpdump overwrites the oldest ones in a loop. With -W 3, the files of the test run are called rot.pcap0, rot.pcap1 and rot.pcap2; without -W, the manual states that the first keeps the name given to -w and that the following ones get a number (rot.pcap1...), to check on your machine.

Without -Z $USER, the command fails in your home folder, on Ubuntu 26.04, Debian 13 and AlmaLinux 10.3: tcpdump: /home/ansible/tcpdump-lab/rot.pcap0: Permission denied, even for the first file. With -Z $USER, the files open. This capture has no -c: start it in Terminal 2, do the three downloads of the 8 MB file in Terminal 3, then stop the capture with Ctrl + C and list the files with ls -l --time-style=full-iso ~/tcpdump-lab/rot*.

Terminal window
sudo tcpdump -n -i lo -C 1 -W 3 -Z $USER -w ~/tcpdump-lab/rot.pcap 'tcp port 8080'
Terminal window
for i in 1 2 3; do curl -s -o /dev/null http://127.0.0.1:8080/big.bin; done
Output of Terminal 2 at the stop, then of ls, Ubuntu 26.04
tcpdump: listening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytes
376 packets captured
1572 packets received by filter
818 packets dropped by kernel
-rw-r--r-- 1 ansible ansible 1051060 2026-11-25 15:38:53.670358301 +0000 /home/ansible/tcpdump-lab/rot.pcap0
-rw-r--r-- 1 ansible ansible 1051060 2026-11-25 15:38:54.642233527 +0000 /home/ansible/tcpdump-lab/rot.pcap1
-rw-r--r-- 1 ansible ansible 728 2026-11-25 15:38:59.642926367 +0000 /home/ansible/tcpdump-lab/rot.pcap2

Look at the last counter: 818 packets lost by the kernel on this dense traffic (8 MB in an instant on lo), and only 376 captured out of 1572. Rotation limits disk space, it does not guarantee a complete capture. Debian 13 lost 667 packets and AlmaLinux 10.3 451 in the same test: record your own. -G N opens a new file every N seconds, and with -W N tcpdump stops (Maximum file limit reached: N, code 0) once N files are created.

Which command should you choose?

tcpdump watches the packets on one side of the machine. Depending on the question, another tool answers faster.

NeedCommand
See what listens on the machiness -tlnp
Test a port by handnetcat
Scan a range, detect servicesnmap
Analyze a pcap file in a graphical interfaceWireshark, which reads the files written with -w
Capture in a Kubernetes Podcapture a Pod's traffic

Troubleshooting

Start by checking the interface and the filter, then the return code.

SymptomCauseFix
tcpdump: any: You don't have permission to perform this capture on that device and (Attempt to create packet socket failed - CAP_NET_RAW may be required)A live capture needs privilegesRun with sudo; reading a file back needs nothing
tcpdump: absent0: No such device existsWrong interface nametcpdump -D, copy the exact name
tcpdump: /home/…/rot.pcap0: Permission denied with -C or -Gtcpdump, started by sudo without -Z, could not create the file under the tcpdump userAdd -Z $USER
The capture shows nothingWrong interface, filter too narrow, or traffic blocked upstreamTry -i any with no filter, then add the filter
No output from -r, code 1, or tcpdump: can't parse filter expression: syntax errorBadly written filter or missing fileRemove 2> /dev/null, read the message, check echo $?
Shell syntax error before tcpdump, or a terminal that scrolls endlesslyFilter without single quotes, or neither -c nor a filterPut it between '...'; add -c N or a port N filter

Clean up the practice folder

Stop the server of Terminal 1 with Ctrl + C, then delete the only folder created by this page. Re-read the name before running rm -r.

Terminal window
cd ~
rm -r ~/tcpdump-lab

Key points

  • tcpdump -r file reads a capture back without privileges; a live capture (-i) needs sudo, and -c N gives the terminal back. Put the filter in single quotes, and -n on a live capture.
  • A host filter hides the other machines, and without parentheses and and or read from left to right: a valid but badly built filter reports nothing.
  • [S] then [S.]: the service answers. [S] then [R.]: active refusal, most often nothing listens (a reject with tcp reset does the same). [S] repeated with no answer: the packets are dropped.
  • -w writes a pcap file: -Z $USER to write it under your account and for rotation with -C or -G, chmod 600 to protect it. Rotation does not prevent lost packets on dense traffic.
  • A capture contains personal data: -A shows logins in clear text, and -s 96 does not protect them. Capture little, protect the file, capture only with authorization.

Knowledge check

The questions cover reading a line, filters, saving and reading back, and reading the three failure cases. The pass mark is 80%.

Knowledge check

Check what you have learned with this interactive quiz

10 questions
6 min
80% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer

FAQ: frequently asked questions