首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >​tcpdump工具使用

​tcpdump工具使用

原创
作者头像
skystone
发布2025-12-08 14:36:07
发布2025-12-08 14:36:07
7520
举报
文章被收录于专栏:Linux DocLinux Doc

1. 环境与安装

CentOS/Rocky Linux,tcpdump属于@base组的包,如果安装服务器时选择了@base组,那么默认已安装。如果未安装,可以通过以下命令安装即可。

代码语言:shell
复制
    yum install tcpdump

2. 参考文档

tcpdump官网:http://www.tcpdump.org/#documentation

3. tcpdump命令格式

3.1. tcpdump -h

代码语言:shell
复制
    ## 简单看,
    tcpdump [options] [expression]
    ## 常用的,
    tcpdump -i any 
    ## tcpdump -h 完整详细的
    Usage: tcpdump [-aAdDefIKlLnNOpqRStuUvxX] [ -B size ] [ -c count ]
		[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]
		[ -i interface ] [ -M secret ] [ -r file ]
		[ -s snaplen ] [ -T type ] [ -w file ] [ -W filecount ]
		[ -y datalinktype ] [ -z command ] [ -Z user ]
		[ expression ]

通过查看man tcpdump,发现环境变量LANG为zh_CN.UTF-8和en_US.UTF-8分别会打印中文和英文手册文档,而尴尬的是,中文手册信息量要少。。。。有些选项没有解释。。。。不更新的么。。。所以通过man tcpdump查看手册时,需要export LANG=en_US.UTF-8。。。

3.2. options,tcpdump选项

tcpdump选项按照是否指定了 -w 写入文件选项,分为2类,来区分2种场景,控制台打印输出和写入文件两种场景。因为-w选项是将 raw 数据包 ( 含header和data ) 写入文件,所以其余选项大部分是用于控制台输出场景,来控制输出的。这个要注意。在简单查看和实时抓包需求下,掌握其余选项很重要,可以在控制台直接看结果。

3.2.1. 包抓取来源
3.2.1.1. -i选项,从网络接口采集

从指定接口(网卡)上监听抓取数据包流量。一定要指定,否则tcpdump会根据查到的所有的系统接口,然后抓取除loopback外编号最小的,融发基础平台里,如果是centos7,是virbr0。。。

3.2.1.2. -r选项,从文件读取

从文件中读取数据包,而不是抓取网卡流量。如果是-,那么从stdin读取。

3.2.2 打印与输出
3.2.2.1 打印

默认为打印

3.2.2.1.1. 是否打印data
  1. -x: 在解析和打印场景下,除打印header外,以16进制格式输出data,以及链路层header(最少输出)。data长度由-s参数决定,默认65535字节。
  2. -xx: 在解析和打印场景下,除打印header外,以16进制格式输出data,以及链路层header。
  3. -X:在解析和打印场景下,除打印header外,以16进制和ASCII两种方式同时输出数据包的header和data,以及链路层header(最少输出)。
  4. -XX:在解析和打印场景下,除打印header外,以16进制和ASCII两种方式同时输出数据包的header和data,以及链路层header。
3.2.2.1.2. 更多详细打印

-v, -vv, -vvv, 更多输出,更多更多输出,更多更多更多输出。这些更多对融发基础平台的抓包实际工作没有帮助。

  1. -v, 更多表示输出ip包的ttl, id, total length, options 和 checksum。
  2. -vv, 更多表示输出NFS响应包,解码后的SMB包。
  3. -vvv, 更多表示输出telnet的SB...SE选项。这些跟融发基础平台的网络抓包调试都没什么关系。
3.2.2.1.3. 时间戳

-t, -tt, -ttt, -tttt, -ttttt

  1. -t表示每个dump输出行不打印timestamp。
  2. -tt表示打印unformatted timestamp,
  3. -ttt表示输出距离上一个报文的delta时间,
  4. -tttt表示按照默认格式输出timestamp,
  5. -ttttt表示输出距离first报文的delta时间。
