​十三次课 2018-08-23

监控系统状态

使用w查看系统负载

w/uptime查看系统负载

  • w
[root@linux7-128 ~]# w
 20:04:47 up 2 min,  1 user,  load average: 0.61, 0.67, 0.29
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.220.1    20:03    7.00s  0.04s  0.00s w

第一行显示系统时间、当前运行时间、目前登录用户(可以从下面看到)、系统负载
load average: 0.61, 0.67, 0.29
这三个数字分别表示1分钟、5分钟、15分钟之内,系统的负载分别是多少。

负载值低时,代表系统目前负载较小,有点浪费。如何合理利用?
要看有几颗逻辑CPU,不是物理CPU。

FROM:表示从哪里来
LOGIN@:表示登陆时间
IDLE:空闲了多长时间
JCPU、PCPU:使用了CPU的时间
WHAT:命令

  • uptime

和w命令第一行相同,所以我们平时使用w查看即可

[root@linux7-128 ~]# uptime 
 20:35:44 up 33 min,  1 user,  load average: 0.00, 0.01, 0.05

cat /proc/cpuinfo 查看CPU核数

  • cat /proc/cpuinfo
[root@linux7-128 ~]# cat /proc/cpuinfo 
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 58
model name	: Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
stepping	: 9
microcode	: 0x12
cpu MHz		: 2493.791
cache size	: 3072 KB
physical id	: 0
siblings	: 2
core id		: 0
cpu cores	: 2
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt aes xsave avx f16c rdrand hypervisor lahf_lm epb fsgsbase smep xsaveopt dtherm ida arat pln pts
bogomips	: 4988.84
clflush size	: 64
cache_alignment	: 64
address sizes	: 40 bits physical, 48 bits virtual
power management:

processor	: 1
vendor_id	: GenuineIntel
cpu family	: 6
model		: 58
model name	: Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
stepping	: 9
microcode	: 0x12
cpu MHz		: 2493.791
cache size	: 3072 KB
physical id	: 0
siblings	: 2
core id		: 1
cpu cores	: 2
apicid		: 1
initial apicid	: 1
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt aes xsave avx f16c rdrand hypervisor lahf_lm epb fsgsbase smep xsaveopt dtherm ida arat pln pts
bogomips	: 4988.84
clflush size	: 64
cache_alignment	: 64
address sizes	: 40 bits physical, 48 bits virtual
power management:

查看processor数,即代表本机的CPU核数,如果是0,代表有1颗。如果是1,代表是2颗,以此类推。

查看physical id数,代表本机物理cpu,如果是1,代表有2个物理CPU

如果说CPU核数是8,即processor数为7时,那么负载值不应大于8,都可以正常运行。5分钟平均负载和15分钟平均负载也是如此,如果负载值大于8,那么表示CPU核数已经不够用了。

lscpu 查看CPU信息

[root@bogon ~]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                1
On-line CPU(s) list:   0
Thread(s) per core:    1
Core(s) per socket:    1
座:                 1
NUMA 节点:         1
厂商 ID:           GenuineIntel
CPU 系列:          6
型号:              61
型号名称:        Intel(R) Core(TM) i5-5250U CPU @ 1.60GHz
步进:              4
CPU MHz:             1599.803
BogoMIPS:            3199.60
超管理器厂商:  VMware
虚拟化类型:     完全
L1d 缓存:          32K
L1i 缓存:          32K
L2 缓存:           256K
L3 缓存:           3072K
NUMA 节点0 CPU:    0
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt arat spec_ctrl intel_stibp arch_capabilities

vmstat监控系统状态

  • vmstat命令
[root@linux7-128 ~]# vmstat 
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 718992   2192 165800    0    0    34     3   55   52  0  0 98  1  0
  • 常用用法

vmstat 1 每一秒查看一次系统状态,不需要时使用Ctrl+c停止

[root@linux7-128 ~]# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 719188   2192 165764    0    0    31     3   53   50  0  0 98  1  0
 0  0      0 719172   2192 165764    0    0     0     0   76   59  0  0 100  0  0
 0  0      0 719172   2192 165764    0    0     0     0   46   42  0  0 100  0  0
 0  0      0 719172   2192 165764    0    0     0     0   49   41  0  0 100  0  0
 0  0      0 719172   2192 165764    0    0     0     0   78   58  0  0 100  0  0
 0  0      0 719172   2192 165764    0    0     0     0   70   47  0  0 100  0  0
^C

vmstat 1 5 每一秒查看一次系统状态,查看5次

[root@linux7-128 ~]# vmstat 1 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 719176   2192 165756    0    0    30     3   52   49  0  0 98  1  0
 0  0      0 719144   2192 165756    0    0     0     0   63   50  0  0 100  0  0
 0  0      0 719144   2192 165756    0    0     0     0   52   45  0  0 100  0  0
 0  0      0 718756   2192 165772    0    0     0     0  150  126  0  1 99  0  0
 0  0      0 718756   2192 165772    0    0     0     0   50   41  0  1 99  0  0

  • 需要关注点
  1. r=run 有多少进程处于运行状态
    当进程有多个时,而CPU只有一个,那么就需要轮流使用,在一个时刻只允许一个进程运行

  2. b=block 有多少个进程在等待

  3. swpd
    当内存不够时,系统可以拿出内存中的一部分数据临时放到swap交换分区里去。如果swpd值一直变换时,说明交换分区和内存在频繁的交换数据,代表内存不够了

  4. si/so
    si 有多少k的块数据,从swap进入到内存中
    so 有多少k的块数据,从内存中进入到swap
    如果swpd在频繁变幻时,这两个值肯定也会变动。

  5. bi/bo
    bi 有多少k的数据从磁盘进入到内存中
    bo 有多少k的数据从内存写入到磁盘
    这两个数值很大时,说明磁盘在频繁的读写。跟CPU和内存比,磁盘是很慢的,如果很频繁的读些数据会导致b列的增加。因为有很多进程在等待磁盘。

  6. us 用户级别的,数字不会超过100,百分比
    表示用户的资源占用CPU的百分比,如果数字大于50,说明系统肯定资源不够

  7. sy 系统本身的一些进程、服务,占用资源的百分比

  8. id 空闲
    us+sy+id+wa=st=100

  9. wa=wait 有多少进程在等待CPU的百分比

  10. cache/buff
    Cache:缓存区,是高速缓存,是位于CPU和主内存之间的容量较小但速度很快的存储器 Buffer:缓冲区,用于存储速度不同步的设备或优先级不同的设备之间传输数据

top 查看进程使用资源情况

  • top

