tcpdump
网络
用法
sudo tcpdump -i <if> -nn <filter>把线路上经过的数据包原样抓下来;它需要 root,抓什么写在选项之后、用它自己的过滤语法如 `port 443 and host x`,而在繁忙接口上不加过滤直接抓,屏幕瞬间就被埋掉。
常用选项
| 选项 | 含义 |
|---|---|
| -i any | Capture on every interface; -i eth0 for just one. |
| -nn | No name and no port-number lookups, so nothing is guessed. |
| -c 100 | Stop after a hundred packets. |
| -w <file.pcap> | Write raw packets to a file for Wireshark; -r reads one back. |
| -A / -X | Print payloads as text / as hex and text. |
| port 443 and host 10.0.0.5 | The filter, in its own pcap syntax, after the flags. |
| -s 0 | Full packets. Modern versions already do this by default. |
示例
sudo tcpdump -i any -nn port 443 -c 20Twenty HTTPS packets, then stop.
sudo tcpdump -i eth0 -w capture.pcap host 10.0.0.5Record 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