3.2.2.1.4. 数字与名称转换

-n, -nn, 按数字格式输出IP和port,不要转换为名称。

  1. -n, 输出IP,而非主机名。如果不设置,tcpdump会lookup DNS,将IP转换为主机名。
  2. -nn, 输出port,而非协议名。
3.2.2.1.5. Link Layer链路层

-e

输出打印链路层header,如Ethernet和IEEE 802.11协议中的MAC地址。

3.2.2.2. 输出到文件
3.2.2.2.1. -w选项,指定写入文件

将 raw 数据包 ( 含header和data ) 写入文件,而非解析并打印出来。如果参数值为-,那么输出到stdout。该文件之后可以使用 tcpdump -r <filename> 选项指定作为输入来解析打印查看。

3.2.2.2.2. -G选项,切割输出文件

指定间隔多长时间,rotate一下-w指定的写入文件。这里rotate有2种情况,如果-w选项的值中包含strftime(3)格式的时间变量,那么新写入,如果不包含,那么间隔时间到了,就会覆盖写入同一个文件。 strftime(3) 格式,%Y,4位数字的年份,%m,月份,%d,天,%H,小时,%M,分钟,%S,秒。例如grabnics%Y%m%d%H%M%S.cap。这个strftime没有毫秒ms吗???!Unix时间戳到秒s,表示从1970年1月1日(UTC)开始所经过的秒数,额。

-G 60要和-Z root结合使用,否则会报permission ded错误。

-G 60表示60秒新生成1个新文件。

例如:

代码语言:shell
复制
    tcpdump -i bond1 -G 60 -Z root -nn -X "host 172.22.95.60 or host 172.22.95.61" -w capture_kafka_fbscj_%Y_%m_%d_%H_%M_%S.cap
3.2.3. 其他选项

-D

打印输出tcpdump可以抓取的所有网络接口的列表,列表中每一行包含一个网络接口,包括接口名称和编号,接口名称和编号都可以用于-i选项的值。

-c

指定抓取报文数量。这个数量是IP包的数量??还是TCP Segment包的数量?按照融发基础平台的操作系统的特点,TCP的MSS小于MTU,不会进行IP包的分片,所以这里的报文数量就是IP包的数量,也是TCP Segment的数量。

3.3. expression,tcpdump表达式

筛选对哪些包进行dump。如果没有指定expression,所有网络数据包都会被dump。否则,只有符合expression条件的包才会被dump。

3.3.1. 基本语法构成

expression的语法,是libpcap提供的,参考pcap-filter(7)

pcap-filter - packet filter syntax,包过滤语法

man pcap-filter的开头描述如下:

The filter expression consists of one or more primitives. Primitives usually consist of an id (name or number) preceded by one or more qualifiers. There are three different kinds of qualifier. type, id name或者number所属的类型,包括host, net, port 和 portrange。dir, direction, 传输方向,包括src, dst, src or dst, src and dst, ra, ta, addr1, addr2, addr3, and addr4. 融发基础平台这里,就是src, dst, src or dst, src and dst. proto, protocol, 指定协议,包括ether, fddi, tr, wlan, ip, ip6, arp, rarp, decnet, tcp 和 udp. 融发基础平台这里,就是ether, ip, arp, rarp, tcp和udp。

以上翻译一下,就是expression表达式由1个或多个原型构成。原型是通过修饰符+id来构成。有3类修饰符:type, dir, proto。这就是pcap filter的基本语法和构成。

expression参数可以是1个或者多个参数。如果expression包含了shell命令的特殊字符,可以用单引号或者双引号封装。

expression一般是多个参数,譬如 tcp port 19000,可以用引号封装,如果没有shell特殊字符,也可以不用引号封装。例如,tcp port (19000 or 6379),这样子的expression,括号()就是shell特殊字符,需要用引号封装。tcpdump -i bond1 "tcp port (19000 or 6379)"。

