متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 69Network · Security

How to capture network traffic with tcpdump

When logs do not explain it — "the app says it connected, the server says nothing arrived" — look at the packets. tcpdump shows what really goes over the wire: which side starts, which side resets, whether DNS answers, whether TLS starts. This how-to captures with the right filters, saves to a file for Wireshark, captures inside a container or Pod, and reads the common patterns (SYN without answer, RST, retransmits).

  • 5 الدروس
  • 9 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

69.1

The quick answer

tcpdump -D
tcpdump -i eth0 -nn 'host 10.0.2.21 and port 5432'
tcpdump -i any -nn -c 2000 -w /tmp/app-db.pcap 'port 5432'
tcpdump -nn -r /tmp/app-db.pcap | head

-nn = no DNS and no port names (faster, clearer). -w saves a file; open it later in Wireshark.

الأوامر في هذا الدرس

69.2

Step by step

  1. Install and find the interface:
dnf -y install tcpdump
apt-get install -y tcpdump
tcpdump -D
  1. Always use a filter — a busy server sends millions of packets:
FilterCaptures
host 10.0.2.21Everything to or from that IP.
port 443 / tcp port 5432One port.
src 203.0.113.50 and dst port 22One direction.
net 10.0.0.0/16A whole network.
'tcp[tcpflags] & (tcp-syn|tcp-rst) != 0'Only connection starts and resets.
udp port 53DNS.
not port 22Hide your own SSH session.
  1. Read the TCP flags in the output:
tcpdump -i eth0 -nn 'tcp port 5432 and host 10.0.2.21'
10:42:01.120 IP 10.0.1.11.48122 > 10.0.2.21.5432: Flags [S], seq 123, win 64240
10:42:02.121 IP 10.0.1.11.48122 > 10.0.2.21.5432: Flags [S], seq 123, win 64240
You seeMeans
[S] again and again, no [S.]No answer: firewall drops, or the host is down.
[S] then [R.]Nothing listens on the port (connection refused).
[S], [S.], [.]Handshake OK — the problem is higher (TLS, auth, app).
Many retransmissionsPacket loss on the way.
  1. Save to a file for Wireshark or a colleague — limit size and time:
tcpdump -i eth0 -nn -s 0 -c 5000 -w /tmp/web.pcap 'port 443'
tcpdump -i eth0 -nn -G 300 -W 12 -w '/var/tmp/cap-%H%M.pcap' 'port 443'

The second line writes a new file every 5 minutes and keeps 12 files (one hour) — for problems that come and go.

  1. Plain HTTP or DNS content (TLS content is encrypted — you see only the handshake):
tcpdump -i eth0 -nn -A -s 0 'tcp port 80' | grep -E 'GET|POST|Host:'
tcpdump -i any -nn 'udp port 53'
  1. Inside a container or Pod:
kubectl debug -it api-6d8f9c7b5d-x2kq9 -n shop --image=nicolaka/netshoot:v0.13 --target=api -- tcpdump -i any -nn port 5432
nsenter -t $(docker inspect -f '{{.State.Pid}}' api) -n tcpdump -i any -nn port 5432
  1. Rules: captures can contain passwords, cookies and personal data. Capture only with permission, only what you need (filters, -c), keep the file 600, delete it when done.

69.3

Check that it worked

You can tell where the conversation stops: no SYN-ACK (network/firewall), a RST (nothing listens / something closes it), or a full handshake (look at TLS or the app). Then delete the capture files.

69.4

Common problems

ProblemCauseFix
tcpdump: … You don't have permissionNeeds root (or the CAP_NET_RAW capability).Run with sudo.
Nothing is capturedWrong interface (VPN, bridge, container).tcpdump -D; try -i any.
Too much output, terminal freezesNo filter, or name lookups.Add a filter and -nn; write to a file with -w.
The file is hugeNo -c or rotation.-c COUNT, or -G/-W rotation.
You can not read HTTPS contentTLS encryption (on purpose).Look at the handshake; use app logs or debug at the proxy for content.
Packets look "missing" on a busy hostThe kernel dropped them (dropped by kernel at the end).Stronger filter; write to a file; bigger buffer -B 4096.

الأوامر في هذا الدرس

69.5

Go further

1You see the client's [S] again and again, but never [S.]. What does it mean?

The SYN gets no answer: a firewall drops it, or the host is down or unreachable.

2Why use -nn?

No DNS and port-name lookups: the output is faster and shows the real IPs and port numbers.

كل الأوامر في هذا الفصل