1。查看咱們xshell是否有抓包工具linux
輸入命令tcpdump。若是有圖下所示:就證實安裝有(通常都會自帶安裝有)shell
2.如下命令進行抓包服務器
tcpdump -i ens192 host 192.168.0.66 -s 1024 -A (這個是抓ip的包)tcp
tcpdump -i ens192 port 8080 -s 1024 -A (這個是抓port的)工具
tcpdump -i ens192 host 192.168.0.66 and port 20250 -s 1024 -A (指定服務器和端口號)google
3.如下是抓到的包atom
其中「captured」的計數指的是應用層捕獲到的數據,「received by filter」和「dropped by kernel」的計數由內核維護,應用層經過getsockopt來獲取。收到一個包,「received by filter」會加1,若是sock的接收buffer被填滿時,則把這個數據包丟棄,將「dropped by kernel」加1。
if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= (unsigned)sk->sk_rcvbuf){
spin_lock(&sk->sk_receive_queue.lock);
po->stats.tp_drops++;
spin_unlock(&sk->sk_receive_queue.lock);
}
經過調節/proc/sys/net/core/rmem_default和/proc/sys/net/core/rmem_max可以改變sk_rcvbuf的大小。
正常「captured」加上「dropped by kernel」應該等於「received by filter」的大小,有的時候出現不等的狀況應該是還有一些數據包在sk_rcvbuf中,尚未被應用層收到的緣由。
丟包緣由:
通過google以及分析,形成這種丟包的緣由是因爲libcap抓到包後,tcpdump上層沒有及時的取出,致使libcap緩衝區溢出,從而覆蓋了未處理包,此處即顯示爲dropped by kernel,注意,這裏的kernel並非說是被linux內核拋棄的,而是被tcpdump的內核,即libcap拋棄掉的
解決方法:
根據以上分析,能夠經過改善tcpdump上層的處理效率來減小丟包率,下面的幾步根據須要選用,每一步都能減小必定的丟包率
1. 最小化抓取過濾範圍,即經過指定網卡,端口,包流向,包大小減小包數量
2. 添加-n參數,禁止反向域名解析
3. 添加-B參數,加大OS capture buffer size
4. 指定-s參數, 最好小於1000
5. 將數據包輸出到cap文件
6. 用sysctl修改SO_REVBUF參數,增長libcap緩衝區長度:/proc/sys/net/core/rmem_default和/proc/sys/net/core/rmem_ma
-B buffer_size
Set the operating system capture buffer size to buffer_size, in units of KiB (1024 bytes).
-n
Don't convert addresses (i.e., host addresses, port numbers, etc.) to names.
-s snaplen
--snapshot-length=snaplen
Snarf snaplen bytes of data from each packet rather than the default of 65535 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. 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 65535, for backwards compatibility with recent older versions of tcpdump.rest