在实际使用中,主要是编写这个表达式。

3.3.2. 可用原型
3.3.2.1. IP和MAC地址匹配

True if either the IPv4/v6 source or destination of the packet is host.

代码语言:txt
复制
    dst host <host>
    src host <host>
    host <host>

也可以在上面的原型前加关键词,ip, arp, rarp, or ip6

代码语言:txt
复制
    ip host <host>
    ##等价于
    ether proto \ip and host <host>
    ##当host为name名称时,这个name名称对应的所有IP都需要匹配。

特定的, ether协议,

True if the Ethernet destination address is ehost. Ehost may be either a name from /etc/ethers or a number (see ethers(3N) for numeric format).

代码语言:txt
复制
    ether dst <ehost>
    ether src <ehost>
    ether host <ehost>
3.3.2.2. 网段匹配
代码语言:txt
复制
    dst net <net>
    src net <net>
    net <net>
    net <net> mask <netmask>
          True if the IPv4 address matches net with the specific netmask.  May be qualified with src or dst.  Note that this syntax is not valid for IPv6 net.
    
    net <net>/<len>
          True if the IPv4/v6 address matches net with a netmask len bits wide.  May be qualified with src or dst.

<net>格式为

代码语言:txt
复制
    172.21
    10.11.10
    10.11.10.0
    10.11.10.0/24

<netmask>格式为

代码语言:txt
复制
    255.255.255.0
3.3.2.3. 广播和组播匹配
代码语言:txt
复制
    ether broadcast
          True if the packet is an Ethernet broadcast packet.  The ether keyword is optional.
    
    ip broadcast

    ether multicast
          True if the packet is an Ethernet multicast packet.  The ether keyword is optional.  This is shorthand for `ether[0] & 1 != 0'.
    
    ip multicast
          True if the packet is an IPv4 multicast packet.
    
    ip6 multicast
          True if the packet is an IPv6 multicast packet.
3.3.2.4. 端口匹配
代码语言:txt
复制
    dst port <port>
    src port <port>
    port <port>

    dst portrange <port1>-<port2>
    src portrange <port1>-<port2>
    portrange <port1>-<port2>

也可以在上面的原型前加关键词,tcp,udp

代码语言:txt
复制
    tcp src port port
3.3.2.5. 报文长度匹配
代码语言:txt
复制
    less length
    ##等价于
    len <= length.
    
    greater length
    ##等价于
    len >= length.
3.3.2.6. 协议匹配
代码语言:txt
复制
    ip proto <protocol>
    ##protocol可以是icmp, icmp6, igmp, igrp, pim, ah, esp, vrrp, udp, or tcp.  
    ##注意 tcp, udp, and icmp 也是关键词,所以需要转义 (\), which  is  \\  in  the  C-shell.
    ##Note that this primitive does not chase the protocol header chain.
    
    proto <protocol>
          True if the packet is an IPv4 or IPv6 packet of protocol type protocol.  Note that this primitive does not chase the protocol header chain.
    
    tcp, udp, icmp
    ##等价于 proto <p>, where p is one of the above protocols.
    
    ip protochain protocol
          Equivalent to ip6 protochain protocol, but this is for IPv4.
    
    protochain protocol
          True if the packet is an IPv4 or IPv6 packet of protocol type protocol.  Note that this primitive chases the protocol header chain.
3.3.2.7. 复杂表达式
代码语言:txt
复制
    <expr> <relop> <expr>

relop为 >, <, >=, <=, =, !=

expr为整型数字(符合标准C语法)、操作符(+, -, *, /, &, |, <<, >>)、len、报文数据取值器(special packet data accessors).

所有的比较数都是正数,即大于0的。

报文数据取值器语法(有点像CSS取值器), proto expr : size

proto: 可以是ether, fddi, tr, wlan, ppp, slip, link, ip, arp, rarp, tcp, udp, icmp, ip6 or radio.

expr: 表示报文数据偏移量。可以用数字,也可以用预定义名称,

icmptype (ICMP type field), icmpcode (ICMP code field), and tcpflags (TCP flags field).

size: 可选,可以不指定。表示从偏移量开始的取值长度,默认为1。

特殊的:The length operator, indicated by the keyword len, gives the length of the packet.

获取所有组播流量:

代码语言:txt
复制
    `ether[0]  &  1  !=  0' catches all multicast traffic.  

