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
-rand read an output line - Filter by host, port and protocol, with
and,or,notand 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
-Aor-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.
tcpdump --versiontcpdump version 4.99.6libpcap 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.
mkdir ~/tcpdump-lab && cd ~/tcpdump-labecho H4sIAAAAAAACA7tyeNNCJgYWBhj4/5+BgRFIL9jIlg3im0ExE4jDBCYZORhcGRg0GBgdGBzY/HoOMDBxHTNOYZ3jwBAAlE0BKQlg+vVBexcDw0KiTQGZADKJIQBoDgPjF6CK1AChXx80tzIwLCLWlCagCSJgtziC3XIC7hZvBobFpPvIiXE3UFIHZopmKwPDEpJ9xLh7jhMkbBl1A0SAphQxMAAAIAu72HYBAAA= | base64 -d | gunzip > capture.pcapmkdir web && printf 'bonjour\n' > web/index.html && head -c 8000000 /dev/urandom > web/big.binmkdir 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'
tcpdump-n-t-r capture.pcap'port 80' -
tcpdumpthe programIt reads packets and prints them. -
-nno namesoptionalAddresses and ports as numbers. -
-tno timeoptionalRemoves the timestamp from each line. -
-r capture.pcapthe sourceA file here;-ifor an interface. -
'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.
tcpdump -n -t -c 2 -r capture.pcap 2> /dev/null | cut -d, -f1IP 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:
tcpdump -t -r capture.pcap 'src 192.0.2.10 and dst port 443' 2> /dev/null | grep -o httpshttpsWithout -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:
tcpdump -n -t -r capture.pcap 'port 80'reading from file capture.pcap, link-type EN10MB (Ethernet), snapshot length 65535IP 192.0.2.10.40000 > 198.51.100.5.80: Flags [S], seq 100, win 64240, length 0IP 198.51.100.5.80 > 192.0.2.10.40000: Flags [S.], seq 500, ack 101, win 64240, length 0IP 192.0.2.20.40001 > 198.51.100.5.80: Flags [S], seq 200, win 64240, length 0The first line comes from tcpdump: the file read and the capture size per packet. After that, each line reads from left to right:
| Part | Meaning |
|---|---|
IP | The packet is IP (v4 here) |
192.0.2.10.40000 | Source address, a dot, source port |
> | Direction of the packet, from source to destination |
198.51.100.5.80 | Destination address, a dot, destination port |
Flags [S] | TCP flags (see the next section) |
seq 100, ack 101, win 64240 | Sequence and acknowledgment numbers, advertised window size |
length 0 | Bytes 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.
| Display | Name | Meaning |
|---|---|---|
[S] | SYN | Request to open a connection |
[S.] | SYN-ACK | The server accepts the request |
[.] | ACK | Acknowledgment, last step of the opening |
[P.] | PUSH, ACK | Data to deliver right away |
[F.] | FIN, ACK | Request for a normal close |
[R] or [R.] | RST | Abrupt 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:
| Filter | Keeps the packets whose |
|---|---|
host 192.0.2.10 | source or destination is this address |
src 198.51.100.5, dst 198.51.100.5 | source, or destination, is this address |
net 192.0.2.0/24 | address is in this subnet |
port 80 | source or destination port is 80 |
dst port 443, portrange 8000-8080 | destination port is 443, or a port is in the range |
tcp, udp, icmp, arp | protocol is this one |
src and dst set the direction. Take the packets sent by the server 198.51.100.5:
tcpdump -n -t -r capture.pcap 'src 198.51.100.5' 2> /dev/null | cut -d, -f1IP 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
Try Packets of capture.pcap
Filter, then print
What the packet says
$ tcpdump -n -t -r capture.pcap 2> /dev/null | cut -d, -f1
- The packet passes the filter: tcpdump prints it.
- The packet passes the filter: tcpdump prints it.
- The packet passes the filter: tcpdump prints it.
- The packet passes the filter: tcpdump prints it.
- The packet passes the filter: tcpdump prints it.
- 5 packets printed out of 5.
What the animation shows
- A filter looks at both directions:
port 80targets the source and the destination. - An answer is one more packet: the request
[S], then[S.]or[R.]. - A packet left out leaves no trace. A filter that is too narrow hides the failure.
- `[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.pcapEssential vocabulary
Filter · which packets
src- A single direction
tcpdump -n -t -r capture.pcap 'src 198.51.100.5' 2> /dev/null | cut -d, -f1→ 2 answers and- Both conditions
tcpdump -n -t -r capture.pcap 'host 192.0.2.10 and port 80' 2> /dev/null | cut -d, -f1→ 2 packets tcpflags- Refusals only
tcpdump -n -t -r capture.pcap 'tcp[tcpflags] & tcp-rst != 0' 2> /dev/null | cut -d, -f1→ the [R.] prot 80- A mistake in the filter
tcpdump -n -t -r capture.pcap 'prot 80' 2> /dev/null; echo $?→ code 1
Read · what is printed
-c 2- The first two
tcpdump -n -t -c 2 -r capture.pcap 2> /dev/null | cut -d, -f1→ 2 lines -n- The names of the ports
tcpdump -t -r capture.pcap 'src 192.0.2.10 and dst port 443' 2> /dev/null | grep -o https→ https instead of 443 --count- Count
tcpdump --count -r capture.pcap 'port 443' 2> /dev/null→ 2 packets -r- A missing capture
tcpdump -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.
tcpdump -n -t -r capture.pcap 'tcp[13] == 2' 2> /dev/null | cut -d, -f1IP 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.
tcpdump --count -r capture.pcap 'port 443' 2> /dev/null2 packetsBy 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
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.
- tcpdump --count
- tcpdump -r
I have tried: show the solution
tcpdump --count -r capture.pcap 'host 192.0.2.20 and port 80 or port 443' 2> /dev/nulltcpdump --count -r capture.pcap 'host 192.0.2.20 and (port 80 or port 443)' 2> /dev/nulltcpdump -n -t -r capture.pcap 'host 192.0.2.20 and port 80 or port 443' 2> /dev/null | cut -d, -f13 packets1 packetIP 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).
tcpdump -D1.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.
python3 -m http.server 8080 --bind 127.0.0.1 --directory ~/tcpdump-lab/webIn Terminal 2, capture the first ten packets of port 8080:
sudo tcpdump -n -t -i lo -c 10 'tcp port 8080'In Terminal 3, send a request, which displays bonjour:
curl -s http://127.0.0.1:8080/tcpdump: verbose output suppressed, use -v[v]... for full protocol decodelistening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytesIP 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 0IP 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 0IP 127.0.0.1.38506 > 127.0.0.1.8080: Flags [.], ack 1, win 256, options [nop,nop,TS val 1513048606 ecr 2157035347], length 0IP 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 010 packets captured24 packets received by filter0 packets dropped by kernelRead 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:
sudo tcpdump -n -i lo -c 10 -Z $USER -w ~/tcpdump-lab/direct.pcap 'tcp port 8080'Terminal 3:
curl -s http://127.0.0.1:8080/ > /dev/nulltcpdump: listening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytes10 packets captured38 packets received by filter0 packets dropped by kernelThe 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:
chmod 600 ~/tcpdump-lab/direct.pcapls -l ~/tcpdump-lab/direct.pcaptcpdump -n -t -r ~/tcpdump-lab/direct.pcap | head -3-rw------- 1 ansible ansible 1130 Nov 25 15:36 /home/ansible/tcpdump-lab/direct.pcapIP 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:
tcpdump -n -t -r capture.pcap 'tcp[tcpflags] & tcp-rst != 0' 2> /dev/null | cut -d, -f1IP 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:
sudo tcpdump -n -ttt -i lo -c 2 'tcp port 9999'Terminal 3:
nc -zv 127.0.0.1 9999tcpdump: verbose output suppressed, use -v[v]... for full protocol decodelistening 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 02 packets captured4 packets received by filter0 packets dropped by kernelnc 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:
sudo tcpdump -n -ttt -i any -c 5 '(host 192.0.2.1 and port 80) or icmp'Terminal 3:
nc -zv -w 5 192.0.2.1 80tcpdump: 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 decodelistening 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 05 packets captured7 packets received by filter0 packets dropped by kernelnc 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 shows | What it indicates | Where to look |
|---|---|---|
[S], [S.], [.] | The connection opens: the service answers | Application side: HTTP error, TLS, slowness |
[S], then [R.] right away | Active 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 answer | Packets dropped (drop), routing, or capture on the wrong side | Firewall, route, side of the capture (capture on the server side too) |
Nothing, not even the [S] | The traffic does not go through this interface | Wrong interface, or filter too narrow |
An open port, a closed port
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.
- sudo tcpdump
- -w
- -Z
- tcpdump -r
I have tried: show the solution
Terminal 2:
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):
nc -zv 127.0.0.1 8080; nc -zv 127.0.0.1 9997Terminal 2, once the capture is finished:
tcpdump -n -t -r ~/tcpdump-lab/ex.pcapreading from file /home/ansible/tcpdump-lab/ex.pcap, link-type EN10MB (Ethernet), snapshot length 262144IP 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 0IP 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 0IP 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 0IP 127.0.0.1.9997 > 127.0.0.1.54760: Flags [R.], seq 0, ack 881530150, win 0, length 0Port 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:
sudo tcpdump -n -i lo -A -c 8 'tcp port 8080'Terminal 3, with a demonstration login and password:
curl -s -u demo:demo http://127.0.0.1:8080/ > /dev/null15: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.1Host: 127.0.0.1:8080Authorization: Basic ZGVtbzpkZW1vUser-Agent: curl/8.18.0Accept: */*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*.
sudo tcpdump -n -i lo -C 1 -W 3 -Z $USER -w ~/tcpdump-lab/rot.pcap 'tcp port 8080'for i in 1 2 3; do curl -s -o /dev/null http://127.0.0.1:8080/big.bin; donetcpdump: listening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytes376 packets captured1572 packets received by filter818 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.pcap2Look 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.
| Need | Command |
|---|---|
| See what listens on the machine | ss -tlnp |
| Test a port by hand | netcat |
| Scan a range, detect services | nmap |
| Analyze a pcap file in a graphical interface | Wireshark, which reads the files written with -w |
| Capture in a Kubernetes Pod | capture a Pod's traffic |
Troubleshooting
Start by checking the interface and the filter, then the return code.
| Symptom | Cause | Fix |
|---|---|---|
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 privileges | Run with sudo; reading a file back needs nothing |
tcpdump: absent0: No such device exists | Wrong interface name | tcpdump -D, copy the exact name |
tcpdump: /home/…/rot.pcap0: Permission denied with -C or -G | tcpdump, started by sudo without -Z, could not create the file under the tcpdump user | Add -Z $USER |
| The capture shows nothing | Wrong interface, filter too narrow, or traffic blocked upstream | Try -i any with no filter, then add the filter |
No output from -r, code 1, or tcpdump: can't parse filter expression: syntax error | Badly written filter or missing file | Remove 2> /dev/null, read the message, check echo $? |
| Shell syntax error before tcpdump, or a terminal that scrolls endlessly | Filter without single quotes, or neither -c nor a filter | Put 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.
cd ~rm -r ~/tcpdump-labKey points
tcpdump -r filereads a capture back without privileges; a live capture (-i) needssudo, and-c Ngives the terminal back. Put the filter in single quotes, and-non a live capture.- A
hostfilter hides the other machines, and without parenthesesandandorread 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 (areject with tcp resetdoes the same).[S]repeated with no answer: the packets are dropped.-wwrites a pcap file:-Z $USERto write it under your account and for rotation with-Cor-G,chmod 600to protect it. Rotation does not prevent lost packets on dense traffic.- A capture contains personal data:
-Ashows logins in clear text, and-s 96does 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
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
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
A new full quiz with random questions
Work again only on the questions you missed
FAQ: frequently asked questions
Main characteristics
| Aspect | Description |
|---|---|
| Type | Command-line packet analyzer (sniffer) |
| File format | pcap, which Wireshark and tshark can read |
| Installation | Already present on Ubuntu 26.04, Debian 13 and AlmaLinux 10.3; otherwise sudo apt install tcpdump or sudo dnf install tcpdump |
| Privileges | Live capture: the CAP_NET_RAW capability, for example with sudo. Reading a file back with -r: no privileges |
Typical use cases
- Network diagnosis: why does this connection fail?
- Firewall: tell a refusal from a silence
- Debugging: see HTTP requests in clear text
- After-the-fact analysis: read back a capture saved with
-w
Comparison
| Aspect | tcpdump | Wireshark |
|---|---|---|
| Interface | Command line | Graphical |
| Server use | Works in an SSH session | Needs a desktop, or a pcap file copied to your workstation |
| Reading packets | One line per packet, BPF filters | Detailed decoding per protocol, conversation browsing |
| Automation | Shell scripts | tshark, the command-line version |
| Files | Writes and reads back the pcap format | Reads the files written with -w |
When to use tcpdump?
- Capture on a remote server with no graphical interface
- Start a capture from a script
- Make a first diagnosis: did the request leave, did anyone answer?
When to use Wireshark?
- Analyze a conversation in detail, protocol by protocol
- Follow a flow from end to end in a graphical interface
- Write filters with autocompletion
Usual sequence
# 1. Capture on the server and write a pcap file
sudo tcpdump -nn -i eth0 -w capture.pcap
# 2. Copy the file to your workstation
scp user@serveur:capture.pcap .
# 3. Open it in Wireshark
wireshark capture.pcap
sudo provides it because root has it.What tcpdump displays without this privilege
tcpdump: any: You don't have permission to perform this capture on that device
(Attempt to create packet socket failed - CAP_NET_RAW may be required)
The tcpdump manual says that reading the packets of an interface may require special privileges, and that reading back a capture file requires none.What about promiscuous mode?
In promiscuous mode, a network card also passes up frames addressed to other machines. tcpdump enables it by default on an interface, and-p turns it off. This mode does not explain the privilege required: on the any interface, which does not support it, the capture requires the same privilege.Avoiding sudo
# Give the capabilities to the binary
sudo setcap cap_net_raw,cap_net_admin=eip /usr/bin/tcpdump
# Check
getcap /usr/bin/tcpdump
After this, any user of the machine can start a capture. Keep this method for your own machines.Simple HTTP capture
# HTTP traffic (port 80)
sudo tcpdump -nn -i eth0 port 80
# With readable content (requests/responses)
sudo tcpdump -nn -A -i eth0 port 80
HTTPS capture
# HTTPS traffic (port 443)
sudo tcpdump -nn -i eth0 port 443
Note: HTTPS content is encrypted. You will see the connections, but not the data.HTTP and HTTPS combined
# Both ports
sudo tcpdump -nn -i eth0 'port 80 or port 443'
Toward a specific server
# HTTP to example.com
sudo tcpdump -nn -A -i eth0 'host example.com and port 80'
# With DNS resolution (slower)
sudo tcpdump -i eth0 'host example.com and port 80'
Example of content visible with -A (fictitious values)
GET /api/users HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbGci...
Content-Type: application/json
Save for Wireshark
# Save for analysis in Wireshark
sudo tcpdump -nn -i eth0 'port 80 or port 443' -w http_traffic.pcap
Basic filters
# All traffic to/from an IP
sudo tcpdump -nn -i eth0 host 192.168.1.100
# Only the traffic FROM this IP (source)
sudo tcpdump -nn -i eth0 src 192.168.1.100
# Only the traffic TO this IP (destination)
sudo tcpdump -nn -i eth0 dst 192.168.1.100
Filter a whole subnet
# The whole 192.168.1.0/24 subnet
sudo tcpdump -nn -i eth0 net 192.168.1.0/24
# Source from this subnet
sudo tcpdump -nn -i eth0 src net 10.0.0.0/8
Useful combinations
# Traffic between two specific IPs
sudo tcpdump -nn -i eth0 'host 192.168.1.100 and host 192.168.1.200'
# A specific IP on a precise port
sudo tcpdump -nn -i eth0 'host 192.168.1.100 and port 22'
# Exclude an IP (e.g. your SSH)
sudo tcpdump -nn -i eth0 'not host 192.168.1.50'
# Several IPs with OR
sudo tcpdump -nn -i eth0 'host 192.168.1.100 or host 192.168.1.101'
Exclude your own SSH connection
If you are connected over SSH, your SSH traffic "pollutes" the capture:# Exclude port 22
sudo tcpdump -nn -i eth0 'not port 22'
# Exclude your SSH client IP
sudo tcpdump -nn -i eth0 'not host 10.0.0.50'
-w. The file is binary: read it back with tcpdump -r, never with cat.Simple save
# All packets
sudo tcpdump -nn -i eth0 -w capture.pcap
# With a filter (HTTP only)
sudo tcpdump -nn -i eth0 -w http.pcap port 80
# Limit to 1000 packets
sudo tcpdump -nn -i eth0 -c 1000 -w capture.pcap
By default, tcpdump keeps up to 262144 bytes of each packet, which preserves the full content. -s 96 keeps only the first 96 bytes: the capture is lighter, but it loses the end of the packets.Automatic file rotation
-C N closes the file and opens another when it exceeds N million bytes (1,000,000 bytes, not 1,048,576). -G N opens another one every N seconds. -W N does not have the same effect depending on the option that goes with it: with -C, tcpdump limits itself to N files and overwrites the oldest ones; with -G, it stops after N files.# Files of 100 million bytes, at most 5 files, the oldest overwritten
sudo tcpdump -nn -i eth0 -C 100 -W 5 -Z $USER -w capture.pcap
# One file per hour, stop after 24 files
sudo tcpdump -nn -i eth0 -G 3600 -W 24 -Z $USER -w 'capture_%Y%m%d_%H%M.pcap'
Without -W, the files of -C are named capture.pcap, capture.pcap1, capture.pcap2, and so on.Why -Z $USER?
On Debian, Ubuntu and AlmaLinux, tcpdump drops root privileges for the tcpdump user before opening its output files. With -C or -G, opening then fails in your home folder, first file included, with Permission denied. -Z $USER makes the files be written under your account.Reading a file back
# Display the content
tcpdump -nn -r capture.pcap
# Apply a filter to the file
tcpdump -nn -r capture.pcap port 443
# With ASCII content
tcpdump -A -r capture.pcap
pcap format
Wireshark, tshark, Zeek and Suricata can read a pcap file.. means ACK.Flags table
| Display | Name | Meaning |
|---|---|---|
[S] |
SYN | Request to open a connection |
[S.] |
SYN-ACK | Positive answer to a SYN |
[.] |
ACK | Acknowledgment |
[P.] |
PUSH, ACK | Data to deliver right away |
[F.] |
FIN, ACK | Request for a normal close |
[R] or [R.] |
RST | Abrupt close or refusal |
[U] |
URG | Urgent data, rare |
Opening a connection
1. Client to server: [S] connection request
2. Server to client: [S.] the server accepts
3. Client to server: [.] the client confirms
4. Both sides exchange data [P.]
5. One side sends [F.], the other answers [F.]
Reading an exchange
| What you see | Likely meaning |
|---|---|
[S] then [S.] |
The service accepts the connection |
[S] then [R.] right away |
Active refusal by the machine: most often no service listens on this port; a firewall with reject with tcp reset gives the same capture |
[S] repeated with the same seq number, no answer |
The client resends its request: the packets are dropped or do not arrive |
[R] in the middle of an established connection |
One of the two sides abruptly cuts the connection |
Filter by flag
# All SYN alone
sudo tcpdump -nn -i eth0 'tcp[13] == 2'
# All RST
sudo tcpdump -nn -i eth0 'tcp[tcpflags] & tcp-rst != 0'
# All FIN
sudo tcpdump -nn -i eth0 'tcp[tcpflags] & tcp-fin != 0'
CAP_NET_RAW capability, needed to create the capture socket.tcpdump: any: You don't have permission to perform this capture on that device
(Attempt to create packet socket failed - CAP_NET_RAW may be required)
Solution 1: use sudo
sudo tcpdump -i eth0
Solution 2: give the binary the capabilities
To avoidsudo on every capture:sudo setcap cap_net_raw,cap_net_admin=eip /usr/bin/tcpdump
getcap /usr/bin/tcpdump
tcpdump -i eth0
After this, any user of the machine can capture. Keep this method for your own machines.Solution 3: read back an existing file
Reading back a pcap file needs no privileges:tcpdump -r capture.pcap
Other case: Permission denied on an output file
If the message names a file (tcpdump: /home/vous/rot.pcap0: Permission denied), tcpdump dropped its root privileges for the tcpdump user and cannot write in this folder. Add -Z $USER to write under your account.-i matches no interface on the machine.tcpdump: absent0: No such device exists
(No such device exists)
Find the right name
# Interfaces that tcpdump can open
tcpdump -D
# Interfaces of the machine and their addresses
ip link show
ip addr show
tcpdump -D numbers the interfaces; copy the name, not the sequence number. On recent distributions, a network card has a name like enp5s0, not eth0: the name depends on the machine.Capture on all interfaces
sudo tcpdump -i any
any groups all the interfaces and adds a column that names the interface of each packet. To find out which interface the traffic to an address leaves by, use ip route get ADDRESS: the word after dev gives the interface.Exclude the SSH port
# Simple method: exclude port 22
sudo tcpdump -nn -i eth0 'not port 22'
Exclude your client IP
# Find your IP (the one you connect from)
who am i
# the line ends with the workstation address, fictitious here: (10.0.0.50)
# Exclude this IP
sudo tcpdump -nn -i eth0 'not host 10.0.0.50'
More precise combination
# Exclude only YOUR SSH connection, in both directions
sudo tcpdump -nn -i eth0 'not (host 10.0.0.50 and port 22)'
With other filters
# Capture HTTP while excluding your SSH
sudo tcpdump -nn -i eth0 'port 80 and not port 22'
# All the traffic of a server, except your SSH
sudo tcpdump -nn -i eth0 'host 192.168.1.100 and not (host 10.0.0.50 and port 22)'
Alternative: capture to a file
# Capture everything, filter afterwards
sudo tcpdump -nn -i eth0 -w capture.pcap
# Read back while excluding SSH
tcpdump -nn -r capture.pcap 'not port 22'