首頁·終端機命令

tcpdump

網路

用法

sudo tcpdump -i <if> -nn <filter>

把線路上經過的封包原樣抓下來;它需要 root,抓什麼寫在選項之後、用它自己的過濾語法如 `port 443 and host x`,而在繁忙介面上不加過濾直接抓,畫面瞬間就被埋掉。

常用選項

選項含義
-i anyCapture on every interface; -i eth0 for just one.
-nnNo name and no port-number lookups, so nothing is guessed.
-c 100Stop after a hundred packets.
-w <file.pcap>Write raw packets to a file for Wireshark; -r reads one back.
-A / -XPrint payloads as text / as hex and text.
port 443 and host 10.0.0.5The filter, in its own pcap syntax, after the flags.
-s 0Full packets. Modern versions already do this by default.

範例

sudo tcpdump -i any -nn port 443 -c 20

Twenty HTTPS packets, then stop.

sudo tcpdump -i eth0 -w capture.pcap host 10.0.0.5

Record one host to a file and open it in Wireshark later.

sudo tcpdump -i any -nn -A "tcp port 80"

Read plain HTTP requests as they go past.

連不上的時候,這些能把範圍縮小——是名稱解析不了、沒有路由,還是連接埠被擋。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. tcpdump 是做什麼的?

把線路上經過的封包原樣抓下來;它需要 root,抓什麼寫在選項之後、用它自己的過濾語法如 `port 443 and host x`,而在繁忙介面上不加過濾直接抓,畫面瞬間就被埋掉。

Q. 怎麼寫?

sudo tcpdump -i <if> -nn <filter> —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 7 個,完整列表在 man tcpdump。這條命令屬於網路。

相關命令

man 手冊: man tcpdump