3秒显示一次,动态的
第一行:和w命令第一行相同
第二行:有多少个任务,有多少个正在运行的进程,有多少个休眠的进程,有多少个停止的进程,有多少个僵尸进程zombie(主进程被意外终止的)
第三行:CPU百分比,平时需要多关注us,如果us很高,系统负载将会特别大。st表示被偷走的cpu,如果服务器做了虚拟化,下面有一些虚拟机,就会偷走一些cpu。
第四行:KiB Mem表示物理内存,需要关注。
第五行:KiB Swap表示交换分区

下面默认按照%CPU排序,占用高的在上。
RES:物理内存大小,单位是字节。
PID:进程号,可以使用它杀死进程,kill+pid号杀死进程
想要按照内存占用%MEM大小排序,按shift+m键
恢复按照%CPU排序,按shift+p键
查看所有CPU使用率,按1,可以来回切换
退出,按q

[root@linux7-128 ~]# top
top - 21:21:30 up  1:19,  1 user,  load average: 0.02, 0.07, 0.06
Tasks: 118 total,   2 running, 116 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1008176 total,   650760 free,   157184 used,   200232 buff/cache
KiB Swap:  4194300 total,  4194300 free,        0 used.   667800 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                             
 2139 root      20   0  157724   2188   1548 R   0.3  0.2   0:00.37 top                                                 
    1 root      20   0  125392   3948   2532 S   0.0  0.4   0:01.20 systemd                                             
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.02 kthreadd                                            
    3 root      20   0       0      0      0 S   0.0  0.0   0:00.04 ksoftirqd/0                                         
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H                                        
    6 root      20   0       0      0      0 S   0.0  0.0   0:00.27 kworker/u128:0                                      
    7 root      rt   0       0      0      0 S   0.0  0.0   0:00.13 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:02.63 rcu_sched                                           
   10 root      rt   0       0      0      0 S   0.0  0.0   0:00.03 watchdog/0                                          
   11 root      rt   0       0      0      0 S   0.0  0.0   0:00.04 watchdog/1                                          
   12 root      rt   0       0      0      0 S   0.0  0.0   0:00.05 migration/1                                         
   13 root      20   0       0      0      0 S   0.0  0.0   0:00.08 ksoftirqd/1                                         
   15 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0H    
  • top -c

查卡到具体的进程命令,全局的路径,看的更详细

[root@linux7-128 ~]# top -c
top - 21:41:48 up  1:39,  1 user,  load average: 0.01, 0.03, 0.05
Tasks: 116 total,   1 running, 115 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.3 sy,  0.0 ni, 99.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1008176 total,   651060 free,   156796 used,   200320 buff/cache
KiB Swap:  4194300 total,  4194300 free,        0 used.   668116 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                             
  276 root      20   0       0      0      0 S   0.3  0.0   0:04.24 [kworker/1:2]                                       
 2385 root      20   0  157756   2276   1636 R   0.3  0.2   0:00.12 top -c                                              
    1 root      20   0  125392   3948   2532 S   0.0  0.4   0:01.21 /usr/lib/systemd/systemd --switched-root --system -+
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.02 [kthreadd]                                          
    3 root      20   0       0      0      0 S   0.0  0.0   0:00.05 [ksoftirqd/0]                                       
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 [kworker/0:0H]                                      
    6 root      20   0       0      0      0 S   0.0  0.0   0:00.27 [kworker/u128:0]                                    
    7 root      rt   0       0      0      0 S   0.0  0.0   0:00.14 [migration/0]                                       
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 [rcu_bh]                    
.................
  • top -bn1
    静态的显示,一次性的把所有的信息全部输出出来,适合在写脚本时用。
[root@linux7-128 ~]# top -bn1
top - 21:43:39 up  1:41,  1 user,  load average: 0.00, 0.02, 0.05
Tasks: 116 total,   1 running, 115 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  3.2 sy,  0.0 ni, 96.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1008176 total,   651908 free,   156008 used,   200260 buff/cache
KiB Swap:  4194300 total,  4194300 free,        0 used.   668964 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0  125392   3948   2532 S   0.0  0.4   0:01.21 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.02 kthreadd
    3 root      20   0       0      0      0 S   0.0  0.0   0:00.05 ksoftirqd/0
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H
    6 root      20   0       0      0      0 S   0.0  0.0   0:00.27 kworker/u128:0
    7 root      rt   0       0      0      0 S   0.0  0.0   0:00.14 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:03.38 rcu_sched
   10 root      rt   0       0      0      0 S   0.0  0.0   0:00.04 watchdog/0
   11 root      rt   0       0      0      0 S   0.0  0.0   0:00.04 watchdog/1
   12 root      rt   0       0      0      0 S   0.0  0.0   0:00.05 migration/1
   13 root      20   0       0      0      0 S   0.0  0.0   0:00.14 ksoftirqd/1
   15 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0H
   17 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kdevtmpfs
   18 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 netns
   19 root      20   0       0      0      0 S   0.0  0.0   0:00.00 khungtaskd
   20 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 writeback
   21 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kintegrityd
.......................

sar

Linux系统里的瑞士军刀

从多方面对系统的活动进行报告,包括:文件的读写情况、系统调用的使用情况、磁盘I/O、CPU效率、内存使用状况、进程活动及IPC有关的活动等

[root@linux7-128 ~]# sar
Linux 3.10.0-693.el7.x86_64 (linux7-128) 	2018年06月07日 	_x86_64_	(2 CPU)

20时02分45秒       LINUX RESTART

20时10分01秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
20时20分01秒     all      0.03      0.00      0.10      0.04      0.00     99.84
20时30分01秒     all      0.03      0.00      0.07      0.00      0.00     99.90
20时40分01秒     all      0.03      0.00      0.07      0.02      0.00     99.88
20时50分01秒     all      0.03      0.00      0.07      0.00      0.00     99.90
21时00分01秒     all      0.04      0.00      0.08      0.00      0.00     99.88
21时10分01秒     all      0.04      0.00      0.08      0.01      0.00     99.87
21时20分01秒     all      0.04      0.01      0.24      0.16      0.00     99.55
21时30分01秒     all      0.05      0.00      0.12      0.00      0.00     99.83
21时40分01秒     all      0.05      0.00      0.13      0.00      0.00     99.82
21时50分01秒     all      0.04      0.00      0.09      0.01      0.00     99.87
22时00分01秒     all      0.03      0.00      0.07      0.00      0.00     99.90
22时10分01秒     all      0.03      0.00      0.08      0.00      0.00     99.89
平均时间:     all      0.03      0.00      0.10      0.02      0.00     99.84

  • 安装