获取所有带options扩展选项的IP报文:

代码语言:txt
复制
    `ip[0] & 0xf != 5' catches all IPv4 packets with options.  

获取未分片的IPv4报文:

代码语言:txt
复制
    `ip[6:2] & 0x1fff = 0' 

上面这个,默认用于tcp和udp的检查。例如,tcp0 表示TCP header的第1个字节, 而不是表示分片后的IP报文里的TCP包的第1个字节。

预定义名称:

有一些预定义的名称,用于指定偏移量。如:

代码语言:txt
复制
    icmptype (ICMP type field), icmpcode (ICMP code field), and tcpflags (TCP flags field).

有一些预定义的名称,用于expr比对。如:

代码语言:txt
复制
    ICMP type field values : 
    icmp-echoreply, icmp-unreach, icmp-sourcequench, icmp-redirect, icmp-echo, icmp-routeradvert,  icmp-router-
    solicit, icmp-timxceed, icmp-paramprob, icmp-tstamp, icmp-tstampreply, icmp-ireq, icmp-ireqreply, icmp-maskreq, icmp-maskreply.
    
    TCP flags field values are available: 
    tcp-fin, tcp-syn, tcp-rst, tcp-push, tcp-ack, tcp-urg.

注意:

均为十进制。

如果是十六进制,在前面加0x。

例如tcp54==0x45。

wireshark中,tcp36==45即可,36和45默认为16进制。

4. tcpdump抓包步骤

以下命令中,为选择的比较好的选项,例如-i为指定网卡,-nn为用数字代替名字,-X为输出打印报文data,-w为写入文件(尽量结合-G进行日志滚动和切割)。其余选项和参数不用考虑了。

  1. 用tcpdump -D 命令查看tcpdump可以捕捉的所有接口,找到想抓包的网络接口名称,作为-i选项的值。如果想在所有网络接口上抓包,-i选项值用any即可。
  2. 考虑使用-w写入文件还是直接print到stdout
  3. 如果-w,则
代码语言:shell
复制
    tcpdump -i any [-G 60 ] -w capture_%Y%m%d%H%M%S.cap [expression]

然后将生成的cap文件下载到自己电脑上,用wireshark软件打开,进行分析即可。

  1. 如果不需要写入文件,

如果只想进行报文header分析,则

代码语言:shell
复制
    tcpdump -i any -nn [expression] >> capture_%Y%m%d%H%M%S.txt,

如果想进行data分析,则

代码语言:shell
复制
    tcpdump -i any -nn -X [expression] >> capture_%Y%m%d%H%M%S.txt

如果想进行mac分析,则

代码语言:shell
复制
    tcpdump -i any -nn -XX [expression] >> capture_%Y%m%d%H%M%S.txt。

可以适当的加-v, -vv, -vvv,我觉得没必要。

5. expression格式及特定场景样例

5.1. 抓取流量保存为文件

抓取应用网卡的访问应用IP的19000或者6379的流量,并保存到文件,每60秒写入新文件。

代码语言:shell
复制
    # tcpdump -i bond1 -G 60 -w capture_%Y%m%d%H%M%S.cap   "dst port ( 19000 or 6379 )"
    tcpdump: listening on bond1, link-type EN10MB (Ethernet), capture size 65535 bytes

5.2. 指定IP地址的流量打印

抓取host为10.10.10.11的流量,并输出打印。

