Home > Article > Operation and Maintenance > What is the linux packet capture command?
The Linux packet capture command is "tcpdump", which can capture the data packets flowing on the network card, and can completely intercept the "header" of the data packets transmitted in the network to provide analysis; it supports network layer, Filtering of protocols, hosts, networks or ports, and providing logical statements such as and, or, not to help you remove useless information.
#The operating environment of this tutorial: Red Hat Enterprise Linux 6.1 system, Dell G3 computer.
The tcpdump command is a datagram sniffing tool based on the command line of the Unix system. It can capture the data packets flowing on the network card.
As the name suggests, tcpdump can completely intercept the "headers" of data packets transmitted on the network and provide analysis. It supports filtering for network layers, protocols, hosts, networks or ports, and provides logical statements such as and, or, not to help you remove useless information. With its powerful functions and flexible interception strategies, tcpdump makes it UNIX-like The preferred tool for network analysis and troubleshooting under the system.
Practical command examples:
(1).Start by default
#普通情况下,直接启动tcpdump将监视第一个网络接口上所有流过的数据包. [root@localhost ~]# tcpdump
(2). Monitor the data packets of the specified network interface
[root@localhost ~]# tcpdump -i eth0 -c 10
(3). Monitor the data packets of the specified host
[root@localhost ~]# tcpdump -i eth0 host 10.20.3.25
(4 .).Get all data sent by host 10.20.3.25
[root@localhost ~]#tcpdump -i eth0 src host 10.20.3.25
(5).Monitor all data packets sent to host 10.20.3.25
[root@localhost ~]# tcpdump -i eth0 dst host 10.20.3.25
(6). Monitor the data packets of the specified host and port
[root@localhost ~]# tcpdump tcp port 22 and host 10.20.3.25
(7). Monitor the data packets of the specified network, such as the communication between this machine and the 10.20.3 network segment Data packets, "-c 10" means to capture only 10 packets
[root@localhost ~]# tcpdump -c 10 net 10.20.3
(8). Capture ping packets
[root@localhost ~]# tcpdump -c 5 -nn -i eth0 icmp
( 9). Parse packet data
[root@localhost ~]# tcpdump -c 2 -q -XX -vvv -nn -i eth0 tcp dst port 22 tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes 10:22:22.334383 IP (tos 0x0, ttl 63, id 26834, offset 0, flags [DF], proto TCP (6), length 40) 10.20.3.25.60401 > 10.20.9.131.22: tcp 0 0x0000: 0050 5685 2ba8 0074 9c0f c748 0800 4500 .PV.+..t...H..E. 0x0010: 0028 68d2 4000 3f06 b23a 0a14 0319 0a14 .(h.@.?..:...... 0x0020: 0983 ebf1 0016 93e3 6ba8 cd6b d1ce 5010 ........k..k..P. 0x0030: f6b4 0d8e 0000 0000 0000 0000 ............ 10:22:22.376759 IP (tos 0x0, ttl 63, id 26835, offset 0, flags [DF], proto TCP (6), length 40) 10.20.3.25.60401 > 10.20.9.131.22: tcp 0 0x0000: 0050 5685 2ba8 0074 9c0f c748 0800 4500 .PV.+..t...H..E. 0x0010: 0028 68d3 4000 3f06 b239 0a14 0319 0a14 .(h.@.?..9...... 0x0020: 0983 ebf1 0016 93e3 6ba8 cd6b d392 5010 ........k..k..P. 0x0030: faf0 078e 0000 0000 0000 0000 ............ 2 packets captured 2 packets received by filter 0 packets dropped by kernel [root@test-core-services-03 ~]#
(10).tcpdump capture HTTP packet
tcpdump -XvvennSs 0 -i eth0 tcp[20:2]=0x4745 or tcp[20:2]=0x4854 0x4745 为"GET"前两个字母"GE",0x4854 为"HTTP"前两个字母"HT"。
tcpdump common options:
Its command format is:
tcpdump [ -DenNqvX ] [ -c count ] [ -F file ] [ -i interface ] [ -r file ] [ -s snaplen ] [ -w file ] [ expression ] 抓包选项: -c:指定要抓取的包数量。注意,是最终要获取这么多个包。例如,指定"-c 10"将获取10个包,但可能已经处理了100个包,只不过只有10个包是满足条件的包。 -i interface:指定tcpdump需要监听的接口。若未指定该选项,将从系统接口列表中搜寻编号最小的已配置好的接口(不包括loopback接口,要抓取loopback接口使用tcpdump -i lo), :一旦找到第一个符合条件的接口,搜寻马上结束。可以使用'any'关键字表示所有网络接口。 -n:对地址以数字方式显式,否则显式为主机名,也就是说-n选项不做主机名解析。 -nn:除了-n的作用外,还把端口显示为数值,否则显示端口服务名。 -N:不打印出host的域名部分。例如tcpdump将会打印'nic'而不是'nic.ddn.mil'。 -P:指定要抓取的包是流入还是流出的包。可以给定的值为"in"、"out"和"inout",默认为"inout"。 -s len:设置tcpdump的数据包抓取长度为len,如果不设置默认将会是65535字节。对于要抓取的数据包较大时,长度设置不够可能会产生包截断,若出现包截断, :输出行中会出现"[|proto]"的标志(proto实际会显示为协议名)。但是抓取len越长,包的处理时间越长,并且会减少tcpdump可缓存的数据包的数量, :从而会导致数据包的丢失,所以在能抓取我们想要的包的前提下,抓取长度越小越好。 输出选项: -e:输出的每行中都将包括数据链路层头部信息,例如源MAC和目标MAC。 -q:快速打印输出。即打印很少的协议相关信息,从而输出行都比较简短。 -X:输出包的头部数据,会以16进制和ASCII两种方式同时输出。 -XX:输出包的头部数据,会以16进制和ASCII两种方式同时输出,更详细。 -v:当分析和打印的时候,产生详细的输出。 -vv:产生比-v更详细的输出。 -vvv:产生比-vv更详细的输出。 其他功能性选项: -D:列出可用于抓包的接口。将会列出接口的数值编号和接口名,它们都可以用于"-i"后。 -F:从文件中读取抓包的表达式。若使用该选项,则命令行中给定的其他表达式都将失效。 -w:将抓包数据输出到文件中而不是标准输出。可以同时配合"-G time"选项使得输出文件每time秒就自动切换到另一个文件。可通过"-r"选项载入这些文件以进行分析和打印。 -r:从给定的数据包文件中读取数据。使用"-"表示从标准输入中读取。
Related recommendations: "Linux Video Tutorial"
The above is the detailed content of What is the linux packet capture command?. For more information, please follow other related articles on the PHP Chinese website!