Home Operation and Maintenance Linux Operation and Maintenance What is the linux packet capture command?

What is the linux packet capture command?

May 08, 2021 pm 03:59 PM
linux tcpdump

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.

What is the linux packet capture command?

#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
Copy after login

(2). Monitor the data packets of the specified network interface

[root@localhost ~]# tcpdump -i eth0 -c 10
Copy after login

(3). Monitor the data packets of the specified host

[root@localhost ~]# tcpdump -i eth0 host 10.20.3.25
Copy after login

(4 .).Get all data sent by host 10.20.3.25

[root@localhost ~]#tcpdump -i eth0 src host 10.20.3.25
Copy after login

(5).Monitor all data packets sent to host 10.20.3.25

[root@localhost ~]# tcpdump -i eth0 dst host 10.20.3.25
Copy after login

(6). Monitor the data packets of the specified host and port

[root@localhost ~]# tcpdump tcp port 22 and host 10.20.3.25
Copy after login

(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
Copy after login

(8). Capture ping packets

[root@localhost ~]# tcpdump -c 5 -nn -i eth0 icmp
Copy after login

( 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 ~]#
Copy after login

(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"。
Copy after login

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:从给定的数据包文件中读取数据。使用"-"表示从标准输入中读取。
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

See all articles