代码语言:shell
复制
    # tcpdump -i bond1 -nn "host 10.10.10.11"
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on bond1, link-type EN10MB (Ethernet), capture size 65535 bytes
    13:52:53.704130 IP 10.10.10.10.60572 > 10.10.10.11.6391: Flags [P.], seq 2700335951:2700335965, ack 1567953358, win 1064, options [nop,nop,TS val 366958701 ecr 365279623], length 14
    13:52:53.704146 IP 10.10.10.10.45212 > 10.10.10.11.36379: Flags [P.], seq 1767418609:1767418623, ack 268640420, win 140, options [nop,nop,TS val 366958701 ecr 365279623], length 14
    13:52:53.704157 IP 10.10.10.10.36733 > 10.10.10.11.6390: Flags [P.], seq 1782314294:1782314308, ack 1178219614, win 860, options [nop,nop,TS val 366958701 ecr 365279623], length 14
    13:52:53.704195 IP 10.10.10.11.6391 > 10.10.10.10.60572: Flags [P.], seq 1:8, ack 14, win 6198, options [nop,nop,TS val 365280663 ecr 366958701], length 7
    13:52:53.704202 IP 10.10.10.10.60572 > 10.10.10.11.6391: Flags [.], ack 8, win 1064, options [nop,nop,TS val 366958701 ecr 365280663], length 0
    13:52:53.704206 IP 10.10.10.11.36379 > 10.10.10.10.45212: Flags [P.], seq 1:8, ack 14, win 501, options [nop,nop,TS val 365280663 ecr 366958701], length 7
    13:52:53.704213 IP 10.10.10.10.45212 > 10.10.10.11.36379: Flags [.], ack 8, win 140, options [nop,nop,TS val 366958701 ecr 365280663], length 0
    13:52:53.704215 IP 10.10.10.11.6390 > 10.10.10.10.36733: Flags [P.], seq 1:8, ack 14, win 6195, options [nop,nop,TS val 365280663 ecr 366958701], length 7
    13:52:53.704221 IP 10.10.10.10.36733 > 10.10.10.11.6390: Flags [.], ack 8, win 860, options [nop,nop,TS val 366958701 ecr 365280663], length 0

5.3. 指定IP地址和指定端口的流量,输出打印

代码语言:shell
复制
    # tcpdump -i bond1 -nn "src host 10.10.10.15 and dst port 19000"
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on bond1, link-type EN10MB (Ethernet), capture size 65535 bytes
    13:50:39.825245 IP 10.10.10.15.43183 > 10.10.10.10.19000: Flags [P.], seq 2289673102:2289673116, ack 2789196841, win 29, length 14
    13:50:39.825426 IP 10.10.10.15.43183 > 10.10.10.10.19000: Flags [.], ack 8, win 29, length 0
    13:51:07.755530 IP 10.10.10.15.43188 > 10.10.10.10.19000: Flags [P.], seq 2477004096:2477004110, ack 2540095981, win 29, length 14
    13:51:07.755636 IP 10.10.10.15.43188 > 10.10.10.10.19000: Flags [.], ack 6, win 29, length 0
    13:51:07.755826 IP 10.10.10.15.43188 > 10.10.10.10.19000: Flags [R.], seq 14, ack 7, win 29, length 0
    13:51:09.825923 IP 10.10.10.15.43183 > 10.10.10.10.19000: Flags [P.], seq 14:28, ack 8, win 29, length 14
    13:51:09.826024 IP 10.10.10.15.43183 > 10.10.10.10.19000: Flags [.], ack 13, win 29, length 0
    13:51:09.826090 IP 10.10.10.15.43183 > 10.10.10.10.19000: Flags [R.], seq 28, ack 14, win 29, length 0

5.4. 指定IP地址段的流量,输出打印

代码语言:shell
复制
    # tcpdump -i any -nn "net 10.10.90" >>  manage_terminal_$(date +%Y%m%d%H%M).txt
    
    13:48:59.575908 IP 10.11.10.140.22 > 10.10.90.229.49844: Flags [P.], seq 4068340242:4068340438, ack 3013917833, win 149, length 196

5.5. NTP报文,输出打印,并打印报文data本身

NTP服务器

代码语言:shell
复制
    # tcpdump -i any -nn -X udp port 123
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
    20:15:35.400431 IP 172.21.98.20.123 > 172.21.95.200.123: NTPv4, Client, length 48
    	0x0000:  45c0 004c e8c6 0000 fd11 ba12 ac15 6214  E..L..........b.
    	0x0010:  ac15 5fc8 007b 007b 0038 b39c 2304 06ec  .._..{.{.8..#...
    	0x0020:  0000 480b 0000 3025 ac15 5fc8 e0b0 b124  ..H...0%.._....$
    	0x0030:  6699 367d e0b0 b124 6685 6fe2 e0b0 b124  f.6}...$f.o....$
    	0x0040:  6699 367d e0b0 b167 667d c439            f.6}...gf}.9
    20:15:35.400495 IP 172.21.95.200.123 > 172.21.98.20.123: NTPv4, Server, length 48
    	0x0000:  45c0 004c 0000 4000 4011 1fda ac15 5fc8  E..L..@.@....._.
    	0x0010:  ac15 6214 007b 007b 0038 1a51 2403 06e9  ..b..{.{.8.Q$...
    	0x0020:  0000 47f4 0000 2f4a ca7b 6a66 e0b0 ae5d  ..G.../J.{jf...]
    	0x0030:  f6ae be33 e0b0 b167 667d c439 e0b0 b167  ...3...gf}.9...g
    	0x0040:  6682 a944 e0b0 b167 6686 51d7            f..D...gf.Q.

NTP 客户端

代码语言:shell
复制
    # tcpdump -i any -nn -X udp port 123
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
    12:20:36.131646 IP 10.10.10.10.123 > 10.13.10.133.123: NTPv4, Client, length 48
    	0x0000:  45c0 004c 0000 4000 4011 4da7 ac14 508c  E..L..@.@.M...P.
    	0x0010:  ac14 4385 007b 007b 0038 ec83 2305 0ae8  ..C..{.{.8..#...
    	0x0020:  0000 4827 0000 b893 ac14 5095 e0b0 aebe  ..H'......P.....
    	0x0030:  2f47 392e e0b0 ae5a 2a56 1c00 e0b0 ae5a  /G9....Z*V.....Z
    	0x0040:  29f7 e80d e0b0 b294 21b2 4bc0            ).......!.K.
    12:20:36.131652 IP 10.10.10.10.123 > 10.13.10.133.123: NTPv4, Client, length 48
    	0x0000:  45c0 004c 0000 4000 4011 4da7 ac14 508c  E..L..@.@.M...P.
    	0x0010:  ac14 4385 007b 007b 0038 ec83 2305 0ae8  ..C..{.{.8..#...
    	0x0020:  0000 4827 0000 b893 ac14 5095 e0b0 aebe  ..H'......P.....
    	0x0030:  2f47 392e e0b0 ae5a 2a56 1c00 e0b0 ae5a  /G9....Z*V.....Z

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 环境与安装
  • 2. 参考文档
  • 3. tcpdump命令格式
    • 3.1. tcpdump -h
    • 3.2. options,tcpdump选项
      • 3.2.1. 包抓取来源
      • 3.2.2 打印与输出
      • 3.2.3. 其他选项
    • 3.3. expression,tcpdump表达式
      • 3.3.1. 基本语法构成
      • 3.3.2. 可用原型
  • 4. tcpdump抓包步骤
  • 5. expression格式及特定场景样例
    • 5.1. 抓取流量保存为文件
    • 5.2. 指定IP地址的流量打印
    • 5.3. 指定IP地址和指定端口的流量,输出打印
    • 5.4. 指定IP地址段的流量,输出打印
    • 5.5. NTP报文,输出打印,并打印报文data本身
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档