tcpdump Basics: Capture and Read Network Traffic From the Command Line


Wireshark is the famous way to watch network traffic — but it needs a graphical screen. When you’re SSH’d into a server, debugging a container, or working on a headless machine, there’s no GUI to open. That’s where tcpdump comes in: the command-line packet sniffer that has shipped with nearly every Linux and Unix system for decades. If you can type, you can capture.

What tcpdump Actually Does

Every packet your computer sends or receives passes through a network interface. tcpdump asks the operating system for a copy of those packets and prints them in real time. It’s the same engine underneath Wireshark — in fact, Wireshark can open tcpdump’s capture files. Learn tcpdump and you learn packet analysis anywhere.

Your First Capture

sudo tcpdump -i eth0 -c 10

This listens on interface eth0 and shows 10 packets, then stops. Don’t know your interface name? Run ip a and look for the one with your IP address. A few useful flags to learn first:

  • -n — don’t resolve hostnames. Faster, cleaner output, and it keeps your capture from leaking DNS lookups.
  • -c 100 — stop after 100 packets so your screen doesn’t scroll forever.
  • -v — verbose: shows more detail per packet, like TTL and packet length.

Filtering: The Skill That Matters

Raw capture is a firehose. Filters are how you find the one packet you care about. tcpdump’s filter language (called BPF) is worth learning:

sudo tcpdump -i eth0 -n host 192.168.1.10
sudo tcpdump -i eth0 -n port 443
sudo tcpdump -i eth0 -n 'tcp port 80 and host example.com'
sudo tcpdump -i eth0 -n 'icmp'
sudo tcpdump -i eth0 -n 'not port 22'

Combine with -c so you capture a sample, not an endless stream. Start broad, then narrow the filter as you learn what normal looks like on your network.

Save It, Read It Later

sudo tcpdump -i eth0 -n -w capture.pcap port 443
tcpdump -r capture.pcap -n | head -20

The -w flag writes raw packets to a file instead of printing them. Later, -r reads the file back — or you open it in Wireshark on your laptop for the full graphical view. This is the standard workflow: capture quietly on the server, analyze comfortably at your desk.

Reading the Output

A typical line looks like this:

10:24:31.512 IP 192.168.1.10.443 > 192.168.1.5.51234: Flags [P.], length 120

Read it left to right: timestamp, source IP and port, destination IP and port, TCP flags, packet length. [S] is a connection starting (SYN), [P.] is data being pushed, [F] is a connection closing. Watch for a flood of [S] packets with no replies — that’s the signature of a port scan hitting your machine.

Three Rules Before You Start

  • Only capture your own network. Capturing traffic on networks you don’t own or have permission to monitor is illegal in most countries. Your home lab and your own servers are the place to learn.
  • Capture files contain sensitive data. A .pcap can hold passwords, cookies and private messages. Treat it like a secret: keep it local, delete it when done, never upload it anywhere.
  • Don’t run tcpdump as root longer than needed. Use sudo for the capture, then read the file as a normal user.

tcpdump is one of those tools that looks intimidating for exactly one afternoon, then becomes muscle memory. Pair it with the Wireshark basics we covered earlier and you can follow a packet from your laptop to the internet and back — from any terminal on earth.

Comments

Popular Posts