监控io性能、free命令、ps命令、查看网络状态、linux下抓包

监控io性能

iostat -x 磁盘使用

iostat 在安装sysstat的时候会自动安装上这个命令。它和sar是同一个包php

[root@yong-02 ~]# yum install -y sysstat
[root@yong-02 ~]# iostat
Linux 3.10.0-693.el7.x86_64 (yong-02) 	2018年05月07日 	_x86_64_	(1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           6.22    0.00    6.70    2.76    0.00   84.33

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda              15.21       360.51        95.85     187493      49847
scd0              0.03         1.98         0.00       1028          0
dm-0             13.01       340.49        91.91     177078      47798

iostat和sar命令差很少,后面接数字
iostat 2,2秒显示一次;sda bk_read/s kb_wrtn/s 读写速度前端

[root@yong-02 ~]# iostat 2
Linux 3.10.0-693.el7.x86_64 (yong-02) 	2018年05月07日 	_x86_64_	(1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           5.84    0.00    6.30    2.59    0.00   85.28

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda              14.48       338.31        94.29     187509      52263
scd0              0.03         1.85         0.00       1028          0
dm-0             12.43       319.52        90.57     177094      50198

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.00    0.00    0.00  100.00

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.00         0.00         0.00          0          0
scd0              0.00         0.00         0.00          0          0
dm-0              0.00         0.00         0.00          0          0

用sar -b 也能够查看读写速度mysql

  • iostat -x 1 这里最重要的是%util;这列表示你的IO等待,磁盘使用有多少时间占用cpu,cpu有部分时间给进程经销计算,也有一部分时间等待io的,等待磁盘读和写,这个时间比是多少,占比是多大呢,这个就是%util。若是这个数字很大,50%60%那你的IO太差,他很是的忙,数字很大,读和写也很大,若是读和写两列不大,可是util这列很大,说明你的硬盘存在问题和故障。若是你的硬盘很慢,确定会影响你的性能,即便你的CPU很快在厉害,硬盘跟不上也会存在很大的瓶颈的。记住iostat -x 关注%util这一列。
[root@yong-02 ~]# iostat -x 1
Linux 3.10.0-693.el7.x86_64 (yong-02) 	2018年05月07日 	_x86_64_	(1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           3.40    0.00    3.69    1.48    0.00   91.43

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
sda               0.00     0.16    6.96    1.38   193.64    54.31    59.47     0.30   36.44   17.84  130.61   7.56   6.31
scd0              0.00     0.00    0.02    0.00     1.06     0.00   114.22     0.00    5.22    5.22    0.00   4.11   0.01
dm-0              0.00     0.00    6.17    1.00   182.89    52.18    65.57     0.31   43.67   18.61  198.89   8.00   5.73

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.00    0.00    0.00  100.00

iotop 磁盘使用

若是发现磁盘io很忙,究竟是那个进程在读写呢,咱们用iotop来查看。linux

[root@yong-02 ~]# yum install -y iotop

 

[root@yong-02 ~]# iotop

Total DISK READ :	0.00 B/s | Total DISK WRITE :       0.00 B/s
Actual DISK READ:	0.00 B/s | Actual DISK WRITE:       0.00 B/s
  TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND    
    1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % systemd --switched-root --system --deserialize 21
    2 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthreadd]
    3 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/0]
    5 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/0:0H]
    7 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/0]
    8 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_bh]
    9 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_sched]
   10 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [watchdog/0]
   12 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kdevtmpfs]
   13 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [netns]
   14 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [khungtaskd]
   15 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [writeback]
   16 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kintegrityd]
   17 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [bioset]
   18 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kblockd]
   19 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [md]
  532 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [xfs-log/sda1]
  533 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [xfs-eofblocks/s]
  534 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [xfsaild/sda1]
   25 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kswapd0]
   26 be/5 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksmd]
   27 be/7 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [khugepaged]

free命令

  • free 查看内存使用状况
  • free -m / -g / -h
  • buffer/cache区别
  • 公式:total=used+free+buff/cache
  • avaliable包含free和buffer/cache剩余

free 单位KBios

