Lab: tcpdump on a remote server, analysis in Wireshark on your desk
Capture on a lab "server" with a filter, a snap length and a ring buffer, measure what each setting really keeps, stream the capture over the network to a "desk" machine, and analyse it there with tshark, the command-line Wireshark.
Contentsand lesson path
On this page
- Goal and setup
- Step 0: Open the lab shell
- Step 1: Build the desk and the server
- Step 2: Measure what -s 96 keeps
- Step 3: How long must the snap length be for a TLS server name?
- Step 4: A ring buffer that wraps, with no drops
- Step 5: Stream the capture to your desk, and break it
- Step 6: Analyse the ring on your desk
- Step 7: Clean up
- Check your understanding
- Apply it
- Review prompt
Lessons of Packet Capture & Analysis
- Layers that matter in practice: OSI vs TCP/IP and encapsulation Concept · 20 min
- Lab: one web request, every layer, in your first capture Lab · 25 min
- Capturing well: capture filters, ring buffers and where to tap Concept · 20 min
- Lab: tcpdump on a remote server, analysis in Wireshark on your desk Lab · 25 min
- Display filters, conversations and stream reassembly Worked example · 25 min
- Check: answer questions from three capture files Knowledge check · 8 questions
What you will learn
- Capture traffic with tcpdump and Wireshark using capture filters, and choose a capture point (host, SPAN port, TAP) for a given problem.
- Capture responsibly: obtain authorization, limit sensitive data, and use ring buffers and snap lengths to control file size.
Prerequisites: Networking Basics
01. Goal and setup
In real work, tcpdump runs on a remote server, usually over SSH, and a file or live stream travels to your workstation for Wireshark. This lab rebuilds that workflow and measures the settings from the previous lesson: what a snap length keeps, how a ring buffer bounds disk use, and why a live stream must not capture itself.
Two namespaces play the machines. server (192.0.2.20) runs a web and a TLS server, and every capture runs there. desk (192.0.2.10) is your workstation. nsenter -t $S_PID -n plays the part of ssh server, and the stream in Step 5 crosses the lab link in a real TCP connection made with socat, as an SSH session would.
You need the tools from the first lab, plus openssl, socat and Wireshark's command-line tools (tshark, mergecap, capinfos), ideally 4.0 or later, which the next lesson needs. On Ubuntu 24.04 apt install tshark gives 4.2; on Ubuntu 22.04 (3.6) add ppa:wireshark-dev/stable first; the wireshark.org installers include tshark. tshark shares the window's dissectors and filters; each step names the matching menu. Recorded with tcpdump 4.99.1, tshark 4.6.9, socat 1.7.4.1, OpenSSL 3.0.2, curl 7.81.0.
02. Step 0: Open the lab shell
Open a lab shell as in Step 0 of the first lab (unshare -c -n --keep-caps bash, or sudo unshare -n bash on a VM you own). LC_ALL=C makes tshark print numbers as shown here.
export LC_ALL=C
ip -br link
tshark --version | head -n 1
socat -V | sed -n 2p
openssl versionlo DOWN 00:00:00:00:00:00 <LOOPBACK>
TShark (Wireshark) 4.6.9.
socat version 1.7.4.1 on Mar 25 2022 09:51:32
OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)03. Step 1: Build the desk and the server
The block creates a 2,049-byte file and a 2.5 MB file in www/, a throwaway TLS certificate for shop.example and the two namespaces, then starts Python's web server on port 8080 (serving only www/, so the key is not downloadable) and openssl s_server on port 8443.
mkdir -p ~/pca-lab04/www && cd ~/pca-lab04
python3 -c 'print("X" * 2048)' > www/payload.txt
head -c 2500000 /dev/zero | tr '\0' 'Y' > www/big.bin
openssl req -x509 -newkey rsa:2048 -nodes -days 2 -subj "/CN=shop.example" \
-keyout lab.key -out lab.crt 2> /dev/null
unshare -n sleep infinity & D_PID=$!
unshare -n sleep infinity & S_PID=$!
sleep 0.5
ip link add d-eth0 address 02:00:00:00:03:10 type veth peer name s-eth0 \
address 02:00:00:00:03:20
ip link set d-eth0 netns $D_PID
ip link set s-eth0 netns $S_PID
nsenter -t $D_PID -n sh -c "ip link set lo up && \
ip addr add 192.0.2.10/24 dev d-eth0 && ip link set d-eth0 up"
nsenter -t $S_PID -n sh -c "ip link set lo up && \
ip addr add 192.0.2.20/24 dev s-eth0 && ip link set s-eth0 up"
nsenter -t $S_PID -n python3 -m http.server 8080 --bind 192.0.2.20 \
--directory www > http.log 2>&1 & HTTP_PID=$!
nsenter -t $S_PID -n openssl s_server -quiet -www -accept 192.0.2.20:8443 \
-cert lab.crt -key lab.key > tls.log 2>&1 & TLS_PID=$!
sleep 1
nsenter -t $S_PID -n ss -tlnState Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 5 192.0.2.20:8080 0.0.0.0:*
LISTEN 0 4096 192.0.2.20:8443 0.0.0.0:*04. Step 2: Measure what -s 96 keeps
Predict before running. Each data segment here has 66 bytes of headers: 14 Ethernet, 20 IPv4 and 32 TCP with timestamps. With -s 96, how many bytes of the 2,049-byte response body will still be in the file?
Two captures of one request run side by side, whole and cut at 96 bytes. tshark then lists each packet's frame.len (length on the wire), frame.cap_len (bytes saved) and tcp.len (TCP data carried).
nsenter -t $S_PID -n tcpdump -Z root --immediate-mode -U -i s-eth0 -s 0 \
-w full.pcap "tcp port 8080" 2> full.log &
T1=$!
nsenter -t $S_PID -n tcpdump -Z root --immediate-mode -U -i s-eth0 -s 96 \
-w snap96.pcap "tcp port 8080" 2> snap96.log &
T2=$!
sleep 1
nsenter -t $D_PID -n curl -sS -o /dev/null http://192.0.2.20:8080/payload.txt
sleep 1
kill -INT $T1 $T2; wait $T1 $T2
stat -c '%s %n' full.pcap snap96.pcap
tshark -r snap96.pcap -T fields -e frame.number -e frame.len -e frame.cap_len \
-e tcp.len3352 full.pcap
1114 snap96.pcap
1 74 74 0
2 74 74 0
3 66 66 0
4 156 96 90
5 66 66 0
6 255 96 189
7 66 66 0
8 2115 96 2049
9 66 66 0
10 66 66 0
11 66 66 0
12 66 66 0tcpdump -r snap96.pcap -nn -A "src port 8080 and greater 1000"reading from file snap96.pcap, link-type EN10MB (Ethernet), snapshot length 96
10:53:15.208768 IP 192.0.2.20.8080 > 192.0.2.10.59682: Flags [P.], seq
4170406348:4170408397, ack 1883435786, win 509, options [nop,nop,TS val 2000429565 ecr
1200948490], length 2049: HTTP
E..5.0@.@./t.......
..."..Y.pB.
.....F.....
w<!.G..
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXOn disk the snap length worked: 1,114 bytes instead of 3,352, with every header intact for TCP analysis. But it did not remove the content. Frame 8, the body, kept 96 − 66 = 30 bytes of data, and -A prints them: thirty X characters. Frames 4 and 6 kept the first 30 bytes of the HTTP headers. Real traffic could leak a cookie that way: a snap length reduces exposure, it does not remove personal data.
Frame 8 is 2,115 bytes long on a 1,500-byte link: segmentation offload again. The server's stack handed the virtual card one 2,049-byte segment, and the capture saw it before any split.
05. Step 3: How long must the snap length be for a TLS server name?
A common goal is to keep the Server Name Indication (SNI), the host name a TLS client sends in its first message, the ClientHello, without storing the encrypted data after it. Before the cipher list starts there are 66 header bytes, a 5-byte record header, a 4-byte handshake header, a 2-byte version, a 32-byte random, a 1-byte length and usually a 32-byte session ID: 142 bytes; the extensions, SNI among them, come later. Predict: is -s 128 enough? The block captures one HTTPS request at 128 bytes and in full, then asks tshark for the server name and, from its PDML output, the name's byte position in the frame.
nsenter -t $S_PID -n tcpdump -Z root --immediate-mode -U -i s-eth0 -s 128 \
-w tls128.pcap "tcp port 8443" 2> tls128.log &
T3=$!
nsenter -t $S_PID -n tcpdump -Z root --immediate-mode -U -i s-eth0 -s 0 \
-w tlsfull.pcap "tcp port 8443" 2> tlsfull.log &
T4=$!
sleep 1
nsenter -t $D_PID -n curl -sSk -o /dev/null \
--resolve shop.example:8443:192.0.2.20 https://shop.example:8443/
sleep 1
kill -INT $T3 $T4; wait $T3 $T4
for f in tls128.pcap tlsfull.pcap; do
echo "$f: $(tshark -r $f -Y "tls.handshake.type == 1" -T fields \
-e frame.len -e frame.cap_len -e tls.handshake.extensions_server_name)"
done
tshark -r tlsfull.pcap -Y "tls.handshake.type == 1" -T pdml \
| grep -o 'showname="Server Name: [^"]*" size="[0-9]*" pos="[0-9]*"'tls128.pcap: 583 128
tlsfull.pcap: 583 583 shop.example
showname="Server Name: shop.example" size="12" pos="219"The 128-byte file has the ClientHello but no server name. In the full file the name starts at byte 219 of 583, so this client (curl with OpenSSL 3.0) needs a snap length of at least 231. Browsers send more cipher suites and extensions, and Chrome shuffles its extension order on every connection (Fastly's analysis), so the name can land well past 256 bytes. If the server name matters, capture handshakes whole and measure on your own traffic. Clients using Encrypted Client Hello (RFC 9849) show only a public front-end name and encrypt the real one.
06. Step 4: A ring buffer that wraps, with no drops
Now capture two 2.5 MB downloads into a ring of three files of about 1 MB (-C 1 -W 3). -B 16384 gives the kernel a 16 MiB buffer and --limit-rate 5M slows curl, so tcpdump keeps up. capinfos prints each file's packet count and first and last packet times.
Predict before running. About 5 MB will flow into a ring that holds about 3 MB. Which file will hold the oldest packets when the capture stops?
nsenter -t $S_PID -n tcpdump -Z root --immediate-mode -i s-eth0 -B 16384 \
-C 1 -W 3 -w ring.pcap "tcp port 8080" 2> ring.log &
T5=$!
sleep 1
for i in 1 2; do
nsenter -t $D_PID -n curl -sS -o /dev/null --limit-rate 5M \
http://192.0.2.20:8080/big.bin
done
sleep 1
kill -INT $T5; wait $T5
tail -n 3 ring.log
stat -c '%s %n' ring.pcap*
capinfos -T -r -c -a -e ring.pcap*262 packets captured
262 packets received by filter
0 packets dropped by kernel
1019336 ring.pcap0
865044 ring.pcap1
1034310 ring.pcap2
ring.pcap0 52 2026-11-27 10:53:20.668048 2026-11-27 10:53:20.668632
ring.pcap1 28 2026-11-27 10:53:20.668643 2026-11-27 10:53:21.107443
ring.pcap2 63 2026-11-27 10:53:20.180556 2026-11-27 10:53:20.668033First check the counters: 0 packets dropped by kernel, so no holes. On the recording machine, the same capture without -B and --limit-rate dropped about two thirds of the packets (around 230 of 340) in three runs out of three. Then read the times: tcpdump filled files 0, 1 and 2, wrapped, overwrote 0 and was part-way through 1 when it stopped. So ring.pcap2, the only file not yet overwritten, holds the oldest packets; after a wrap, file numbers say nothing about age. Full files end slightly over 1,000,000 bytes because the size is checked before each write, and together they hold under 3 MB of the 5 MB that passed: the oldest 2 MB are gone, by design.
07. Step 5: Stream the capture to your desk, and break it
For quick triage you can stream instead. In production: ssh server "sudo -n tcpdump -U -w - 'filter'" | wireshark -k -i -, where -w - writes pcap data to standard output, SSH carries it and Wireshark reads it from standard input. Here the desk saves the stream with socat, and the server pipes tcpdump into socat across the lab link for five seconds. The filter's not port 9000 excludes the stream's own connection.
nsenter -t $D_PID -n socat -u TCP-LISTEN:9000,bind=192.0.2.10 \
OPEN:live.pcap,creat,trunc & RX=$!
sleep 0.5
nsenter -t $S_PID -n sh -c "timeout 5 tcpdump -Z root --immediate-mode -U \
-i s-eth0 -w - 'tcp port 8080 and not port 9000' 2> stream.log \
| socat -u STDIN TCP:192.0.2.10:9000" & TX=$!
sleep 1
for i in 1 2 3; do
nsenter -t $D_PID -n curl -sS -o /dev/null http://192.0.2.20:8080/payload.txt
done
wait $TX $RX
tshark -r live.pcap -q -z conv,tcp================================================================================
TCP Conversations
Filter:<No Filter>
192.0.2.10:48718 <-> 192.0.2.20:8080 frames <- 5, -> 7, total 12
bytes <- 2576 bytes, -> 560 bytes, total 3136 bytes; start 0.000000000, dur 0.0012
192.0.2.10:48724 <-> 192.0.2.20:8080 frames <- 5, -> 7, total 12
bytes <- 2576 bytes, -> 560 bytes, total 3136 bytes; start 0.016329000, dur 0.0011
192.0.2.10:48732 <-> 192.0.2.20:8080 frames <- 5, -> 7, total 12
bytes <- 2576 bytes, -> 560 bytes, total 3136 bytes; start 0.033423000, dur 0.0013
================================================================================This is tshark's form of Wireshark's Statistics > Conversations window, TCP tab: one row per connection, keyed by addresses and ports. <- counts what the right-hand side sent, here the server's 2,576 bytes, and -> the client's requests and acknowledgements.
Predict, then break it. What if the filter is just tcp, so the stream's own packets on port 9001 match? The next block runs that for two seconds with no requests, then deletes the file.
nsenter -t $D_PID -n socat -u TCP-LISTEN:9001,bind=192.0.2.10 \
OPEN:loop.pcap,creat,trunc & RX=$!
sleep 0.5
nsenter -t $S_PID -n sh -c "timeout 2 tcpdump -Z root --immediate-mode -U \
-i s-eth0 -w - tcp 2> loop.log | socat -u STDIN TCP:192.0.2.10:9001" & TX=$!
wait $TX $RX
stat -c '%s %n' live.pcap loop.pcap
tail -n 3 loop.log
rm loop.pcap10008 live.pcap
121655820 loop.pcap
11559 packets captured
11575 packets received by filter
0 packets dropped by kernel80 to 120 MB in two seconds in the recorded runs, with no traffic: each captured packet goes into the stream, the stream's segments cross s-eth0, tcpdump captures them too, and so on. An SSH stream that captures port 22 on its own interface does the same and can saturate the link. Excluding the transport's port, or a filter that matches only the traffic you need, breaks the loop.
08. Step 6: Analyse the ring on your desk
From a real server you would copy the ring files with scp server:/var/tmp/ring.pcap* .; the namespaces here share one file system, so they are already in place. mergecap combines them, ordering packets by timestamp by default (-a would concatenate in argument order). Then list the conversations and the SYN packets (display filter tcp.flags.syn == 1).
mergecap -w merged.pcapng ring.pcap*
capinfos -c -u merged.pcapng
tshark -r merged.pcapng -q -z conv,tcp
tshark -r merged.pcapng -Y "tcp.flags.syn == 1" -T fields -e frame.number \
-e ip.src -e tcp.srcport -e tcp.dstportFile name: merged.pcapng
Number of packets: 143
Capture duration: 0.926887 seconds
================================================================================
TCP Conversations
Filter:<No Filter>
192.0.2.10:59700 <-> 192.0.2.20:8080 frames <- 75, -> 54, total 129
bytes <- 2506 kB, -> 3694 bytes, total 2510 kB; start 0.454683000, dur 0.4722
192.0.2.20:8080 <-> 192.0.2.10:59686 frames <- 3, -> 11, total 14
bytes <- 210 bytes, -> 405 kB, total 405 kB; start 0.000000000, dur 0.4363
================================================================================
15 192.0.2.10 59700 8080
16 192.0.2.20 8080 59700The second download is complete, with its handshake (frames 15 and 16 in the output shown). The first appears only from the middle: 405 kB of its 2.5 MB and no SYN; the left-hand address is whichever side sent the first packet seen, here the server. A wrapped ring often loses the start of long connections. In the Wireshark window, open merged.pcapng and use Statistics > Conversations.
09. Step 7: Clean up
kill $HTTP_PID $TLS_PID $D_PID $S_PID
sleep 1
cd ~ && rm -rf ~/pca-lab04
test -d ~/pca-lab04 || echo "lab directory removed"lab directory removed10. Check your understanding
11. Apply it
Do the skill practice on a machine you may capture on, such as your own VM. Start tcpdump with the filter port 53 or port 443, a ring buffer (-C 1 -W 4) and --immediate-mode, then run dig example.com and curl -sS -o /dev/null https://example.com/. In Wireshark or tshark, citing frame numbers, identify the DNS query and answer, the TCP handshake, the server name in the ClientHello and the bytes each layer adds to one data frame. Would your SNI survive -s 256?
Next, the worked example on display filters and streams finds one failing request in a capture that holds many.
12. Review prompt
Explain, without looking back, what -s 96 keeps of a data segment and why that is not the same as "headers only"; why the oldest packets of a wrapped ring are not in the first file; why a live capture streamed over a network connection can capture itself; and what the drop counter tells you about a file before you analyse it.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.