Linux tcpdump命令帮助和示例

Tcpdump是用于网络数据包分析的基本命令行实用工具。它显示网络上的TCP/IP和其他传输的网络数据包,tcpdump 适用于大多数的类Unix系统操作系统(如Linux,BSD等)。类Unix系统的 tcpdump 需要使用libpcap这个捕捉数据的库就像 Windows下的WinPcap。

Tcpdump使用libpcap库,该库有助于捕获所有Linux/Unix发行版中可用的网络数据包。

Linux tcpdump命令帮助和示例

Tcpdump命令用于从网络接口读取网络数据包的数据。您还可以使用以前创建的数据包文件通过tcpdump进行读取和分析。您可以将tcpdump用作root用户或具有sudo特权用户的命令。

在本教程的此处,我们将讨论tcpdump命令在安装中的用法以及一些基本示例。

让我们从安装tcpdump工具开始。

Tcpdump安装:

在大多数预装的Linux发行版tcpdump中,如果未将其安装在系统中,则可以使用以下方法将其安装在系统中。

CentOS/RHEL

您可以使用以下命令在CentOS/RHEL中安装tcpdump,

$ sudo yum install tcpdump

Fedora

在fedora上,您应该使用以下命令安装tcpdump,

$ dnf install tcpdump

Ubuntu/Debian/Linux Mint

同样,您可以在Ubuntu或Debian或Linux Mint上使用以下命令来安装tcpdump,

现在,我们来看一下tcpdump命令示例。

例子:

我们将使用下面创建的文件作为示例命令的输入

示例1:从所有接口捕获数据包

使用命令tcpdump捕获网络软件包,如下所示:

linuxidc@linuxidc:~/www.linuxidc.com$ sudo tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on wlp2s0, link-type EN10MB (Ethernet), capture size 262144 bytes
20:15:28.897761 IP linuxidc.49164 > 183.222.96.208.https: Flags [.], ack 3243612939, win 501, options [nop,nop,TS val 1582584396 ecr 1860790567], length 0
20:15:28.899731 IP linuxidc.41411 > _gateway.domain: 30792+ PTR? 208.96.222.183.in-addr.arpa. (45)
20:15:28.965333 IP 183.222.96.208.https > linuxidc.49164: Flags [.], ack 1, win 643, options [nop,nop,TS val 1860800808 ecr 1582543782], length 0
20:15:29.058203 IP _gateway.domain > linuxidc.41411: 30792 ServFail 0/0/0 (45)
20:15:29.153761 IP linuxidc.49168 > 183.222.96.208.https: Flags [.], ack 179524709, win 501, options [nop,nop,TS val 1582584652 ecr 1860790821], length 0
20:15:29.234028 IP 183.222.96.208.https > linuxidc.49168: Flags [.], ack 1, win 251, options [nop,nop,TS val 1860801061 ecr 1582543871], length 0
20:15:32.847078 IP 192.168.1.113.mdns > 224.0.0.251.mdns: 0 PTR (QM)? _leboremote._tcp.local. (40)

......

Linux tcpdump命令帮助和示例

示例2:从特定接口捕获数据包

您可以将“ tcpdump”命令与选项“ -i”和接口名称一起使用,以从特定接口捕获网络软件包。

linuxidc@linuxidc:~/www.linuxidc.com$ sudo tcpdump -i wlp2s0
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on wlp2s0, link-type EN10MB (Ethernet), capture size 262144 bytes
20:14:05.697766 IP linuxidc.49154 > 183.222.96.208.https: Flags [.], ack 991288211, win 501, options [nop,nop,TS val 1582501196 ecr 1860707369], length 0
20:14:05.699550 IP linuxidc.42223 > _gateway.domain: 49981+ PTR? 208.96.222.183.in-addr.arpa. (45)
20:14:05.755355 IP 183.222.96.208.https > linuxidc.49154: Flags [.], ack 1, win 285, options [nop,nop,TS val 1860717618 ecr 1582450090], length 0
20:14:05.834046 IP linuxidc.58056 > 156.99.224.35.bc.googleusercontent.com.http: Flags [S], seq 1741286821, win 64240, options [mss 1460,sackOK,TS val 1163018628 ecr 0,nop,wscale 7], length 0
20:14:05.893904 IP _gateway.domain > linuxidc.42223: 49981 ServFail 0/0/0 (45)
20:14:05.898067 IP linuxidc.40699 > _gateway.domain: 28863+ PTR? 156.99.224.35.in-addr.arpa. (44)
20:14:06.067226 IP 156.99.224.35.bc.googleusercontent.com.http > linuxidc.58056: Flags [S.], seq 2057714007, ack 1741286822, win 28160, options [mss 1420,sackOK,TS val 2436647274 ecr 1163018628,nop,wscale 7], length 0
20:14:06.067271 IP linuxidc.58056 > 156.99.224.35.bc.googleusercontent.com.http: Flags [.], ack 1, win 502, options [nop,nop,TS val 1163018861 ecr 2436647274], length 0

......

Linux tcpdump命令帮助和示例

示例3:检查可用接口

您可以将“ tcpdump”命令与“ -D”选项一起使用,以显示系统中所有可用接口,如下所示。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/11546.html