[root@yong-02 ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1008152      122500      622348        6884      263304      710160
Swap:       2097148           0     2097148

free -m 单位Msql

[root@yong-02 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            984         119         607           6         257         693
Swap:          2047           0        2047

free -h 单位G后端

[root@yong-02 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           984M        119M        607M        6.7M        257M        693M
Swap:          2.0G          0B        2.0G
  1. total 是整个内存大小 total=used+free+buff/cache缓存

  2. used 使用了多少内存服务器

  3. free 剩余多少内存 正则剩余的内存是第二行的free网络

  4. available 正则可用的内存 available=free+剩余buff/cache 

输入图片说明

  • cache:磁盘速度是比较慢的,cpu比较快,二者速度相差比较大,为了缓解这个局面,工程师就想到了缓存这个思路,缓存是介于cpu和磁盘之间的一种介子,他的速度比cpu慢一点,比磁盘速度快一点,当咱们从磁盘里取数据的时候速度很慢,就能够事先把数据取出来放到内存里(cache)当cpu用到的时候就从内存里去取,这个速度就比原来直接从磁盘里取拿数据速度要快不少,这里内存里存放这部分数据的空间就要cache
  • buffe正好相反,cpu把数据处理完后,造成一个结果(也是数据)要写入到磁盘里去的时候,这个速度也很慢的,咱们一样的速度也是先把它写到内存里,内存再逐渐慢慢地在后台一点点写入磁盘里,这一部分空间就叫中buffer(缓冲)
  • buffer 是即将写入到磁盘里去的数据暂存的内存区域
  • cached是从磁盘里读出来的数据暂存的内存区域。 linux内核会先分配一部分buffer,cacha出来留着备用,这部分空间不可能直接用完,可能会有一部分剩余,那么这部分剩余实际上对内存来讲是可用的,可回收的。
    内存正真剩余=物理内存+buffer+cache
    free列第一行的数字为0表示内存已经分配完,但并不表示内存就耗尽了。 由于可能还有分配的cached并无用完。
    物理内存、buffer和cached三个没有再可分配的容量那就意味着全部的物理内存都耗尽了,
    真正的内存用完了致使频繁使用swap 程序运行速度会超级慢

ps 查看系统当中全部的进程

  1. top -bn1 也能够显示全部的进程
[root@yong-02 ~]# top -bn1
top - 20:40:10 up  1:04,  1 user,  load average: 0.03, 0.02, 0.05
Tasks:  91 total,   1 running,  90 sleeping,   0 stopped,   0 zombie
%Cpu(s):  5.6 us, 11.1 sy,  0.0 ni, 83.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1008152 total,   601044 free,   130932 used,   276176 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   695940 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
    1 root      20   0  128164   6820   4060 S  0.0  0.7   0:08.53 systemd
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.01 kthreadd
    3 root      20   0       0      0      0 S  0.0  0.0   0:00.82 ksoftirqd/0
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
    7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh
    9 root      20   0       0      0      0 S  0.0  0.0   0:04.10 rcu_sched
   10 root      rt   0       0      0      0 S  0.0  0.0   0:00.07 watchdog/0
   12 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs
   13 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns
   14 root      20   0       0      0      0 S  0.0  0.0   0:00.00 khungtaskd
   15 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 writeback
   16 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kintegrityd
   17 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   18 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kblockd
   19 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 md
。。。
  1. ps aux 一盘工做中使用这个命令
[root@yong-02 ~]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.2  0.6 128164  6820 ?        Ss   19:35   0:08 /usr/lib/systemd/systemd --switched-root --system --deserialize
root         2  0.0  0.0      0     0 ?        S    19:35   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    19:35   0:00 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   19:35   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S    19:35   0:00 [migration/0]
root         8  0.0  0.0      0     0 ?        S    19:35   0:00 [rcu_bh]
root         9  0.1  0.0      0     0 ?        R    19:35   0:04 [rcu_sched]
root        10  0.0  0.0      0     0 ?        S    19:35   0:00 [watchdog/0]
root        12  0.0  0.0      0     0 ?        S    19:35   0:00 [kdevtmpfs]
root        13  0.0  0.0      0     0 ?        S<   19:35   0:00 [netns]
root        14  0.0  0.0      0     0 ?        S    19:35   0:00 [khungtaskd]
root        15  0.0  0.0      0     0 ?        S<   19:35   0:00 [writeback]
root        16  0.0  0.0      0     0 ?        S<   19:35   0:00 [kintegrityd]
root        17  0.0  0.0      0     0 ?        S<   19:35   0:00 [bioset]
root        18  0.0  0.0      0     0 ?        S<   19:35   0:00 [kblockd]
root        19  0.0  0.0      0     0 ?        S<   19:35   0:00 [md]
root        25  0.0  0.0      0     0 ?        S    19:35   0:00 [kswapd0]
root        26  0.0  0.0      0     0 ?        SN   19:35   0:00 [ksmd]
。。。。
  1. ps -elf 这二者显示的内容是差很少的
  2. [root@yong-02 ~]# ps -elf
    F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
    4 S root         1     0  0  80   0 - 32041 ep_pol 19:35 ?        00:00:08 /usr/lib/systemd/systemd --switched-root --system --d
    1 S root         2     0  0  80   0 -     0 kthrea 19:35 ?        00:00:00 [kthreadd]
    1 S root         3     2  0  80   0 -     0 smpboo 19:35 ?        00:00:00 [ksoftirqd/0]
    1 S root         5     2  0  60 -20 -     0 worker 19:35 ?        00:00:00 [kworker/0:0H]
    1 S root         7     2  0 -40   - -     0 smpboo 19:35 ?        00:00:00 [migration/0]
    1 S root         8     2  0  80   0 -     0 rcu_gp 19:35 ?        00:00:00 [rcu_bh]
    1 R root         9     2  0  80   0 -     0 -      19:35 ?        00:00:04 [rcu_sched]
    5 S root        10     2  0 -40   - -     0 smpboo 19:35 ?        00:00:00 [watchdog/0]
    5 S root        12     2  0  80   0 -     0 devtmp 19:35 ?        00:00:00 [kdevtmpfs]
    1 S root        13     2  0  60 -20 -     0 rescue 19:35 ?        00:00:00 [netns]
    1 S root        14     2  0  80   0 -     0 watchd 19:35 ?        00:00:00 [khungtaskd]
    1 S root        15     2  0  60 -20 -     0 rescue 19:35 ?        00:00:00 [writeback]
    1 S root        16     2  0  60 -20 -     0 rescue 19:35 ?        00:00:00 [kintegrityd]
    1 S root        17     2  0  60 -20 -     0 rescue 19:35 ?        00:00:00 [bioset]
    1 S root        18     2  0  60 -20 -     0 rescue 19:35 ?        00:00:00 [kblockd]
    1 S root        19     2  0  60 -20 -     0 rescue 19:35 ?        00:00:00 [md]

     

USER 用户
PID 进程标志数,/proc文件里面有跟pid相同名字的目录;进程的id,这个id颇有用,在linux中内核管理进程就得靠pid来识别和管理某一个程,好比我想终止某一个进程,则用 ‘kill 进程的pid 有时并不能杀掉,则须要加一个-9选项了 kill -9 进程pid

某个进程有问题,我想知道它是那个目录下的?(pid都在proc这个目录下,每个pid就是一个目录)

  • ls /proc/进程ID/ %cpu 占用cpu百分比
    %mem 占用内存百分比
    VSZ 虚拟内存大小
    RSS 真正内存大小
    TTY 从哪里启动,pts/0 当前终端

  • STAT 进程的状态 S 表示休眠sleep
    s 主进程,父进程
    < 高优先级,优先获得cpu的资源
    N 低优先级

  • 前台运行进程
    S<s 高优先级的主进程,休眠状态
    R runing,正在运行的进程(某个时间段使用到cpu资源的进程)
    L 被锁的进程
    l 多线程的进程
    Z僵尸进程
    x 已经死掉的进程
    T 暂停的进程ctrl+z
    D 不能被终端的进程,好比IO,不多见。
    START 进程启动时间
    TIME 占用cpu多长时间
    COMMAND 进程名称
[root@yong-02 ~]# ps aux |grep -c kworker
10
[root@yong-02 ~]# ps aux |grep  kworker
root         5  0.0  0.0      0     0 ?        S<   19:35   0:00 [kworker/0:0H]
root        37  0.0  0.0      0     0 ?        S    19:35   0:00 [kworker/u128:1]
root       278  0.0  0.0      0     0 ?        S    19:35   0:00 [kworker/u128:2]
root       292  0.0  0.0      0     0 ?        S<   19:35   0:00 [kworker/0:1H]
root       545  0.0  0.0      0     0 ?        S<   19:36   0:00 [kworker/u129:0]
root       549  0.0  0.0      0     0 ?        S<   19:36   0:00 [kworker/u129:2]
root      1217  0.0  0.0      0     0 ?        S    20:35   0:00 [kworker/0:1]
root      1298  0.1  0.0      0     0 ?        S    20:39   0:00 [kworker/0:3]
root      1311  0.1  0.0      0     0 ?        S    20:44   0:00 [kworker/0:0]
root      1315  0.0  0.0 112676   980 pts/0    R+   20:45   0:00 grep --color=auto kworker

监控系统状态

  • netstat 查看网络状态
  • netstat -lnp 查看监听端口
  • netstat -an 查看系统的网络链接情况
  • netstat -lntp 只看出tcp的,不包含socket
  • ss -an 和nestat殊途同归 分享一个小技巧:
    netstat -an | awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}'

netstat 命令是用来查看网络链接状态,系统全部开放端口,路由表等信息

  1. netstat -lnp 用来监听有哪些接口
[root@yong-02 ~]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      990/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1094/master         
tcp6       0      0 :::22                   :::*                    LISTEN      990/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1094/master         
udp        0      0 127.0.0.1:323           0.0.0.0:*                           657/chronyd         
udp6       0      0 ::1:323                 :::*                                657/chronyd         
raw6       0      0 :::58                   :::*                    7           684/NetworkManager  
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     12287    1/systemd            /run/lvm/lvmetad.socket
unix  2      [ ACC ]     STREAM     LISTENING     7715     1/systemd            /run/systemd/journal/stdout
unix  2      [ ACC ]     STREAM     LISTENING     12085    1/systemd            /run/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     19024    1094/master          public/pickup
unix  2      [ ACC ]     STREAM     LISTENING     14937    1/systemd            /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     19028    1094/master          public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     19031    1094/master          public/qmgr
unix  2      [ ACC ]     STREAM     LISTENING     19053    1094/master          public/flush
unix  2      [ ACC ]     STREAM     LISTENING     19068    1094/master          public/showq
unix  2      [ ACC ]     STREAM     LISTENING     19035    1094/master          private/tlsmgr
…………

查看监听的接口后,服务端到底跟哪些客户端进行通讯,以及客户端跟咱们服务端通讯过程到底十一个什么样的状态?
是链接进行数据传输仍是二者刚刚创建链接,仍是通讯完成后保持一个链接而正在等待:要用netstat -an 来查看

netstat -an

  1. 查看服务端和客户端之间的状态
  2. 用这个值去衡量服务器有没有压力,有没有并发,并发是多少
  3. netstat -an |grep 122.122.69.86:80|grep -ic estab -i是忽略大小写 -c符合要求的行数
    这台服务器的并发数是884.意思是在这一秒或者这一个时刻内有884个链接。

netstat -lntp

netstat -lntp 查看监听tcp的端口

[root@yong-02 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      990/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1094/master         
tcp6       0      0 :::22                   :::*                    LISTEN      990/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1094/master

netstat -an tcp IP

[root@yong-02 ~]# netstat -an tcp IP
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0     52 192.168.180.135:22      192.168.180.1:56906     ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
udp        0      0 127.0.0.1:323           0.0.0.0:*                          
udp6       0      0 ::1:323                 :::*                               
raw6       0      0 :::58                   :::*                    7          
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     12287    /run/lvm/lvmetad.socket
unix  2      [ ]         DGRAM                    7699     /run/systemd/notify
unix  2      [ ]         DGRAM                    7701     /run/systemd/cgroups-agent
unix  2      [ ]         DGRAM                    15898    /var/run/chrony/chronyd.sock
unix  2      [ ACC ]     STREAM     LISTENING     7715     /run/systemd/journal/stdout
unix  5      [ ]         DGRAM                    7718     /run/systemd/journal/socket
unix  14     [ ]         DGRAM                    7720     /dev/log
unix  2      [ ACC ]     STREAM     LISTENING     12085    /run/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     19024    public/pickup
unix  2      [ ACC ]     STREAM     LISTENING     14937    /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     19028    public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     19031    public/qmgr
unix  2      [ ACC ]     STREAM     LISTENING     19053    public/flush
…………

.sock linux,unix 中特有的文件,能够通信
Path 状态
TIME_WAIT 传输完成 连接保持着
ESTABLTSHED 已经创建连接正在通信
FIN_WAIT2 状态详情,可参考【TCP/IP 三次握手】
netstat -an |grep 112.112.69.86:80 查看80端口 并发状态
netstat -an |grep 112.112.69.86:80 |grep -ic estab 统计连接(ESTABLTSHED)正在通信的连接总数 (前端 静态网页 2到3万 后端 有php mysql等 2000到3000 左右

  • netstat -an | awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}'
[root@yong-02 ~]# netstat -an | awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}'
LISTEN 	 4
ESTABLISHED 	 1

tcpdump 工具

若是没有这个工具须要安装一下;yum install -y tcpdump

[root@yong-02 ~]# yum install tcpdump
[root@yong-02 ~]# tcpdump -nn -i ens33

tcpdump -i ens33

第一个n就是ip以数字的形式打印出来,若是不加就会显示主机名

tcpdump -n -i ens33

第2n是端口

tcpdump -nn -i ens33 加上第2n 就会显示ip,端口形式显示

tcpdump 命令:

  • -i:指定网卡
  • host:指定ip
  • port:指定端口
  • -c :指定包数量
  • -w :写入指定文件里,将包的内容写入文件里面,若是不加-w直接在屏幕上显示的不是数据包,而是数据流向
  • length 长度
  • tcpdump -nn -i ens33 port 22 指定22端口

  • tcpdump -nn -i ens33 port 22 -c 10 抓10个包

[root@yong-02 ~]# tcpdump -nn -i ens33 port 22 -c 10
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
21:36:02.536663 IP 192.168.180.135.22 > 192.168.180.1.56906: Flags [P.], seq 1439628768:1439628964, ack 2081619192, win 264, length 196
21:36:02.536990 IP 192.168.180.1.56906 > 192.168.180.135.22: Flags [.], ack 196, win 2050, length 0
21:36:02.537623 IP 192.168.180.135.22 > 192.168.180.1.56906: Flags [P.], seq 196:472, ack 1, win 264, length 276
21:36:02.537939 IP 192.168.180.135.22 > 192.168.180.1.56906: Flags [P.], seq 472:636, ack 1, win 264, length 164
21:36:02.538046 IP 192.168.180.1.56906 > 192.168.180.135.22: Flags [.], ack 636, win 2049, length 0
21:36:02.538223 IP 192.168.180.135.22 > 192.168.180.1.56906: Flags [P.], seq 636:896, ack 1, win 264, length 260
21:36:02.538424 IP 192.168.180.135.22 > 192.168.180.1.56906: Flags [P.], seq 896:1060, ack 1, win 264, length 164
21:36:02.538591 IP 192.168.180.1.56906 > 192.168.180.135.22: Flags [.], ack 1060, win 2047, length 0
21:36:02.538762 IP 192.168.180.135.22 > 192.168.180.1.56906: Flags [P.], seq 1060:1320, ack 1, win 264, length 260
21:36:02.538997 IP 192.168.180.135.22 > 192.168.180.1.56906: Flags [P.], seq 1320:1484, ack 1, win 264, length 164
10 packets captured
11 packets received by filter
0 packets dropped by kernel
  • tcpdump -nn -i ens33 port 22 -c 6 -w /tmp/a.cap 写入指定文件里,将包的内容写入文件里面,是真真的包,若是不加-w直接在屏幕上显示的不是数据包,而是数据流向
[root@yong-02 ~]# tcpdump -nn -i ens33 port 22 -c 10 -w /tmp/a.cap
tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
10 packets captured
10 packets received by filter
0 packets dropped by kernel
  • tcpdump -r 查看数据流,不是数据包
[root@yong-02 ~]# tcpdump -r /tmp/a.cap 
reading from file /tmp/a.cap, link-type EN10MB (Ethernet)
21:37:05.911668 IP yong-02.ssh > 192.168.180.1.56906: Flags [P.], seq 1439631392:1439631524, ack 2081620060, win 264, length 132
21:37:05.911849 IP 192.168.180.1.56906 > yong-02.ssh: Flags [.], ack 132, win 2048, length 0
21:37:19.519031 IP 192.168.180.1.56906 > yong-02.ssh: Flags [P.], seq 1:37, ack 132, win 2048, length 36
21:37:19.559309 IP yong-02.ssh > 192.168.180.1.56906: Flags [.], ack 37, win 264, length 0
21:37:21.878678 IP 192.168.180.1.60396 > yong-02.ssh: Flags [S], seq 506333761, win 64240, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
21:37:21.878775 IP yong-02.ssh > 192.168.180.1.60396: Flags [S.], seq 3917968579, ack 506333762, win 29200, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
21:37:21.878958 IP 192.168.180.1.60396 > yong-02.ssh: Flags [.], ack 1, win 2053, length 0
21:37:21.915472 IP yong-02.ssh > 192.168.180.1.60396: Flags [P.], seq 1:22, ack 1, win 229, length 21
21:37:21.956067 IP 192.168.180.1.60396 > yong-02.ssh: Flags [.], ack 22, win 2053, length 0
21:37:22.024462 IP 192.168.180.1.60396 > yong-02.ssh: Flags [P.], seq 1:50, ack 22, win 2053, length 49

wireshark 工具

若是没有须要先安装一下 yum install -y wireshark

[root@yong-02 ~]# yum install -y wireshark
  • 用法:tshark 只须要记住这条命令;用的时候复制一下 tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri"
相关文章
相关标签/搜索