本文永久链接: https://www.xtplayer.cn/linux/network/tcpdump-man-page/

本文翻译至:https://www.tcpdump.org/manpages/tcpdump.1.html

基本参数

# tcpdump  -h

tcpdump version 4.9.3
libpcap version 1.9.1 (with TPACKET_V3)
OpenSSL 1.1.1g FIPS 21 Apr 2020
Usage: tcpdump [-aAbdDefhHIJKlLnNOpqStuUvxX#] [ -B size ] [ -c count ]
[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]
[ -i interface ] [ -j tstamptype ] [ -M secret ] [ --number ]
[ -Q in|out|inout ]
[ -r file ] [ -s snaplen ] [ --time-stamp-precision precision ]
[ --immediate-mode ] [ -T type ] [ --version ] [ -V file ]
[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]
[ -Z user ] [ expression ]

描述

Tcpdump 打印出与布尔表达式匹配的网络接口上的数据包的描述内容,该描述之前有一个时间戳,默认打印为小时、分钟、秒以及从午夜开始的几分之一秒。它还可以使用 -w 参数运行,将把数据包保存到一个文件中供以后分析,和/或使用 -r 参数从保存的数据包文件中读取,也可以使用 -V 参数读取已保存的包文件列表。在所有情况下,tcpdump 只处理匹配表达式的数据包。

如果不使用 -c 参数指定捕获数据包数量,它会一直捕捉数据包,一直到收到 SIGINT(control + c)或者 SIGTERM(kill 终止命令)信号时才会停止。 如果使用 -c 参数,它会捕捉包到收到 SIGINT 或者 SIGTERM 信号,或者收到指定数量的包之后才会停止。

tcpdump 完成捕捉包,它会报告以下的计数:

  • captured tcpdump 已接收和处理的数据包数量;
  • received by filter 过滤器接收的数据包数量;
  • dropped by kernel 丢弃的包,一般由于 buffer 空间不够引起;

对于支持 SIGINFO 信号的平台,比如大多数的 BSD(包括 mac osx)和 Digital/Tru64 UNIX, 它收到 SIGINFO 信号时会报告这些计数,然后继续捕捉包。从网络设备上读取包需要 root 权限,从文件上读取不需要。使用SIGUSR2信号和 -w 参数将强制把数据包缓冲区刷新到输出文件中。

参数

  • -A

    以 ASCII 码方式显示每一个数据包(不会显示数据包中链路层头部信息). 在抓取包含网页数据的数据包时, 可方便查看数据。

  • -b

    在 BGP 报文中打印 AS 号,使用 ASDOT 代替 ASPLAIN。

  • -B buffer_size

  • –buffer-size=buffer_size

    将操作系统捕获缓冲区大小设置为 buffer_size,单位为 KiB(1024 字节)。

  • -c count

    接收到 count 个数据包后退出。

  • –count

    读取捕获文件时仅在标准错误上打印数据包计数,而不是解析/打印数据包。如果在命令行中指定了过滤器,tcpdump 只对匹配该过滤器表达式的数据包进行统计。

  • -C file_size (此选项用于配合 -w file 选项使用))

    在将原始数据包写入 savefile 之前,检查该文件当前是否大于 file_size,如果大于,则关闭当前 savefile 并打开一个新的 savefile。在第一个保存文件之后的保存文件将具有用 -w 标志指定的名称,后面有一个数字,从 1 开始递增。file_size 的单位是百万字节(1,000,000 字节,而不是 1,048,576 字节)。

  • -d

    以容易阅读的形式在标准输出上打印编译后的包匹配代码并停止。

    请注意,尽管代码编译始终是特定于 DLT 的,但是通常不可能(且不必要)指定用于转储的 DLT,因为 tcpdump 使用 -r 指定的输入 pcap 文件的 DLT 或默认的 DLT。-i 指定的网络接口,或 -y 和 -i 分别指定的网络接口的特定 DLT。在这些情况下,转储显示代码完全相同,可以过滤输入文件或不带-d 的网络接口。

    但是,当没有指定 -r-i 时,指定 -d 可防止 tcpdump 猜测合适的网络接口(参见 -i )。在本例中,DLT 默认为 EN10MB,可以使用 -y 手动将其设置为另一个有效值。

  • -dd

    使用 c 语言片段的方式打印 packet-matching code。

  • -ddd

    使用十进制数字的方式打印 packet-matching code。

  • -D

  • –list-interfaces

    打印系统上可用的网络接口列表,tcpdump 可以在该网络接口上捕获数据包。对于每个网络接口,将打印一个数字和一个接口名称,并可能在其后显示该接口的文本描述。可以将接口名称或编号传递给 -i 参数,以指定要捕获的接口。

    这对于没有命令列出网络接口的操作系统(例如 Windows 系统或缺少 ifconfig -a 的 UNIX 系统)很有用,该数字在 Windows 2000 和更高版本的系统上非常有用,在 Windows 2000 和更高版本的系统上,接口名称是一个有点复杂的字符串。

    如果tcpdump是用libpcap的旧版本构建的,缺少 pcap_findalldevs(3PCAP) 函数,则不支持 -D 参数。

  • -e

    在每个转储行上打印链接级标题。例如,它可以用于打印以太网和 IEEE 802.11 等协议的 MAC 层地址。

  • -E

    Use spi@ipaddr algo:secret for decrypting IPsec ESP packets that are addressed to addr and contain Security Parameter Index value spi. This combination may be repeated with comma or newline separation.

    注意,目前仅支持对 IPv4 ESP 报文设置 secret。

    可用于加密的算法包括 des-cbc, 3des-cbc, blowfish-cbc, rc3-cbc, cast128-cbc, 或者没有(none),默认为 des-cbc。注意:只有在 tcpdump编译时启用了加密技术,才具有解密数据包的能力。

    secret 为用于 ESP 的密钥, 使用 ASCII 字符串方式表达. 如果以 0x 开头, 该密钥将以 16 进制方式读入.

    该选项中 ESP 的定义遵循 RFC2406, 而不是 RFC1827. 并且, 此选项只是用来调试的, 不推荐以真实密钥(secret)来使用该选项, 因为这样不安全: 在命令行中输入的 secret 可以被其他人通过 ps 等命令查看到.

    除了上述语法之外,语法 file name 可用于让 tcpdump 读取提供的文件。该文件在收到第一个 ESP 数据包时被打开,因此 tcpdump 可能已经给予的任何特殊权限都应该已经被放弃。

  • -f

    以数字而不是符号的方式打印 “外部” IPv4 地址(这个选项是为了避免 Sun 的 NIS 服务器的严重脑损伤——通常在转换非本地互联网号码时它会永远挂起)。

    对“外部”IPv4 地址的测试是使用正在捕获的接口的 IPv4 地址和 netmask 完成的。如果该地址或 netmask 不可用,或者因为正在进行捕获的接口没有地址或 netmask,或者因为捕获是在 Linux “any”接口上进行的,该接口可以在多个接口上进行捕获,那么该选项将不能正确工作。

  • -F file

    使用 file 作为过滤表达式的输入。在命令行中给出的附加表达式将被忽略。

  • -G rotate_seconds

    如果指定该参数,将以 -w 选项指定的转储文件每 rotate_seconds 秒旋转一次。保存文件的名称将由 -w 指定,其中应包含strftime定义的时间格式。如果没有指定时间格式,每个新文件将覆盖之前的文件。每当生成的文件名不是唯一的,tcpdump 就会覆盖已存在的数据。因此,不建议提供比捕获周期更粗的时间规范。

    如果与 -C 选项一起使用,文件名将采取 ‘file‘ 的形式。

  • -h

  • –help

    打印 tcpdump 和 libpcap 版本字符串,输出帮助信息并退出。

  • –version

    打印 tcpdump 和 libpcap 版本字符串并退出。

  • -H

    尝试检测 802.11s 网络头。

  • -i interface

  • –interface=interface

    监听 interface 接口。如果没指定,tcpdump 会搜索系统中编号最少的接口(不包括回环地址)。

    对于 linux 2.2 及以后的系统,可以使用 any 来指定捕捉所有的接口。对于 promiscuous 模式不适用。

    如果支持-D 参数,可以使用接口数字来进行指定。

  • -I

  • –monitor-mode

    将接口设置为 monitor mode,这只支持 ieee802.11 Wi-Fi 接口,并且只支持某些操作系统。

    注意,在监控模式下,适配器可能会与它所关联的网络解除关联,因此您将无法使用该适配器的任何无线网络。如果您在监控模式下进行捕获,并且没有通过另一个适配器连接到另一个网络,那么这可能会阻止访问网络服务器上的文件,或解析主机名或网络地址。

    此标志将影响**-L** 标志的输出。如果没有指定 -I,则只显示那些在监控模式下可用的链路层类型;如果指定了 -I,则只显示在监控模式下可用的链路层类型。

  • –immediate-mode

    捕获在 “immediate mode”。在这种模式下,数据包一到达就被发送到 tcpdump,而不是为了提高效率而进行缓冲。如果数据包被打印到终端而不是保存信息包到“savefile”时,这是默认值。

  • -j tstamp_type

  • –time-stamp-type=tstamp_type

    将捕获的时间戳类型设置为tstamp_type。时间戳类型使用的名称见**pcap-tstamp**(7);并不是所有列出的类型对任何给定的接口都是有效的。

  • -J

  • –list-time-stamp-types

    列出接口和出口支持的时间戳类型。如果不能设置接口的时间戳类型,则不列出时间戳类型。

  • –time-stamp-precision=tstamp_precision

    When capturing, set the time stamp precision for the capture to tstamp_precision. Note that availability of high precision time stamps (nanoseconds) and their actual accuracy is platform and hardware dependent. Also note that when writing captures made with nanosecond accuracy to a savefile, the time stamps are written with nanosecond resolution, and the file is written with a different magic number, to indicate that the time stamps are in seconds and nanoseconds; not all programs that read pcap savefiles will be able to read those captures.

    When reading a savefile, convert time stamps to the precision specified by timestamp_precision, and display them with that resolution. If the precision specified is less than the precision of time stamps in the file, the conversion will lose precision.

    The supported values for timestamp_precision are micro for microsecond resolution and nano for nanosecond resolution. The default is microsecond resolution.

  • –micro

  • –nano

    Shorthands for –time-stamp-precision=micro or –time-stamp-precision=nano, adjusting the time stamp precision accordingly. When reading packets from a savefile, using –micro truncates time stamps if the savefile was created with nanosecond precision. In contrast, a savefile created with microsecond precision will have trailing zeroes added to the time stamp when –nano is used.

  • -K

  • –dont-verify-checksums

    Don’t attempt to verify IP, TCP, or UDP checksums. This is useful for interfaces that perform some or all of those checksum calculation in hardware; otherwise, all outgoing TCP checksums will be flagged as bad.

  • -l

    缓冲 stdout 行数。如果您想在捕获数据时查看数据,这是非常有用的。例如,

    tcpdump -l | tee dat 或者 tcpdump -l > dat & tail -f dat

    注意,在 Windows 上,line buffered 意味着 unbuffered,所以如果指定了 -l,WinDump 将单独写入每个字符。

    -U is similar to -l in its behavior, but it will cause output to be ``packet-buffered’’, so that the output is written to stdout at the end of each packet rather than at the end of each line; this is buffered on all platforms, including Windows.

  • -L

  • –list-data-link-types

    List the known data link types for the interface, in the specified mode, and exit. The list of known data link types may be dependent on the specified mode; for example, on some platforms, a Wi-Fi interface might support one set of data link types when not in monitor mode (for example, it might support only fake Ethernet headers, or might support 802.11 headers but not support 802.11 headers with radio information) and another set of data link types when in monitor mode (for example, it might support 802.11 headers, or 802.11 headers with radio information, only in monitor mode).

  • -m module

    module 文件加载 SMI MIB 模块定义。该选项可多次使用,用于将多个 MIB 模块加载到 tcpdump中。

  • -M secret

    如果 TCP 数据包(TCP segments)有 TCP-MD5 选项(在 RFC 2385 有相关描述), 则为其摘要的验证指定一个公共的密钥 secret.

  • -n

    不要将地址(即主机地址、端口号等)转换为名称。

  • -N

    -N 不打印出 host 的域名部分。比如,如果设置了此参数, tcpdump 将会打印 ‘nic’ 而不是 ‘nic.ddn.mil’。

  • -#

  • –number

    在行首打印一个可选的数据包号。

  • -O

  • –no-optimize

    不要运行数据包匹配代码优化器。这只有在您怀疑优化器中存在 bug 时才有用。

  • -p

  • –no-promiscuous-mode

    不要 将接口设置为混杂模式。请注意,接口可能由于其他原因处于混杂模式,因此,’-p’ 不能用作 ‘ether host {local-hw-addr} or ether broadcast’ 的缩写。

  • –print

    即使原始数据包被保存到带有**-w**标志的文件中,也打印输出已解析的数据包。

  • -Q direction

  • –direction=direction

    Choose send/receive direction direction for which packets should be captured. Possible values are in', out’ and `inout’. Not available on all platforms.

  • -q

    快速(安静?)输出。打印更少的协议信息,这样输出行就更短。

  • -r file

    从文件 file 中读取包数据. 如果 file 字段为 ‘-‘ 符号, 则 tcpdump 会从标准输入中读取包数据。

  • -S

  • –absolute-tcp-sequence-numbers

    打印绝对而不是相对的 TCP 序列号。

    相对顺序号可理解为, 相对第一个 TCP 包顺序号的差距,比如, 接受方收到第一个数据包的绝对顺序号为 232323, 对于后来接收到的第 2 个,第 3 个数据包, tcpdump 会打印其序列号为 1, 2 分别表示与第一个数据包的差距为 1 和 2. 而如果此时-S 选项被设置, 对于后来接收到的第 2 个, 第 3 个数据包会打印出其绝对顺序号: 232324, 232325。

  • -s snaplen

  • –snapshot-length=snaplen

    设置 tcpdump 的数据包抓取长度为 snaplen,如果不设置默认将会是 68 字节(而支持网络接口分接头。68 字节对于 IP, ICMP(nt: Internet Control Message Protocol,因特网控制报文协议), TCP 以及 UDP 协议的报文已足够, 但对于名称服务(nt: 可理解为 dns, nis 等服务), NFS 服务相关的数据包会产生包截短. 如果产生包截短这种情况, tcpdump 的相应打印输出行中会出现’’[|proto]’’的标志(proto 实际会显示为被截短的数据包的相关协议层次). 需要注意的是, 采用长的抓取长度,会增加包的处理时间, 并且会减少 tcpdump 可缓存的数据包的数量, 从而会导致数据包的丢失. 所以, 在能抓取我们想要的包的前提下, 抓取长度越小越好。把 snaplen 设置为 0 意味着让 tcpdump 自动选择合适的长度来抓取数据包.

    Snarf snaplen bytes of data from each packet rather than the default of 262144 bytes. Packets truncated because of a limited snapshot are indicated in the output with ``[|proto]’’, where proto is the name of the protocol level at which the truncation has occurred.

    Note that taking larger snapshots both increases the amount of time it takes to process packets and, effectively, decreases the amount of packet buffering. This may cause packets to be lost. Note also that taking smaller snapshots will discard data from protocols above the transport layer, which loses information that may be important. NFS and AFS requests and replies, for example, are very large, and much of the detail won’t be available if a too-short snapshot length is selected.

    If you need to reduce the snapshot size below the default, you should limit snaplen to the smallest number that will capture the protocol information you’re interested in. Setting snaplen to 0 sets it to the default of 262144, for backwards compatibility with recent older versions of tcpdump.

  • -T type

    Force packets selected by “expression“ to be interpreted the specified type. Currently known types are aodv (Ad-hoc On-demand Distance Vector protocol), carp (Common Address Redundancy Protocol), cnfp (Cisco NetFlow protocol), domain (Domain Name System), lmp (Link Management Protocol), pgm (Pragmatic General Multicast), pgm_zmtp1 (ZMTP/1.0 inside PGM/EPGM), ptp (Precision Time Protocol), radius (RADIUS), resp (REdis Serialization Protocol), rpc (Remote Procedure Call), rtcp (Real-Time Applications control protocol), rtp (Real-Time Applications protocol), snmp (Simple Network Management Protocol), someip (SOME/IP), tftp (Trivial File Transfer Protocol), vat (Visual Audio Tool), vxlan (Virtual eXtensible Local Area Network), wb (distributed White Board) and zmtp1 (ZeroMQ Message Transport Protocol 1.0).

    Note that the pgm type above affects UDP interpretation only, the native PGM is always recognised as IP protocol 113 regardless. UDP-encapsulated PGM is often called “EPGM” or “PGM/UDP”.

    Note that the pgm_zmtp1 type above affects interpretation of both native PGM and UDP at once. During the native PGM decoding the application data of an ODATA/RDATA packet would be decoded as a ZeroMQ datagram with ZMTP/1.0 frames. During the UDP decoding in addition to that any UDP packet would be treated as an encapsulated PGM packet.

  • -t

    不要在每个转储行上打印时间戳。

  • -tt

    在每个转储行上打印时间戳。在每个转储行上显示自 1970 年 1 月 1 日、00:00:00、UTC 以来的秒数,以及自该时间以来的几分之一秒。

  • -ttt

    在每个转储行上的当前行和前一行之间打印一个时间增量,微秒或者纳秒由 –time-stamp-precision 选项决定,默认为 微秒。

  • -tttt

    在每个转储行上打印一个时间戳。

  • -ttttt

    在每个转储行的当前行和第一行之间打印一个增量,微秒或者纳秒由 –time-stamp-precision 选项决定,默认为 微秒。

  • -u

    打印未解码的 NFS 句柄。

  • -U

  • –packet-buffered

    If the -w option is not specified, or if it is specified but the –print flag is also specified, make the printed packet output ``packet-buffered’’; i.e., as the description of the contents of each packet is printed, it will be written to the standard output, rather than, when not writing to a terminal, being written only when the output buffer fills.

    如果指定了 -w 选项,则使保存的原始数据包输出为 packet-buffered,也就是说,当每个数据包被保存时,它将被写入输出文件,而不是只在输出缓冲区填充时才写入。

    The -U flag will not be supported if tcpdump was built with an older version of libpcap that lacks the pcap_dump_flush(3PCAP) function.

  • -v

    当分析和打印的时候, 产生详细的输出。

  • -vv

    产生比-v 更详细的输出。

  • -vvv

    产生比 -vv 更详细的输出。

  • -V file

    从 file 中读取一个文件名列表。如果 file 为-,则从标准输入中读取。

  • -w file

    把原生信息写入到 file 文件,而不是进行输出。后续可以使用-r 来读取。如果 file 为 -,则写到标准输出。如果将该输出写入文件或管道,则该输出将被缓冲,因此从文件或管道读取数据包的程序可能在接收数据包后的一段时间内看不到数据包,因为数据还未写入文件。使用 -U 标志使数据包在收到后立即被写入文件。

    The MIME type application/vnd.tcpdump.pcap has been registered with IANA for pcap files. The filename extension .pcap appears to be the most commonly used along with .cap and .dmp. Tcpdump itself doesn’t check the extension when reading capture files and doesn’t add an extension when writing them (it uses magic numbers in the file header instead). However, many operating systems and applications will use the extension if it is present and adding one (e.g. .pcap) is recommended.

    See pcap-savefile(5) for a description of the file format.

  • -W filecount

    此选项与 -C 选项配合使用,这将限制可打开的文件数目,并且当文件数据超过这里设置的限制时,依次循环替代之前的文件, 这相当于一个拥有 filecount 个文件的文件缓冲池。同时,该选项会使得每个文件名的开头会出现足够多并用来占位的 0, 这可以方便这些文件被正确的排序。

    -G 选项一起使用,将限制所创建的旋转转储文件的数量,当达到限制时将以状态 0 退出。

    如果与 -C* 和 -G 同时使用,**-W** 选项当前将被忽略,只会影响文件名。

  • -x

    When parsing and printing, in addition to printing the headers of each packet, print the data of each packet (minus its link level header) in hex. The smaller of the entire packet or snaplen bytes will be printed. Note that this is the entire link-layer packet, so for link layers that pad (e.g. Ethernet), the padding bytes will also be printed when the higher layer packet is shorter than the required padding. In the current implementation this flag may have the same effect as -xx if the packet is truncated.

  • -xx

    解析和打印时,除了打印每个数据包的报头外,还打印每个数据包的数据,包括其链路级报头,十六进制。

  • -X

    When parsing and printing, in addition to printing the headers of each packet, print the data of each packet (minus its link level header) in hex and ASCII. This is very handy for analysing new protocols. In the current implementation this flag may have the same effect as -XX if the packet is truncated.

  • -XX

    解析和打印时,除了打印每个数据包的报头外,还打印每个数据包的数据,包括其链路级报头,十六进制和 ASCII 格式。

  • -y datalinktype

  • –linktype=datalinktype

    Set the data link type to use while capturing packets (see -L) or just compiling and dumping packet-matching code (see -d) to datalinktype.

  • -z postrotate-command

    Used in conjunction with the -C or -G options, this will make tcpdump run “ postrotate-command file “ where file is the savefile being closed after each rotation. For example, specifying -z gzip or -z bzip2 will compress each savefile using gzip or bzip2.

    Note that tcpdump will run the command in parallel to the capture, using the lowest priority so that this doesn’t disturb the capture process.

    And in case you would like to use a command that itself takes flags or different arguments, you can always write a shell script that will take the savefile name as the only argument, make the flags & arguments arrangements and execute the command that you want.

  • -Z user

  • –relinquish-privileges=user

    如果tcpdump以 root 身份运行,在打开捕获设备或输入 savefile 之后,但在打开任何 savefile 输出之前,将用户 ID 更改为user,将组 ID 更改为user的主组。

    这种行为也可以在编译时默认启用。

  • expression

    selects which packets will be dumped. If no expression is given, all packets on the net will be dumped. Otherwise, only packets for which expression is `true’ will be dumped.For the expression syntax, see pcap-filter(7).The expression argument can be passed to tcpdump as either a single Shell argument, or as multiple Shell arguments, whichever is more convenient. Generally, if the expression contains Shell metacharacters, such as backslashes used to escape protocol names, it is easier to pass it as a single, quoted argument rather than to escape the Shell metacharacters. Multiple arguments are concatenated with spaces before being parsed.

示例

  1. 打印所有到达或者离开 sundown 主机的包:

tcpdump host sundown

  1. 打印 holios 与 hot 或者 ace 通信的包:

tcpdump host helios and \( hot or ace \)

  1. 打印除了 helios 的所有与 ace 通信的 ip 包:

tcpdump ip host ace and not helios

  1. To print all traffic between local hosts and hosts at Berkeley:

tcpdump net ucb-ether

  1. To print all ftp traffic through internet gateway snup: (note that the expression is quoted to prevent the shell from (mis-)interpreting the parentheses):

tcpdump 'gateway snup and (port ftp or ftp-data)'

  1. To print traffic neither sourced from nor destined for local hosts (if you gateway to one other net, this stuff should never make it onto your local net).

tcpdump ip and not net** *localnet*

  1. To print the start and end packets (the SYN and FIN packets) of each TCP conversation that involves a non-local host.

tcpdump 'tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst net** *localnet*'

  1. To print the TCP packets with flags RST and ACK both set. (i.e. select only the RST and ACK flags in the flags field, and if the result is “RST and ACK both set”, match)

tcpdump 'tcp[tcpflags] & (tcp-rst|tcp-ack) == (tcp-rst|tcp-ack)'

  1. To print all IPv4 HTTP packets to and from port 80, i.e. print only packets that contain data, not, for example, SYN and FIN packets and ACK-only packets. (IPv6 is left as an exercise for the reader.)

tcpdump 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'

  1. To print IP packets longer than 576 bytes sent through gateway snup:

tcpdump 'gateway snup and ip[2:2] > 576'

  1. To print IP broadcast or multicast packets that were not sent via Ethernet broadcast or multicast:

tcpdump 'ether[0] & 1 = 0 and ip[16] >= 224'

  1. To print all ICMP packets that are not echo requests/replies (i.e., not ping packets):

tcpdump 'icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply'