在Ubuntu中使用Sniffer(如tcpdump、Wireshark)过滤数据包的方法如下:
sudo apt-get update && sudo apt-get install tcpdump
sudo tcpdump -i eth0 port 80 # 捕获eth0接口的HTTP流量
sudo tcpdump src 192.168.1.100 # 捕获源IP为192.168.1.100的数据包
sudo tcpdump dst 192.168.1.100 # 捕获目标IP为192.168.1.100的数据包
sudo tcpdump 'tcp and port 443 and host 192.168.1.100' # 捕获该主机的HTTPS流量
sudo tcpdump -i eth0 -w capture.pcap # 保存为pcap文件供后续分析
sudo apt-get update && sudo apt-get install wireshark
http
:过滤HTTP流量。ip.addr == 192.168.1.100 && tcp.port == 22
:过滤该IP的SSH流量。udp and net 10.0.0.0/8
:过滤10.0.0.0/8网段的UDP流量。Enter
应用过滤,结果会实时显示匹配的数据包。条件 | 语法示例 | 说明 |
---|---|---|
协议 | tcp /udp /icmp |
按协议过滤 |
端口 | port 80 /src port 22 |
按端口号过滤 |
IP地址 | host 192.168.1.100 |
捕获与该IP相关的所有流量 |
网段 | net 192.168.1.0/24 |
捕获该网段的所有流量 |
组合条件 | 'tcp and port 443 and host 10.0.0.1' |
多条件组合(需用引号包裹) |
sudo
获取root权限,避免权限不足导致无法捕获数据包。以上方法可快速定位目标流量,结合工具的实时分析功能可有效排查网络问题。