Log inSign up

Packet capture on your server with tcpdump

Install the tcpdump package, find the public interface with ip route show default, and run sudo tcpdump -i eth0 -nn port 443 to watch matching packets scroll past. Add -w file.pcap to save a capture for later study, and always keep your own SSH session out of the output with and not port 22.

Topic: Networking and diagnostics · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

When is tcpdump the right tool?

Reach for tcpdump when logs and status commands disagree with what you expect and you need proof of what really crossed the wire. Typical questions it settles: did the client packet arrive at all, did the server answer, was the answer a reset, and which address did a DNS query really go to. Higher level tools such as ss show sockets and mtr shows the path, but only a capture shows individual packets with their flags and timing.

Captures need root rights because they put the interface in a mode that copies traffic to user space. Log in as your sudo user (see the first SSH session tutorial) and keep in mind that a capture can contain passwords from unencrypted protocols, so treat saved files as sensitive.

How do you install tcpdump and check the version?

The package is in the base repositories of every supported image. On Ubuntu 24.04, Ubuntu 22.04, Debian 12 and Debian 13:

sudo apt update
sudo apt install -y tcpdump
tcpdump --version

On Rocky Linux 9 and AlmaLinux 9:

sudo dnf install -y tcpdump
tcpdump --version

The version line also names the libpcap release underneath. Next, list the interfaces tcpdump can open and find the one that carries the default route:

sudo tcpdump -D
ip route show default
ip -6 route show default

The name after dev is your public interface. This guide uses eth0; replace it with ens3 or whatever your server reports.

Which options do you use every time?

A handful of flags turn the raw stream into something readable. -n stops reverse lookups of addresses and -nn also keeps port numbers as numbers, which avoids slow DNS queries that would themselves show up in the capture. -c 50 stops after fifty packets, -v adds TTL and length detail, and -tttt prints a full date and time on every line.

sudo tcpdump -i eth0 -nn -c 50 not port 22
sudo tcpdump -i any -nn -tttt -c 20 icmp or icmp6

The any pseudo interface listens everywhere at once, including lo and tunnels such as wg0, which is handy when you are not sure which path a packet takes. For payload inspection, -A prints ASCII and -X prints hex plus ASCII. Modern builds already capture whole packets, so the old habit of adding -s 0 is no longer needed.

How do you write capture filters?

Filters use the Berkeley packet filter language, and the kernel applies them before packets reach tcpdump, so a narrow filter keeps the output short and the CPU cost low. Combine primitives with and, or and not, and quote any expression that contains brackets.

# one remote address, both directions
sudo tcpdump -i eth0 -nn host 203.0.113.50

# web traffic only, excluding your own SSH session
sudo tcpdump -i eth0 -nn '(port 80 or port 443) and not port 22'

# DNS questions and answers
sudo tcpdump -i eth0 -nn udp port 53

# only new TCP connection attempts (SYN without ACK)
sudo tcpdump -i eth0 -nn 'tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn'

# IPv6 traffic only
sudo tcpdump -i eth0 -nn ip6

src and dst narrow a host or port to one direction, and net 198.51.100.0/24 matches a whole range. If nothing appears, widen the filter one step at a time rather than guessing.

How do you save captures to a file and rotate them?

Use -w to write raw packets in pcap format instead of printing them; the file keeps every detail and can be filtered again later. A size or time limit stops a forgotten capture from filling the disk.

sudo mkdir -p /var/tmp/caps
sudo tcpdump -i eth0 -nn -w /var/tmp/caps/web.pcap -c 5000 port 443

# ring buffer: ten files of 50 MB each, oldest overwritten
sudo tcpdump -i eth0 -nn -C 50 -W 10 -w /var/tmp/caps/ring.pcap not port 22

On many builds tcpdump drops root rights after it starts and switches to an unprivileged user called tcpdump. The first file is opened before that switch, but rotated files are created afterwards, so a rotation into a root owned folder fails with permission denied. Either give the folder to that user with sudo chown tcpdump: /var/tmp/caps or keep root with -Z root for that one run. Read a file back with the same filter syntax:

sudo tcpdump -nn -r /var/tmp/caps/web.pcap 'tcp[tcpflags] & tcp-rst != 0'
ls -lh /var/tmp/caps

To study a capture in a graphical analyser on your laptop, copy it down with scp deploy@198.51.100.24:/var/tmp/caps/web.pcap . after changing its owner to deploy.

How do you run a long capture safely?

Run long captures inside a terminal multiplexer so a dropped SSH connection does not end them; the tmux guide covers that. Combine the ring buffer from above with a narrow filter and check free space first with df -h /var/tmp. On a small plan, capturing every packet of a busy web server for hours can write gigabytes, while a filter on one port and one client stays tiny.

For a time based cut, -G 3600 starts a new file every hour when the file name contains strftime fields:

sudo tcpdump -i eth0 -nn -G 3600 -W 24 -w '/var/tmp/caps/dns-%Y%m%d-%H.pcap' udp port 53

With -W 24 tcpdump exits after twenty four files, which gives you one day of DNS traffic and then stops on its own.

How do you read real problems from the output?

Each line shows a timestamp, the source and destination with ports, the TCP flags and the length. Flags appear as [S] for SYN, [S.] for SYN with ACK, [.] for a plain ACK, [P.] for data pushed, [F.] for a close and [R] for a reset. A few patterns are worth knowing:

  • Repeated [S] from a client with no reply means the packet reaches the server but nothing answers, which usually points at a firewall drop. Compare with the firewall rules tutorial.
  • [S] answered by [R.] means the port is closed: no program is listening on it.
  • No packets at all while the client insists it is sending means the traffic is lost before it reaches the server, so test the path with mtr.
  • A DNS query with no answer line points at the resolver, which the dig guide helps confirm.

To prove the tool works end to end, start a capture for ICMP in one window and ping the server from your computer in another; each echo request and reply should appear as a pair.

How do you keep tcpdump up to date and clean up?

tcpdump parses untrusted input, so take its updates with the normal system upgrades (sudo apt upgrade or sudo dnf upgrade). Captures are not removed automatically. When an investigation ends, delete the folder with sudo rm -r /var/tmp/caps so old traffic, which may include private data, does not linger on disk or in your backups.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-2short, filtered captures1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months
A2-4longer ring buffer captures on busy hosts2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months

Questions about Capture packets with tcpdump

Why does my capture show nothing?

Check the interface name with sudo tcpdump -D, try -i any, and loosen the filter. Traffic inside a WireGuard tunnel appears on wg0, not on the public interface, where you only see encrypted UDP.

Can I capture traffic to Docker containers?

Yes. Container traffic passes through the docker0 bridge or a per network bridge, so capture on that interface or on any. Published ports also appear on the public interface before translation.

Does tcpdump slow the server down?

A filtered capture costs very little. Printing every packet of a busy link to the terminal costs more, mostly through the terminal itself, so write to a file with -w when traffic is heavy.

Can tcpdump decrypt HTTPS?

No. It sees TLS records but not their content. For application debugging use the server logs or request tools such as curl with -v.

Is it allowed to capture on my VPS?

Capturing your own server traffic for diagnosis is normal administration. You only see packets addressed to or sent by your server, not those of other customers.