Using tcpdump to Capture Network Traffic on VPS
tcpdump is an indispensable command-line tool for sysadmins. It captures and analyzes network packets on Linux — whether you're debugging dropped connections, verifying that firewall rules are working, or investigating unexpected traffic on your VPS. This guide covers tcpdump from basics to advanced BPF filters with real-world examples.
Why tcpdump Matters on a VPS
When an application misbehaves and logs don't tell the full story, tcpdump lets you see exactly what's happening at the network layer. You can confirm whether packets are actually leaving the server, whether the remote end is responding, and whether TCP handshakes are completing. A common scenario: Postfix failing to deliver email because the ISP silently blocks outbound port 25 — impossible to diagnose without watching the actual packets.
Installing tcpdump
Most Linux distributions ship tcpdump or have it in their package repository:
# Debian / Ubuntu
apt install tcpdump -y
# CentOS / RHEL / Rocky / AlmaLinux
dnf install tcpdump -y
# Verify installation
tcpdump --version
Basic Commands
tcpdump requires root privileges or sudo to access network interfaces:
# List all available network interfaces
tcpdump -D
# Capture 10 packets on eth0 then stop
tcpdump -i eth0 -c 10
# Capture on all interfaces
tcpdump -i any -c 20
# Show numeric IP addresses (skip reverse DNS — much faster)
tcpdump -i eth0 -n -c 20
# Verbose output with full timestamps
tcpdump -i eth0 -tttt -vv -c 10
-n to disable reverse DNS lookups. It dramatically speeds up tcpdump, especially on a busy VPS with heavy traffic.Filtering Traffic with BPF Expressions
BPF (Berkeley Packet Filter) is tcpdump's filter language. Good filters reduce noise and help you focus on relevant packets:
Filter by Host
# All traffic to/from a specific IP
tcpdump -i eth0 -n host 203.0.113.10
# Traffic originating from a source IP
tcpdump -i eth0 -n src 203.0.113.10
# Traffic destined for a specific IP
tcpdump -i eth0 -n dst 203.0.113.10
# Exclude your own SSH session to reduce noise
tcpdump -i eth0 -n not host YOUR_IP
Filter by Port
# HTTP traffic (port 80)
tcpdump -i eth0 -n port 80
# HTTPS traffic (port 443)
tcpdump -i eth0 -n port 443
# Either port 80 or 443
tcpdump -i eth0 -n 'port 80 or port 443'
# Inbound MySQL connections (should only come from localhost)
tcpdump -i eth0 -n dst port 3306
# SSH connections excluding your current session
tcpdump -i eth0 -n 'port 22 and not src host MY_IP'
Filter by Protocol
# TCP traffic only
tcpdump -i eth0 -n tcp
# UDP traffic only
tcpdump -i eth0 -n udp
# ICMP (ping) traffic
tcpdump -i eth0 -n icmp
# Only TCP SYN packets (new connection attempts)
tcpdump -i eth0 -n 'tcp[tcpflags] & tcp-syn != 0'
Saving Packets to a File
Saving captures as .pcap files lets you analyze them later in Wireshark on your local machine for deep inspection:
# Save all packets to a pcap file
tcpdump -i eth0 -n -w /tmp/capture.pcap
# Capture only HTTP traffic for 60 seconds
timeout 60 tcpdump -i eth0 -n port 80 -w /tmp/http-traffic.pcap
# Read back a saved pcap file
tcpdump -r /tmp/capture.pcap -n
# Read with a filter applied
tcpdump -r /tmp/capture.pcap -n 'host 203.0.113.10'
# Rotate files: max 10 MB each, keep 5 files
tcpdump -i eth0 -n -w /tmp/cap-%Y%m%d-%H%M%S.pcap -C 10 -W 5
Inspecting Packet Payloads
For plaintext protocols like HTTP, you can read the actual content being transmitted:
# Show payload as ASCII (useful for HTTP)
tcpdump -i eth0 -n -A port 80 -c 20
# Show payload as hex + ASCII
tcpdump -i eth0 -n -X port 80 -c 10
# Capture HTTP GET/POST requests specifically
tcpdump -i eth0 -n -A 'tcp port 80 and (tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420 or tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x504f5354)'
# Watch DNS queries in real time
tcpdump -i eth0 -n port 53 -vv -c 20
Real-World Use Cases on a VPS
Verify Nginx Is Receiving Requests
# Watch all incoming connections to web ports
tcpdump -i eth0 -n 'port 80 or port 443' -c 50
# Count new connection attempts per second (DDoS indicator)
tcpdump -i eth0 -n 'tcp[tcpflags] & tcp-syn != 0' -l | \
awk '{print $1}' | cut -d. -f1 | uniq -c
Debug MySQL Connectivity
# Confirm the application is actually reaching MySQL
tcpdump -i lo -n port 3306 -A -c 20
# Alert if MySQL receives connections from outside (it shouldn't)
tcpdump -i eth0 -n dst port 3306
Monitor DNS Resolution
# See every DNS query your VPS sends out
tcpdump -i eth0 -n port 53
# Record 30 seconds then analyze
timeout 30 tcpdump -i eth0 -n port 53 -w /tmp/dns.pcap
tcpdump -r /tmp/dns.pcap -n -vv
Detect Port Scanning
# Identify IPs sending excessive SYN packets (port scan signature)
tcpdump -i eth0 -n 'tcp[tcpflags] = tcp-syn' -c 100 | \
awk '{print $3}' | cut -d. -f1-4 | sort | uniq -c | sort -rn | head
Verify tcpdump Is Working Correctly
# Test by capturing ICMP while pinging from another machine
tcpdump -i eth0 -n icmp
# From a second terminal, ping the VPS
ping YOUR_VPS_IP
# Expected output:
# 14:23:01.123456 IP 1.2.3.4 > 10.0.0.1: ICMP echo request, id 1234, seq 1
# 14:23:01.123789 IP 10.0.0.1 > 1.2.3.4: ICMP echo reply, id 1234, seq 1
# Check packet drop statistics
tcpdump -i eth0 -n -c 100 2>&1 | tail -3
# Good output:
# 100 packets captured
# 100 packets received by filter
# 0 packets dropped by kernel
# Increase the kernel buffer if drops occur
tcpdump -i eth0 -n -B 65536 -c 100
packets dropped by kernel means the capture buffer is too small for the traffic rate. Add -B 65536 (or higher) to allocate a larger buffer.Troubleshooting Common Issues
Error: "tcpdump: eth0: You don't have permission"
tcpdump needs elevated privileges to open raw network sockets.
# Run with sudo
sudo tcpdump -i eth0 -n
# Or grant the capability directly (avoids sudo requirement)
sudo setcap cap_net_raw,cap_net_admin=eip /usr/sbin/tcpdump
# Verify the capability was set
getcap /usr/sbin/tcpdump
Error: "tcpdump: eth0: No such device exists"
The interface name is wrong. Modern Linux systems often use predictable names like ens3 or enp3s0.
# List all interfaces and their names
ip link show
# or
tcpdump -D
# Use -i any if you're unsure which interface carries the traffic
tcpdump -i any -n -c 10
Error: "tcpdump: syntax error" in BPF Filter
Shell metacharacters in filter expressions need quoting.
# Wrong: shell may misinterpret 'or' and parentheses
tcpdump -i eth0 port 80 or port 443
# Correct: wrap the entire filter in single quotes
tcpdump -i eth0 -n 'port 80 or port 443'
tcpdump -i eth0 -n 'host 1.2.3.4 and port 80'
tcpdump -i eth0 -n 'not host 1.2.3.4 and tcp'
Error: "0 packets captured" Despite Active Traffic
The filter may be too restrictive or you may be watching the wrong interface.
# Start without any filter to confirm traffic exists
tcpdump -i eth0 -n -c 5
# If still empty, try all interfaces
tcpdump -i any -n -c 5
# Check which interfaces have active traffic
ip -s link show
tcpdump Causing High CPU Load
Capturing every packet on a busy interface can be CPU-intensive. Use filters and limits.
# Always limit the capture count
tcpdump -i eth0 -n -c 1000 -w /tmp/cap.pcap
# Narrow with a filter to reduce processed packets
tcpdump -i eth0 -n 'port 80' -c 500
# Run at lower CPU priority
nice -n 10 tcpdump -i eth0 -n -c 500
Frequently Asked Questions
What is the difference between tcpdump and Wireshark?
tcpdump is a command-line tool that runs directly on the server — ideal for capturing packets and saving them to .pcap files. Wireshark is a GUI application on your local machine optimized for analyzing .pcap files interactively. The typical workflow: use tcpdump to capture on the VPS, download the .pcap file, then open it in Wireshark for visual analysis with protocol dissection and filtering.
Can tcpdump capture HTTPS traffic content?
tcpdump can see that HTTPS connections exist (TCP port 443 with a valid handshake), but the payload is TLS-encrypted and unreadable. To inspect HTTPS content you need access to the application's plaintext logs, or tools like mitmproxy in a controlled test environment. Never attempt to decrypt production traffic you don't own.
Is it safe to run tcpdump on a production VPS?
Yes, with some precautions. tcpdump is purely passive — it reads packets but never modifies or injects traffic. Practical guidelines: always use a -c count limit to auto-stop the capture; use BPF filters to reduce the captured volume; delete .pcap files after analysis since they may contain sensitive data; and run at reduced CPU priority with nice during heavy traffic periods.