如果没有sar命令可以安装sysstat
yum install -y sysstat

  • /var/log/sa

sar命令如果不加具体的选项,默认会调用系统里保留的一个历史文件
/var/log/sa sar生成的历史文件所在的目录,sar每隔十分钟会把系统的状态过滤一遍,保存在文件里,存在此目录下。

** sa07 是二进制文件,需要使用sar -f 查看
sar07 是文本文件,可以直接使用cat命令查看 **

  • sar -n DEV查看网卡流量
[root@linux7-128 ~]# sar -n DEV 1 2
Linux 3.10.0-693.el7.x86_64 (linux7-128) 	2018年06月07日 	_x86_64_	(2 CPU)

22时03分13秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
22时03分14秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22时03分14秒 virbr0-nic      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22时03分14秒    virbr0      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22时03分14秒     ens33      3.00      1.00      0.18      0.19      0.00      0.00      0.00

22时03分14秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
22时03分15秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22时03分15秒 virbr0-nic      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22时03分15秒    virbr0      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22时03分15秒     ens33      3.00      2.00      0.18      0.64      0.00      0.00      0.00

平均时间:     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
平均时间:        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均时间: virbr0-nic      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均时间:    virbr0      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均时间:     ens33      3.00      1.50      0.18      0.42      0.00      0.00      0.00

用法和vmstat类似,后面跟1 2 表示每隔1秒显示一次,显示2次

第一列:时间
第二列:IFACE 网卡名字
第三列:rxpck/s 接受到的数据包,单位个数,几千正常,上万就不正常
第四列:txpck/s 发送出去的数据包
第五列:rxkB/s 接收的数据量,单位KB
第六列:txkB/s 发送的数据量
第七列:rxcmp/s 不需要关注
第八列:txcmp/s 不需要关注
第九列:rxmcst/s 不需要关注

  • sar -n DEV -f /var/log/sa/sa06
    查看前一天的历史数据

/var/log/sa里保持的文件最多为1个月,所以文件为sa01-sa31

  • sar -q 查看系统负载
[root@linux7-128 ~]# sar -q
Linux 3.10.0-693.el7.x86_64 (linux7-128) 	2018年06月07日 	_x86_64_	(2 CPU)

20时02分45秒       LINUX RESTART

20时10分01秒   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
20时20分01秒         0       158      0.00      0.03      0.11         0
20时30分01秒         0       157      0.00      0.01      0.06         0
20时40分01秒         0       157      0.00      0.01      0.05         0
20时50分01秒         1       157      0.00      0.01      0.05         0
21时00分01秒         0       157      0.00      0.01      0.05         0
21时10分01秒         0       158      0.00      0.01      0.05         0
21时20分01秒         0       159      0.11      0.10      0.07         0
21时30分01秒         0       159      0.00      0.01      0.05         0
21时40分01秒         0       158      0.07      0.04      0.05         0
21时50分01秒         0       158      0.00      0.01      0.05         0
22时00分01秒         0       157      0.00      0.01      0.05         0
22时10分01秒         0       157      0.00      0.01      0.05         0
22时20分01秒         0       158      0.00      0.01      0.05         0
平均时间:         0       158      0.01      0.02      0.06         0


如果需要查看6号的
sar -q -f /var/log/sa/sa06

  • sar -b 查看磁盘读写
[root@linux7-128 ~]# sar -b
Linux 3.10.0-693.el7.x86_64 (linux7-128) 	2018年06月07日 	_x86_64_	(2 CPU)

20时02分45秒       LINUX RESTART

20时10分01秒       tps      rtps      wtps   bread/s   bwrtn/s
20时20分01秒      0.18      0.05      0.13      6.00      2.31
20时30分01秒      0.06      0.00      0.06      0.00      0.79
20时40分01秒      0.07      0.00      0.06      0.15      0.84
20时50分01秒      0.06      0.00      0.06      0.00      0.80
21时00分01秒      0.05      0.00      0.05      0.00      0.72
21时10分01秒      0.21      0.02      0.19      0.32      2.94
21时20分01秒      5.47      4.79      0.67    134.78     28.59
21时30分01秒      0.03      0.00      0.03      0.00      0.33
21时40分01秒      0.07      0.00      0.07      0.00      0.80
21时50分01秒      0.09      0.00      0.09      0.01      1.24
22时00分01秒      0.08      0.01      0.08      0.37      1.09
22时10分01秒      0.07      0.00      0.07      0.00      0.94
22时20分01秒      0.09      0.02      0.07      0.67      0.88
平均时间:      0.50      0.38      0.13     10.95      3.25

nload命令

  • 安装
    默认没有安装,使用yum install -y nload

  • nload
    动态的显示网卡实时速度的一个页面

Incoming 进来带宽
Outgoing 出去带宽
curr 当前值
Avg 平均值
Min 最小值
Max 最大值
Ttl

第一行显示网卡名字、IP、网卡其中之一,可以按右方向键切换网卡,按q退出

监控io性能

iostat

  • 安装
    在安装sysstat包时,默认会安装iostat包,和sar在同一个包里

  • iostat

