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