Tcpdump是用于網(wǎng)絡(luò)數(shù)據(jù)包分析的基本命令行實(shí)用工具。它顯示網(wǎng)絡(luò)上的TCP/IP和其他傳輸?shù)木W(wǎng)絡(luò)數(shù)據(jù)包,tcpdump 適用于大多數(shù)的類(lèi)Unix系統(tǒng)操作系統(tǒng)(如Linux,BSD等)。類(lèi)Unix系統(tǒng)的 tcpdump 需要使用libpcap這個(gè)捕捉數(shù)據(jù)的庫(kù)就像 Windows下的WinPcap。
Tcpdump使用libpcap庫(kù),該庫(kù)有助于捕獲所有Linux/Unix發(fā)行版中可用的網(wǎng)絡(luò)數(shù)據(jù)包。
Tcpdump命令用于從網(wǎng)絡(luò)接口讀取網(wǎng)絡(luò)數(shù)據(jù)包的數(shù)據(jù)。您還可以使用以前創(chuàng)建的數(shù)據(jù)包文件通過(guò)tcpdump進(jìn)行讀取和分析。您可以將tcpdump用作root用戶或具有sudo特權(quán)用戶的命令。
在本教程的此處,我們將討論tcpdump命令在安裝中的用法以及一些基本示例。
讓我們從安裝tcpdump工具開(kāi)始。
Tcpdump安裝:
在大多數(shù)預(yù)裝的Linux發(fā)行版tcpdump中,如果未將其安裝在系統(tǒng)中,則可以使用以下方法將其安裝在系統(tǒng)中。
CentOS/RHEL
您可以使用以下命令在CentOS/RHEL中安裝tcpdump,
$ sudo yum install tcpdump
Fedora
在fedora上,您應(yīng)該使用以下命令安裝tcpdump,
$ dnf install tcpdump
Ubuntu/Debian/Linux Mint
同樣,您可以在Ubuntu或Debian或Linux Mint上使用以下命令來(lái)安裝tcpdump,
現(xiàn)在,我們來(lái)看一下tcpdump命令示例。
例子:
我們將使用下面創(chuàng)建的文件作為示例命令的輸入
示例1:從所有接口捕獲數(shù)據(jù)包
使用命令tcpdump捕獲網(wǎng)絡(luò)軟件包,如下所示:
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)
……
示例2:從特定接口捕獲數(shù)據(jù)包
您可以將“ tcpdump”命令與選項(xiàng)“ -i”和接口名稱一起使用,以從特定接口捕獲網(wǎng)絡(luò)軟件包。
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
……
示例3:檢查可用接口
您可以將“ tcpdump”命令與“ -D”選項(xiàng)一起使用,以顯示系統(tǒng)中所有可用接口,如下所示。
linuxidc@linuxidc:~/www.linuxidc.com$ sudo tcpdump -D
[sudo] linuxidc 的密碼:
1.wlp2s0 [Up, Running]
2.any (Pseudo-device that captures on all interfaces) [Up, Running]
3.lo [Up, Running, Loopback]
4.enp1s0 [Up]
5.bluetooth0 (Bluetooth adapter number 0)
6.nflog (Linux netfilter log (NFLOG) interface)
7.nfqueue (Linux netfilter queue (NFQUEUE) interface)
8.usbmon1 (USB bus number 1)
9.usbmon2 (USB bus number 2)
……
更多Linux命令相關(guān)信息見(jiàn)Linux命令大全 專(zhuān)題頁(yè)面 http://www.haoyitu.cn/topicnews.aspx?tid=16