[root@linux7-128 ~]# iostat 
Linux 3.10.0-693.el7.x86_64 (linux7-128) 	2018年06月08日 	_x86_64_	(2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.31    0.00    0.52    2.42    0.00   96.75

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               9.31       208.36        70.19     277206      93380
sdb               0.19         2.75         0.00       3664          0
dm-0              0.03         0.78         0.00       1036          0
  • iostat 1 2
    用法和vmstat用法类似,每一秒执行一次,执行2次

  • iostat -x

[root@linux7-128 ~]# iostat -x 
Linux 3.10.0-693.el7.x86_64 (linux7-128) 	2018年06月08日 	_x86_64_	(2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.21    0.00    0.37    1.55    0.00   97.88

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.01     0.11    4.54    1.43   133.33    45.03    59.70     0.20   33.53   23.41   65.58   5.67   3.39
sdb               0.00     0.00    0.12    0.00     1.76     0.00    28.62     0.00    4.16    4.16    0.00   3.02   0.04
dm-0              0.00     0.00    0.02    0.00     0.50     0.00    48.19     0.00    5.95    5.95    0.00   5.88   0.01

**重要指标:%util **
表示io等待,就是说磁盘使用有多少时间,占用CPU的
cpu有一部分时间是给进程处理计算的,也有一部分时间是要等待io的,等待磁盘读写,这个等待的时间比

如果数字很大,50%,说明磁盘非常忙,读写肯定也会很大。如果读写不大,时间比很大,就说明硬盘有问题

iotop

监控磁盘读写状况

  • 安装
    yum -y install iotop

  • iotop

和top很像,动态显示

free命令

查看内存使用情况

  • free
[root@linux7-128 ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1008176      120352      724020        7228      163804      719616
Swap:       4194300           0     4194300

centos6和centos7显示的内容是不一样的

7更加直观,第一行是说明,第二行是内存使用情况,第三行是swap使用情况

我们主要关注第二行,内存使用情况

第一列:total 内存总大小 ,单位KB
第二列:used 内存已用大小
第三列:free 内存剩余大小
第四列:shared 共享大小
第五列:buff/cache 缓冲/缓存

0000(磁盘) --》内存(cache)--》CPU
cpu(00000) --》内存(buff)--》磁盘

正常情况下内存总大小=已有+剩余,这里是并不对等的,因为在Linux系统里,会有一部分空间给buff/cache
第六列:available free剩余内存+buff/cache剩余的

公式:total=used+free+buff/cache

指定单位为M时,使用free -m,指定单位为G时,使用free -g或者free -h在后面显示单位

  • free- h
[root@linux7-128 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           984M        117M        706M        7.1M        160M        702M
Swap:          4.0G          0B        4.0G

ps命令

查看系统进程

ps aux

静态的,一次性把系统当前所有进程列出来

[root@linux7-128 ~]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.3 125376  3936 ?        Ss   21:50   0:01 /usr/lib/systemd/systemd --switched-root --system --dese
root         2  0.0  0.0      0     0 ?        S    21:50   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    21:50   0:00 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   21:50   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S    21:50   0:00 [migration/0]
root         8  0.0  0.0      0     0 ?        S    21:50   0:00 [rcu_bh]
root         9  0.0  0.0      0     0 ?        S    21:50   0:01 [rcu_sched]
root        10  0.0  0.0      0     0 ?        S    21:50   0:00 [watchdog/0]
root        11  0.0  0.0      0     0 ?        S    21:50   0:00 [watchdog/1]
root        12  0.0  0.0      0     0 ?        S    21:50   0:00 [migration/1]
............
  • USER 用户

  • PID 进程ID ,杀死进程时可以用到kill PID
    有时候这样并不能终止进程,需要加-9选项,即kill -9 PID,但这样有点暴力,严重时会丢失数据,尽量不要使用。

  • STAT 进程状态,分为以下几种

    D    不可中断     Uninterruptible sleep (usually IO)
    R    正在运行,或在队列中的进程
    S    处于休眠状态
    T    停止或被追踪
    Z    僵尸进程
    W    进入内存交换(从内核2.6开始无效)
    X    死掉的进程


    <    高优先级
    N    低优先级
    L    有些页被锁进内存
    s    包含子进程
    +    在前台运行的进程,比如在当前终端执行ps aux就是前台进程
    l    多线程,克隆线程  multi-threaded (using CLONE_THREAD, like NPTL pthreads do)

查看进程所在目录
ls -l /proc/进程的PID/


[root@linux7-128 ~]# ls -l /proc/502/
ls: 无法读取符号链接/proc/502/exe: 没有那个文件或目录
总用量 0
dr-xr-xr-x 2 root root 0 6月  11 10:35 attr
-rw-r--r-- 1 root root 0 6月  11 10:35 autogroup
-r-------- 1 root root 0 6月  11 10:35 auxv
-r--r--r-- 1 root root 0 6月  11 10:35 cgroup
--w------- 1 root root 0 6月  11 10:35 clear_refs
-r--r--r-- 1 root root 0 6月  11 10:30 cmdline
-rw-r--r-- 1 root root 0 6月  11 10:35 comm
-rw-r--r-- 1 root root 0 6月  11 10:35 coredump_filter
-r--r--r-- 1 root root 0 6月  11 10:35 cpuset
lrwxrwxrwx 1 root root 0 6月  11 10:35 cwd -> /
-r-------- 1 root root 0 6月  11 10:35 environ
lrwxrwxrwx 1 root root 0 6月  11 10:30 exe
dr-x------ 2 root root 0 6月  11 10:35 fd
dr-x------ 2 root root 0 6月  11 10:35 fdinfo
-rw-r--r-- 1 root root 0 6月  11 10:35 gid_map
-r-------- 1 root root 0 6月  11 10:35 io
-r--r--r-- 1 root root 0 6月  11 10:35 limits
-rw-r--r-- 1 root root 0 6月  11 10:35 loginuid
dr-x------ 2 root root 0 6月  11 10:35 map_files
-r--r--r-- 1 root root 0 6月  11 10:35 maps
-rw------- 1 root root 0 6月  11 10:35 mem
-r--r--r-- 1 root root 0 6月  11 10:35 mountinfo
-r--r--r-- 1 root root 0 6月  11 10:35 mounts
-r-------- 1 root root 0 6月  11 10:35 mountstats
dr-xr-xr-x 6 root root 0 6月  11 10:35 net
dr-x--x--x 2 root root 0 6月  11 10:35 ns
-r--r--r-- 1 root root 0 6月  11 10:35 numa_maps
-rw-r--r-- 1 root root 0 6月  11 10:35 oom_adj
-r--r--r-- 1 root root 0 6月  11 10:35 oom_score
-rw-r--r-- 1 root root 0 6月  11 10:35 oom_score_adj
-r--r--r-- 1 root root 0 6月  11 10:35 pagemap
-r--r--r-- 1 root root 0 6月  11 10:35 personality
-rw-r--r-- 1 root root 0 6月  11 10:35 projid_map
lrwxrwxrwx 1 root root 0 6月  11 10:35 root -> /
-rw-r--r-- 1 root root 0 6月  11 10:35 sched
-r--r--r-- 1 root root 0 6月  11 10:35 schedstat
-r--r--r-- 1 root root 0 6月  11 10:35 sessionid
-rw-r--r-- 1 root root 0 6月  11 10:35 setgroups
-r--r--r-- 1 root root 0 6月  11 10:35 smaps
-r--r--r-- 1 root root 0 6月  11 10:35 stack
-r--r--r-- 1 root root 0 6月  11 10:30 stat
-r--r--r-- 1 root root 0 6月  11 10:35 statm
-r--r--r-- 1 root root 0 6月  11 10:30 status
-r--r--r-- 1 root root 0 6月  11 10:35 syscall
dr-xr-xr-x 3 root root 0 6月  11 10:35 task
-r--r--r-- 1 root root 0 6月  11 10:35 timers
-rw-r--r-- 1 root root 0 6月  11 10:35 uid_map
-r--r--r-- 1 root root 0 6月  11 10:35 wchan

  • ps aux |grep 进程名称
    检查某一个进程有没有运行
[root@linux7-128 ~]# ps aux |grep nginx
root      1867  0.0  0.0 112720   984 pts/0    S+   22:51   0:00 grep --color=auto nginx
[root@linux7-128 ~]# ps aux |grep httpd
root      1878  0.0  0.0 112720   984 pts/0    S+   22:51   0:00 grep --color=auto httpd
[root@linux7-128 ~]# ps aux |grep mysql
root      1887  0.0  0.0 112720   984 pts/0    R+   22:51   0:00 grep --color=auto mysql

ps -elf

类似于ps aux,把系统进程罗列出来

[root@linux7-128 ~]# 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 - 31344 ep_pol 21:50 ?        00:00:01 /usr/lib/systemd/systemd --switched-root --sys
1 S root         2     0  0  80   0 -     0 kthrea 21:50 ?        00:00:00 [kthreadd]
1 S root         3     2  0  80   0 -     0 smpboo 21:50 ?        00:00:00 [ksoftirqd/0]
1 S root         5     2  0  60 -20 -     0 worker 21:50 ?        00:00:00 [kworker/0:0H]
1 S root         7     2  0 -40   - -     0 smpboo 21:50 ?        00:00:00 [migration/0]
1 S root         8     2  0  80   0 -     0 rcu_gp 21:50 ?        00:00:00 [rcu_bh]
1 S root         9     2  0  80   0 -     0 rcu_gp 21:50 ?        00:00:01 [rcu_sched]
5 S root        10     2  0 -40   - -     0 smpboo 21:50 ?        00:00:00 [watchdog/0]
5 S root        11     2  0 -40   - -     0 smpboo 21:50 ?        00:00:00 [watchdog/1]
1 S root        12     2  0 -40   - -     0 smpboo 21:50 ?        00:00:00 [migration/1]
1 S root        13     2  0  80   0 -     0 smpboo 21:50 ?        00:00:00 [ksoftirqd/1]
1 S root        15     2  0  60 -20 -     0 worker 21:50 ?        00:00:00 [kworker/1:0H]
5 S root        17     2  0  80   0 -     0 devtmp 21:50 ?        00:00:00 [kdevtmpfs]
1 S root        18     2  0  60 -20 -     0 rescue 21:50 ?        00:00:00 [netns]
............

###进程和线程区别
1.简要了解一下进程和线程
对于操作系统而言,进程是核心之核心,整个现代操作系统的根本,就是以进程为单位在执行任务。系统的管理架构也是基于进程层面的。在按下电源键之后,计算机就开始了复杂的启动过程,此处有一个经典问题:当按下电源键之后,计算机如何把自己由静止启动起来的?本文不讨论系统启动过程,请读者自行科普。操作系统启动的过程简直可以描述为上帝创造万物的过程,期初没有世界,但是有上帝,是上帝创造了世界,之后创造了万物,然后再创造了人,然后塑造了人的七情六欲,再然后人类社会开始遵循自然规律繁衍生息。。。操作系统启动进程的阶段就相当于上帝造人的阶段。本文讨论的全部内容都是“上帝造人”之后的事情。第一个被创造出来的进程是0号进程,这个进程在操作系统层面是不可见的,但它存在着。0号进程完成了操作系统的功能加载与初期设定,然后它创造了1号进程(init),这个1号进程就是操作系统的“耶稣”。1号进程是上帝派来管理整个操作系统的,所以在用pstree查看进程树可知,1号进程位于树根。再之后,系统的很多管理程序都以进程身份被1号进程创造出来,还创造了与人类沟通的桥梁——shell。从那之后,人类可以跟操作系统进行交流,可以编写程序,可以执行任务。。。

而这一切,都是基于进程的。每一个任务(进程)被创建时,系统会为他分配存储空间等必要资源,然后在内核管理区为该进程创建管理节点,以便后来控制和调度该任务的执行。

进程真正进入执行阶段,还需要获得CPU的使用权,这一切都是操作系统掌管着,也就是所谓的调度,在各种条件满足(资源与CPU使用权均获得)的情况下,启动进程的执行过程。

除CPU而外,一个很重要的资源就是存储器了,系统会为每个进程分配独有的存储空间,当然包括它特别需要的别的资源,比如写入时外部设备是可使用状态等等。有了上面的引入,我们可以对进程做一个简要的总结:
进程,是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。它的执行需要系统分配资源创建实体之后,才能进行。
随着技术发展,在执行一些细小任务时,本身无需分配单独资源时(多个任务共享同一组资源即可,比如所有子进程共享父进程的资源),进程的实现机制依然会繁琐的将资源分割,这样造成浪费,而且还消耗时间。后来就有了专门的多任务技术被创造出来——线程。

线程的特点就是在不需要独立资源的情况下就可以运行。如此一来会极大节省资源开销,以及处理时间。
2.进程和线程的区别
1).二者的相同点
无论是进程还是线程,对于程序员而言,都是用来实现多任务并发的技术手段。二者都可以独立调度,因此在多任务环境下,功能上并无差异。并且二者都具有各自的实体,是系统独立管理的对象个体。所以在系统层面,都可以通过技术手段实现二者的控制。而且二者所具有的状态都非常相似。而且,在多任务程序中,子进程(子线程)的调度一般与父进程(父线程)平等竞争。

其实在Linux内核2.4版以前,线程的实现和管理方式就是完全按照进程方式实现的。在2.6版内核以后才有了单独的线程实现。
进程状态图
线程状态图
2).实现方式的差异
进程是资源分配的基本单位,线程是调度的基本单位。
这句经典名言已流传数十年,各种操作系统教材都可见此描述。确实如此,这就是二者的显著区别。读者请注意“基本”二字。相信有读者看到前半句的时候就在心里思考,“进程岂不是不能调度?”,非也!进程和线程都可以被调度,否则多进程程序该如何运行呢!

只是,线程是更小的可以调度的单位,也就是说,只要达到线程的水平就可以被调度了,进程自然可以被调度。它强调的是分配资源时的对象必须是进程,不会给一个线程单独分配系统管理的资源。若要运行一个任务,想要获得资源,最起码得有进程,其他子任务可以以线程身份运行,资源共享就行了。
简而言之,进程的个体间是完全独立的,而线程间是彼此依存的。多进程环境中,任何一个进程的终止,不会影响到其他进程。而多线程环境中,父线程终止,全部子线程被迫终止(没有了资源)。而任何一个子线程终止一般不会影响其他线程,除非子线程执行了exit()系统调用。任何一个子线程执行exit(),全部线程同时灭亡。

其实,也没有人写出只有线程而没有进程的程序。多线程程序中至少有一个主线程,而这个主线程其实就是有main函数的进程。它是整个程序的进程,所有线程都是它的子线程。我们通常把具有多线程的主进程称之为主线程。
参考博客:https://my.oschina.net/cnyinlinux/blog/422207

netstat 查看网络状态

查看tcp/ip通信状态,任何情况下,机器本身是没有监听 端口的

  • netstat -lnp 查看监听端口
[root@linux7-128 ~]# 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:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1134/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      902/sshd            
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      899/cupsd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1004/master         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      902/sshd            
tcp6       0      0 ::1:631                 :::*                    LISTEN      899/cupsd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1004/master         
udp        0      0 192.168.122.1:53        0.0.0.0:*                           1134/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           1134/dnsmasq        
udp        0      0 0.0.0.0:60036           0.0.0.0:*                           588/avahi-daemon: r 
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           588/avahi-daemon: r 
udp        0      0 127.0.0.1:323           0.0.0.0:*                           637/chronyd         
udp6       0      0 ::1:323                 :::*                                637/chronyd         
raw6       0      0 :::58                   :::*                    7           628/NetworkManager  
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     21593    1004/master          public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     21596    1004/master          public/qmgr
unix  2      [ ACC ]     STREAM     LISTENING     21615    1004/master          private/verify
unix  2      [ ACC ]     STREAM     LISTENING     21612    1004/master          private/trace
unix  2      [ ACC ]     STREAM     LISTENING     16951    653/gssproxy         /var/lib/gssproxy/default.sock
.........
  • netstat -an 查看tcp/ip所有的连接状态

  • netstat -lntp 只查看tcp的监听端口

[root@linux7-128 ~]# netstat -ltnp
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:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1134/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      902/sshd            
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      899/cupsd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1004/master         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      902/sshd            
tcp6       0      0 ::1:631                 :::*                    LISTEN      899/cupsd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1004/master     
  • netstat -ltunp 查看tcp和udp的监听端口
[root@linux7-128 ~]# netstat -ltunp
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:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1134/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      902/sshd            
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      899/cupsd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1004/master         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      902/sshd            
tcp6       0      0 ::1:631                 :::*                    LISTEN      899/cupsd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1004/master         
udp        0      0 192.168.122.1:53        0.0.0.0:*                           1134/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           1134/dnsmasq        
udp        0      0 0.0.0.0:60036           0.0.0.0:*                           588/avahi-daemon: r 
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           588/avahi-daemon: r 
udp        0      0 127.0.0.1:323           0.0.0.0:*                           637/chronyd         
udp6       0      0 ::1:323                 :::*                                637/chronyd     
  • 小技巧:查看所有状态数字

netstat -an |awk '/^tcp/{++sta[$NF]}END{for(key in sta)print key,"\t",sta[key]}'

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

ESTABLISHED如果很大,说明系统很忙。并发连接

  • ss -an 和netstat异曲同工,显示tcpip状态

ss -an |grep -i listen
查看listen的状态,不会显示进程名称

[root@linux7-128 ~]# ss -an |grep -i listen
u_str  LISTEN     0      100    public/cleanup 21593                 * 0                  
u_str  LISTEN     0      100    public/qmgr 21596                 * 0                  
u_str  LISTEN     0      100    private/verify 21615                 * 0                  
u_str  LISTEN     0      100    private/trace 21612                 * 0                  
u_str  LISTEN     0      10     /var/lib/gssproxy/default.sock 16951                 * 0                  
u_str  LISTEN     0      100    public/flush 21618                 * 0                  
u_str  LISTEN     0      100    public/showq 21633                 * 0                  
u_str  LISTEN     0      5      /var/run/lsm/ipc/sim 17735                 * 0                  
u_str  LISTEN     0      128    @ISCSID_UIP_ABSTRACT_NAMESPACE 15295                 * 0                  
u_str  LISTEN     0      100    private/bounce 21606                 * 0                  
u_str  LISTEN     0      128    /run/systemd/private 12653                 * 0                  
u_seq  LISTEN     0      128    /run/udev/control 12668                 * 0                  
u_str  LISTEN     0      100    private/tlsmgr 21600                 * 0                  
u_str  LISTEN     0      128    /run/lvm/lvmpolld.socket 12670                 * 0                  
u_str  LISTEN     0      100    private/rewrite 21603                 * 0                  
u_str  LISTEN     0      100    private/defer 21609                 * 0                  
u_str  LISTEN     0      128    /run/lvm/lvmetad.socket 12704                 * 0                  
u_str  LISTEN     0      100    private/proxymap 21621                 * 0                  
u_str  LISTEN     0      100    private/proxywrite 21624                 * 0                  
u_str  LISTEN     0      100    private/smtp 21627                 * 0                  
u_str  LISTEN     0      100    private/relay 21630                 * 0                  
u_str  LISTEN     0      100    private/error 21636                 * 0                  
u_str  LISTEN     0      100    private/retry 21639                 * 0                  
u_str  LISTEN     0      10     /var/run/abrt/abrt.socket 17580                 * 0                  
u_str  LISTEN     0      100    private/discard 21642                 * 0                  
u_str  LISTEN     0      100    private/local 21645                 * 0                  
u_str  LISTEN     0      128    /var/run/dbus/system_bus_socket 15277                 * 0                  
u_str  LISTEN     0      100    private/virtual 21648                 * 0                  
u_str  LISTEN     0      100    private/lmtp 21651                 * 0                  
u_str  LISTEN     0      128    /var/run/rpcbind.sock 15280                 * 0                  
u_str  LISTEN     0      100    private/anvil 21654                 * 0                  
u_str  LISTEN     0      100    private/scache 21657                 * 0                  
u_str  LISTEN     0      128    /var/run/libvirt/virtlogd-sock 15285                 * 0                  
u_str  LISTEN     0      128    /var/run/libvirt/virtlockd-sock 15288                 * 0                  
u_str  LISTEN     0      128    /var/run/cups/cups.sock 15290                 * 0                  
u_str  LISTEN     0      10     /run/gssproxy.sock 17595                 * 0                  
u_str  LISTEN     0      128    /var/run/avahi-daemon/socket 15292                 * 0                  
u_str  LISTEN     0      128    @ISCSIADM_ABSTRACT_NAMESPACE 15284                 * 0                  
u_str  LISTEN     0      128    /run/systemd/journal/stdout 8148                  * 0                  
u_str  LISTEN     0      32     /var/run/vmware/guestServicePipe 14297                 * 0                  
u_str  LISTEN     0      5      /var/run/lsm/ipc/simc 17376                 * 0                  
u_str  LISTEN     0      128    /var/run/libvirt/libvirt-sock 20708                 * 0                  
u_str  LISTEN     0      128    /var/run/libvirt/libvirt-sock-ro 20710                 * 0                  
u_str  LISTEN     0      20     /var/run/libvirt/libvirt-admin-sock 20712                 * 0                  
u_str  LISTEN     0      100    public/pickup 21589                 * 0                  
tcp    LISTEN     0      128       *:111                   *:*                  
tcp    LISTEN     0      5      192.168.122.1:53                    *:*                  
tcp    LISTEN     0      128       *:22                    *:*                  
tcp    LISTEN     0      128    127.0.0.1:631                   *:*                  
tcp    LISTEN     0      100    127.0.0.1:25                    *:*                  
tcp    LISTEN     0      128      :::111                  :::*                  
tcp    LISTEN     0      128      :::22                   :::*                  
tcp    LISTEN     0      128     ::1:631                  :::*                  
tcp    LISTEN     0      100     ::1:25                   :::* 

linux下抓包

tcpdump工具

安装

yum -y install tcpdump

使用

  • tcpdump选项
    tcpdump采用命令行方式对接口的数据包进行筛选抓取,其丰富特性表现在灵活的表达式上。
    不带任何选项的tcpdump,默认会抓取第一个网络接口,且只有将tcpdump进程终止才会停止抓包。
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:从给定的数据包文件中读取数据。使用"-"表示从标准输入中读取。

所以常用的选项也就这几个:

  • tcpdump -nn
  • tcpdump -nn -i 网卡名字
    显示主机IP地址
12:43:06.864383 IP 192.168.220.128.22 > 192.168.220.1.52562: Flags [P.], seq 300812:301024, ack 53, win 277, options [nop,nop,TS val 7744426 ecr 1739641], length 212
12:43:06.864821 IP 192.168.220.1.52562 > 192.168.220.128.22: Flags [.], ack 301024, win 238, options [nop,nop,TS val 1739642 ecr 7744425], length 0

相互流向

  • tcpdump -i 网卡名字
    显示主机名字

  • tcpdump -nn port 80
    查看80端口

  • tcpdump -nn not port 22 and host 192.168.0.100
    不要22端口并且只要192.168.0.100的包

  • tcpdump -nn -c 100 -w /tmp/1.cap
    指定100个数据包并且存放到1.cap里去

[root@linux7-128 ~]# tcpdump -i ens33 -c 20 -w /tmp/1.cap
tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
20 packets captured
20 packets received by filter
0 packets dropped by kernel

要有数据产生才能抓包,可以再打开一个终端产生一些数据,例如执行top命令、vmstat命令,过一会就会抓取完。

查看1.cap信息,不能直接使用cat查看

[root@linux7-128 ~]# file /tmp/1.cap 
/tmp/1.cap: tcpdump capture file (little-endian) - version 2.4 (Ethernet, capture length 262144)

使用tcpdump -r /tmp/1.cap查看

[root@linux7-128 ~]# tcpdump -r /tmp/1.cap
reading from file /tmp/1.cap, link-type EN10MB (Ethernet)
12:50:48.682743 IP linux7-128.ssh > 192.168.220.1.52562: Flags [P.], seq 1589167410:1589167542, ack 3436632416, win 277, options [nop,nop,TS val 8206244 ecr 1785816], length 132
12:50:48.683013 IP 192.168.220.1.52562 > linux7-128.ssh: Flags [.], ack 132, win 260, options [nop,nop,TS val 1785821 ecr 8206205], length 0
12:50:53.185900 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:50:54.055727 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:50:55.055839 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:50:56.183013 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:50:57.055982 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:50:58.056147 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:50:59.183207 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:51:00.056807 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:51:01.056772 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:51:11.184360 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:51:12.056372 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:51:13.056519 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:51:14.184674 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:51:15.056597 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:51:16.057852 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:51:17.185387 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:51:18.056899 ARP, Request who-has gateway tell 192.168.220.1, length 46
12:51:19.057020 ARP, Request who-has gateway tell 192.168.220.1, length 46

参考: http://blog.chinaunix.net/uid-29792372-id-5048174.html

tshark

安装

yum -y install wireshark

使用

  • 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"

查看指定网卡80端口的web访问情况

Linux网络相关

  • ifconfig查看网卡ip(yum install net-tools)
[root@linux7-128 ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.220.128  netmask 255.255.255.0  broadcast 192.168.220.255
        inet6 fe80::c6d3:ed0a:87b0:ac8e  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:61:da:b9  txqueuelen 1000  (Ethernet)
        RX packets 24436  bytes 22369078 (21.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 19718  bytes 4767520 (4.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 40  bytes 3152 (3.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 40  bytes 3152 (3.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:e9:d7:c4  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • ifcongfig -a
    当网卡down掉的时候,一般不会显示,加上-a会全部显示出来

  • ifup ens33/ifdown ens33
    启用/关闭网卡

不要在Xshell里使用,在虚拟机里使用。一般在对网卡进行更改后使用,例如添加网关。

也可以一起使用 ifdown ens33 && ifup ens33

[root@linux7-128 ~]# ifdown ens33 &&  ifup ens33
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
  • 设定虚拟网卡

1.首先在网卡配置文件目录里复制一个ens33的配置文件

[root@linux7-128 ~]# cd /etc/sysconfig/network-scripts/
[root@linux7-128 network-scripts]# ls
ifcfg-ens33  ifdown-ippp    ifdown-sit       ifup-bnep  ifup-plip    ifup-Team          network-functions-ipv6
ifcfg-lo     ifdown-ipv6    ifdown-Team      ifup-eth   ifup-plusb   ifup-TeamPort
ifdown       ifdown-isdn    ifdown-TeamPort  ifup-ib    ifup-post    ifup-tunnel
ifdown-bnep  ifdown-post    ifdown-tunnel    ifup-ippp  ifup-ppp     ifup-wireless
ifdown-eth   ifdown-ppp     ifup             ifup-ipv6  ifup-routes  init.ipv6-global
ifdown-ib    ifdown-routes  ifup-aliases     ifup-isdn  ifup-sit     network-functions
[root@linux7-128 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:0
[root@linux7-128 network-scripts]# ls
ifcfg-ens33    ifdown-ib    ifdown-routes    ifup-aliases  ifup-isdn    ifup-sit          network-functions
ifcfg-ens33:0  ifdown-ippp  ifdown-sit       ifup-bnep     ifup-plip    ifup-Team         network-functions-ipv6
ifcfg-lo       ifdown-ipv6  ifdown-Team      ifup-eth      ifup-plusb   ifup-TeamPort
ifdown         ifdown-isdn  ifdown-TeamPort  ifup-ib       ifup-post    ifup-tunnel
ifdown-bnep    ifdown-post  ifdown-tunnel    ifup-ippp     ifup-ppp     ifup-wireless
ifdown-eth     ifdown-ppp   ifup             ifup-ipv6     ifup-routes  init.ipv6-global

2.编辑虚拟网卡配置文件

[root@linux7-128 network-scripts]# vi ifcfg-ens33:0

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33:0
UUID=28bb0a23-c7ac-4b6f-b019-f4c9ad70711b
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.220.122
NETMASK=255.255.255.0
GATEWAY=192.168.220.2
~                                                                                                                        
~                                                                                                                        
-- INSERT --

3.断开重新连接下网卡

[root@linux7-128 network-scripts]# ifdown ens33 &&  ifup ens33
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/5)

4.查看网卡

[root@linux7-128 network-scripts]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.220.128  netmask 255.255.255.0  broadcast 192.168.220.255
        inet6 fe80::c6d3:ed0a:87b0:ac8e  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:61:da:b9  txqueuelen 1000  (Ethernet)
        RX packets 25096  bytes 22425315 (21.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 20106  bytes 4822414 (4.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.220.122  netmask 255.255.255.0  broadcast 192.168.220.255
        ether 00:0c:29:61:da:b9  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 40  bytes 3152 (3.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 40  bytes 3152 (3.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • mii-tool ens33 查看网卡是否连接
[root@linux7-128 network-scripts]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok
  • ethtool ens33 也可以查看网卡是否连接
[root@linux7-128 network-scripts]# ethtool ens33
Settings for ens33:
	Supported ports: [ TP ]
	Supported link modes:   10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Full 
	Supported pause frame use: No
	Supports auto-negotiation: Yes
	Advertised link modes:  10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Full 
	Advertised pause frame use: No
	Advertised auto-negotiation: Yes
	Speed: 1000Mb/s
	Duplex: Full
	Port: Twisted Pair
	PHYAD: 0
	Transceiver: internal
	Auto-negotiation: on
	MDI-X: off (auto)
	Supports Wake-on: d
	Wake-on: d
	Current message level: 0x00000007 (7)
			       drv probe link
	Link detected: yes
  • 更改主机名 hostnamectl set-hostname aminglinux

centos6不支持此命令,想要生效需要退出重新登录一下
查看主机名配置文件

[root@linux7-128 ~]# cat /etc/hostname 
linux7-128
  • DNS配置文件/etc/resolv.conf
[root@linux7-128 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 119.29.29.29

这是由网卡配置文件定义的,也可以临时编辑文件设置dns2,但是重启后依然会被网卡里的配置文件覆盖

  • /etc/hosts文件
[root@linux7-128 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

在Windows和Linux下都有此文件,后续将lamp时去访问自定义域名时,就会用到此文件

假设我们ping一个域名,会解析出他的公网IP,但是如果我们在hosts文件里配置此域名,就会解析到我们配置的IP上

[root@linux7-128 ~]# ping www.qq.com
PING www.qq.com (123.151.148.111) 56(84) bytes of data.
64 bytes from 123.151.148.111 (123.151.148.111): icmp_seq=1 ttl=128 time=38.5 ms
64 bytes from 123.151.148.111 (123.151.148.111): icmp_seq=2 ttl=128 time=38.6 ms
64 bytes from 123.151.148.111 (123.151.148.111): icmp_seq=3 ttl=128 time=42.7 ms
-编辑hosts文件-
[root@linux7-128 ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.220.66 www.qq.com
-重新ping-
[root@linux7-128 ~]# ping www.qq.com
PING www.qq.com (192.168.220.66) 56(84) bytes of data.
From linux7-128 (192.168.220.128) icmp_seq=1 Destination Host Unreachable
From linux7-128 (192.168.220.128) icmp_seq=2 Destination Host Unreachable
From linux7-128 (192.168.220.128) icmp_seq=3 Destination Host Unreachable
From linux7-128 (192.168.220.128) icmp_seq=4 Destination Host Unreachable

注意:
同一个域名也可以绑定多个IP,但是只以最后一次生效。

拓展

三次握手与四次挥手分别对应TCP连接建立过程与断开过程,先上TCP报文格式:

三次握手过程:

问题1: 为什么要三次握手?

答:三次握手的目的是建立可靠的通信信道,说到通讯,简单来说就是数据的发送与接收,而三次握手最主要的目的就是双方确认自己与对方的发送与接收机能正常。

    第一次握手:Client什么都不能确认;Server确认了对方发送正常

    第二次握手:Client确认了:自己发送、接收正常,对方发送、接收正常;Server确认了:自己接收正常,对方发送正常

    第三次握手:Client确认了:自己发送、接收正常,对方发送、接收正常;Server确认了:自己发送、接收正常,对方发送接收正常

所以三次握手就能确认双发收发功能都正常,缺一不可。

问题2:为什么要发送特定的数据包,随便发不行吗?

答:三次握手的另外一个目的就是确认双方都支持TCP,告知对方用TCP传输。

    第一次握手:Server 猜测Client可能要建立TCP请求,但不确定,因为也可能是Client乱发了一个数据包给自己

    第二次握手:通过ack=J+1,Client知道Server是支持TCP的,且理解了自己要建立TCP连接的意图

    第三次握手:通过ack=K+1,Server知道Client是支持TCP的,且确实是要建立TCP连接

问题3:上图中的SYN和ACK是什么?

答:SYN是标志位,SYN=1表示请求连接;

    ACK其实就是ack后面加上的那个数,真正发送的时候不单独发ACK,只发ack,下面四次挥手的图同理

四次挥手:

问题1: 为什么要四次挥手?

答:根本原因是,一方发送FIN只表示自己发完了所有要发的数据,但还允许对方继续把没发完的数据发过来。

    举个例子:A和B打电话,通话即将结束后,A说“我没啥要说的了”,B回答“我知道了”,但是B可能还会有要说的话,A不能要求B跟着自己的节奏结束通话,于是B可能又巴拉巴拉说了一通,最后B说“我说完了”,A回答“知道了”,这样通话才算结束。

问题2:为什么双方要发送这样的数据包?

答:和握手的情况类似,只是为了让对方知晓自己理解了对方的意图。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值