Sometimes the logs and the summary tools disagree, and you need to see what is really on the wire. Packet analysis means capturing traffic and reading it. The common command line tool is tcpdump. Wireshark is a graphical tool that reads the same capture files.
Capture only on networks and systems you are allowed to inspect. Packets can contain private data.
First capture
Capturing normally needs root. List the interfaces, then capture a few packets:
sudo tcpdump -D
sudo tcpdump -n -i eth0 -c 3-i picks the interface (yours may be named differently), -c 3 stops after three packets and -n prints addresses and ports as numbers.
Reading a line
12:30:00.000001 IP 192.0.2.10.51000 > 198.51.100.5.443: Flags [S], seq 100, win 64240, length 0
12:30:00.020001 IP 198.51.100.5.443 > 192.0.2.10.51000: Flags [S.], seq 500, ack 101, win 65160, length 0
12:30:00.020101 IP 192.0.2.10.51000 > 198.51.100.5.443: Flags [.], ack 1, win 502, length 0Each line has a time, the source address.port, the destination and details. TCP flags are abbreviated:
| Shown | Meaning |
|-------|---------|
| S | SYN |
| . | ACK |
| F | FIN |
| R | RST |
| P | PSH |
The three lines above are the TCP handshake: SYN, SYN-ACK, ACK. Real output may include TCP options too. After the handshake, tcpdump shows relative sequence numbers by default.
Patterns help. Repeated [S] with no answer suggests the packets or replies are being lost or filtered. An immediate [R.] reply to a SYN usually means nothing is listening on that port, or a firewall is rejecting it.
Filters
A filter expression keeps only what you care about:
sudo tcpdump -n -i any port 53
sudo tcpdump -n -i any host 192.0.2.1 and tcp port 80You can combine host, net, port, tcp, udp and icmp with and, or and not. When capturing over SSH, not port 22 keeps your own session out of the results (assuming SSH uses port 22). -i any captures on all interfaces on Linux.
Save and read later
sudo tcpdump -i any -c 100 -w web.pcap port 443
tcpdump -n -r web.pcap-w writes raw packets to a file instead of printing them, and -r reads them back. Open the file in Wireshark to follow streams and expand each protocol layer. HTTPS contents stay encrypted, though you can still see addresses, ports and timing.
-A prints packet data as text, and -X shows hex and text. Both are useful for unencrypted protocols.
Try it
Start sudo tcpdump -n -i any -c 10 port 53, then in another terminal run getent hosts example.com. Look for the query and the answer. A cached name may not leave the machine as you expect, and some systems send DNS over a local address or an encrypted connection.
Check your understanding
Packet Analysis
All 5 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.