運維經常使用命令基礎(一)

第十三課 運維基礎(一)html

目錄前端

1、使用w查看系統負載
2、vmstat命令
3、top命令
4、sar命令
5、nload命令
6、監控io性能
7、free命令
8、ps命令
9、查看網絡狀態
10、linux下抓包
11、Linux網絡相關
12、擴展node


1、使用w命令查看系統負載

w命令顯示當前機器上登陸的用戶以及他們的進程信息。單獨執行w命令顯示全部的用戶,也能夠指定用戶名稱,僅顯示該用戶的相關信息。python

[root@lanquark demo]# w
 21:57:07 up 51 min,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    10.0.1.229       21:08    3.00s  0.46s  0.01s w
root     pts/1    10.0.1.229       21:33   21:47   0.04s  0.04s -bash

第一行從左至右依次顯示的信息爲:mysql

系統當前時間;系統已經運行的時間;當前有多少用戶登陸;系統在過去1分鐘,5分鐘,15鍾內的平均負載linux

若是平均負載的值超過了邏輯cpu數量,那麼就須要注意了。ios

cpu的信息可經過/proc/cpuinfo文件查詢web

[root@lanquark demo]# cat /proc/cpuinfo 
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 42
model name  : Intel(R) Pentium(R) CPU G840 @ 2.80GHz
stepping    : 7
microcode   : 0x29
cpu MHz     : 2793.225
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 pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer xsave hypervisor lahf_lm epb tpr_shadow vnmi ept vpid tsc_adjust dtherm arat pln pts
bogomips    : 5587.33
clflush size    : 64
cache_alignment : 64
address sizes   : 42 bits physical, 48 bits virtual
power management:

processor   : 1
vendor_id   : GenuineIntel
cpu family  : 6
model       : 42
model name  : Intel(R) Pentium(R) CPU G840 @ 2.80GHz
stepping    : 7
microcode   : 0x29
cpu MHz     : 2793.225
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 pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer xsave hypervisor lahf_lm epb tpr_shadow vnmi ept vpid tsc_adjust dtherm arat pln pts
bogomips    : 5587.33
clflush size    : 64
cache_alignment : 64
address sizes   : 42 bits physical, 48 bits virtual
power management:

邏輯cpu的數量統計算法

[root@lanquark demo]# grep -c 'model name' /proc/cpuinfo 
2

第二欄的從左至右依次顯示的信息爲:登陸用戶名;登陸的終端名稱;登陸的遠程主機名,登陸時間,空閒時間,該終端下全部進程的cpu時間(包括當先後臺進程的cpu時間,但不包括過去的後臺進程的cpu時間);當前進程的cpu時間;當前進程的命令行。sql

uptime命令

uptime命令顯示系統運行了多長時間。uptime命令給出如下信息的單行顯示:系統當前時間;系統已經運行的時間;當前有多少用戶登陸;系統在過去1分鐘,5分鐘,15鍾內的平均負載。與W命令的第一行結果是同樣的。

[root@lanquark demo]# uptime 
 23:15:22 up  2:09,  2 users,  load average: 0.00, 0.01, 0.05
[root@lanquark demo]# w
 23:15:28 up  2:09,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    10.0.1.229       21:08    0.00s  0.62s  0.01s w
root     pts/1    10.0.1.229       21:33    1:06m  0.07s  0.00s less -s


2、vmstat命令

vmstat命令顯示虛擬內存統計信息。包括進程,內存,頁面文件,阻塞的I/O,陷井,和cpu。

常見用法

vmstat
靜態顯示內存狀態一次

[root@lanquark demo]# vmstat
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 738636   2076 127756    0    0     6     1   27   27  0  0 100  0  0

vmstat [n] 在此n表明時間間隔
意思是以n秒每次的時間間隔動態顯示內存狀態(按Ctrl+c結束命令)。

[root@lanquark demo]# vmstat 3
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 738636   2076 127756    0    0     6     1   26   26  0  0 100  0  0
 0  0      0 738636   2076 127756    0    0     0     0   61   51  0  0 100  0  0
 0  0      0 738636   2076 127756    0    0     0     0   37   35  0  0 100  0  0
 0  0      0 738636   2076 127756    0    0     0     0   49   44  0  0 100  0  0
^C

vmstat [n] [m]
意思是以n秒爲時間間隔,顯示m次後命令結束!

[root@lanquark demo]# vmstat 3  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 738776   2076 127756    0    0     6     1   26   26  0  0 100  0  0
 0  0      0 738760   2076 127756    0    0     0     0   37   37  0  0 100  0  0
 0  0      0 738760   2076 127756    0    0     0     0   37   37  0  0 100  0  0
 0  0      0 738760   2076 127756    0    0     0     0   39   36  0  0 100  0  0
 0  0      0 738760   2076 127756    0    0     0     0   37   36  0  0 100  0  0

字段說明:

Procs(進程)

r: 運行隊列中進程數量,這個值也能夠判斷是否須要增長CPU。(超過邏輯cpu的個數須要考慮增長cpu)
b: 等待IO的進程數量。

Memory(內存)

swpd: 使用虛擬內存大小,若是swpd的值不爲0,可是SI,SO的值長期爲0,這種狀況不會影響系統性能。
free: 空閒物理內存大小。
buff: 用做緩衝的內存大小。
cache: 用做緩存的內存大小,若是cache的值大的時候,說明cache處的文件數多,若是頻繁訪問到的文件都能被cache處,那麼磁盤的讀IO bi會很是小。

Swap
si: 每秒從交換區寫到內存的大小,由磁盤調入內存。
so: 每秒寫入交換區的內存大小,由內存調入磁盤。
注意:內存夠用的時候,這2個值都是0,若是這2個值長期大於0時,系統性能會受到影響,磁盤IO和CPU資源都會被消耗。有些朋友看到空閒內存(free)不多的或接近於0時,就認爲內存不夠用了,不能光看這一點,還要結合si和so,若是free不多,可是si和so也不多(大多時候是0),那麼不用擔憂,系統性能這時不會受到影響的。

IO(如今的Linux版本塊的大小爲1kb)

bi: 每秒讀取的塊數
bo: 每秒寫入的塊數
注意:隨機磁盤讀寫的時候,這2個值越大(如超出1024k),能看到CPU在IO等待的值也會越大。

system(系統)

in: 每秒中斷數,包括時鐘中斷。
cs: 每秒上下文切換數。
注意:上面2個值越大,會看到由內核消耗的CPU時間會越大。

CPU(以百分比表示)

us: 用戶進程執行時間百分比(user time)
us的值比較高時,說明用戶進程消耗的CPU時間多,可是若是長期超50%的使用,那麼咱們就該考慮優化程序算法或者進行加速。
sy: 內核系統進程執行時間百分比(system time)
sy的值高時,說明系統內核消耗的CPU資源多,這並非良性表現,咱們應該檢查緣由。
wa: IO等待時間百分比
wa的值高時,說明IO等待比較嚴重,這可能因爲磁盤大量做隨機訪問形成,也有可能磁盤出現瓶頸(塊操做)。

id: 空閒時間百分比


3、top命令

top命令是Linux下經常使用的性能分析工具,可以實時顯示系統中各個進程的資源佔用情況,相似於Windows的任務管理器。

語法:top [選項]

經常使用選項
-b:批量處理模式
-c:顯示詳細進程信息
-bn1:靜態顯示全部進程(該法多用於shell腳本)

top視圖

top - 23:54:11 up  2:48,  2 users,  load average: 0.00, 0.01, 0.05
Tasks: 100 total,   2 running,  98 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.2 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   999720 total,   736876 free,   132980 used,   129864 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   714768 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                        
     9 root      20   0       0      0      0 S   0.7  0.0   0:03.22 rcu_sched                                      
    13 root      20   0       0      0      0 S   0.7  0.0   0:00.16 ksoftirqd/1                                    
  1621 root      20   0  157684   2148   1516 R   0.2  0.2   0:00.07 top                                            
  1622 root      20   0       0      0      0 S   0.2  0.0   0:00.01 kworker/1:0                                    
     1 root      20   0  125736   4248   2504 S   0.0  0.4   0:01.19 systemd                                        
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kthreadd             
   
   ... 中間略...
   
    30 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kswapd0                                        
    31 root      25   5       0      0      0 S   0.0  0.0   0:00.00 ksmd                                           
    32 root      39  19       0      0      0 S   0.0  0.0   0:00.09 khugepaged

字段說明以下

統計信息區(前五行)

第一行從左至右依次顯示內容爲:系統當前時間;系統已經運行的時間;當前有多少用戶登陸;系統在過去1分鐘,5分鐘,15鍾內的平均負載。同uptime命令的運行結果。

第二行從左至右依次顯示內容爲:進程總數;正在運行的進程數;睡眠的進程數;中止的進程數;殭屍進程數

第三行從左至右依次顯示內容爲:用戶空間佔用cpu比;內核空間佔用cpu百分比;用戶進程空間內改變優先級的進程佔用cpu百分比;空閒cpu百分比;等待輸入輸出的cpu時間百分比;處理硬件中斷的CPU時間;處理軟件中斷的CPU時間;這個虛擬機被hypervisor偷去的CPU時間

最後兩行爲內存信息,內容以下:

物理內存總量;使用的物理內存總量;空閒內存總量;用做內核緩存的內存量;交換區總量;使用的交換區總量;空閒交換區總量;緩衝的交換區總量。

進程信息區

PID:進程ID,進程的惟一標識符

USER:進程全部者的實際用戶名。

PR:進程的調度優先級。這個字段的一些值是'rt'。這意味這這些進程運行在實時態。

NI:進程的nice值(優先級)。越小的值意味着越高的優先級。負值表示高優先級,正值表示低優先級

VIRT:進程使用的虛擬內存。進程使用的虛擬內存總量,單位kb。VIRT=SWAP+RES

RES:駐留內存大小。駐留內存是任務使用的非交換物理內存大小。進程使用的、未被換出的物理內存大小,單位kb。RES=CODE+DATA

SHR:SHR是進程使用的共享內存。共享內存大小,單位kb

S:這個是進程的狀態。它有如下不一樣的值:

D - 不可中斷的睡眠態。
R – 運行態
S – 睡眠態
T – 被跟蹤或已中止
Z – 殭屍態

%CPU:自從上一次更新時到如今任務所使用的CPU時間百分比。

%MEM:進程使用的可用物理內存百分比。

TIME+:任務啓動後到如今所使用的所有CPU時間,精確到百分之一秒。

COMMAND:運行進程所使用的命令。進程名稱(命令名/命令行)

還有許多在默認狀況下不會顯示的輸出,它們能夠顯示進程的頁錯誤、有效組和組ID和其餘更多的信息。

top的交互命令

‘h’: 幫助

Help for Interactive Commands - procps-ng version 3.3.10
Window 1:Def: Cumulative mode Off.  System: Delay 10.0 secs; Secure mode Off.

  Z,B,E,e   Global: 'Z' colors; 'B' bold; 'E'/'e' summary/task memory scale
  l,t,m     Toggle Summary: 'l' load avg; 't' task/cpu stats; 'm' memory info
  0,1,2,3,I Toggle: '0' zeros; '1/2/3' cpus or numa node views; 'I' Irix mode
  f,F,X     Fields: 'f'/'F' add/remove/order/sort; 'X' increase fixed-width

  L,&,<,> . Locate: 'L'/'&' find/again; Move sort column: '<'/'>' left/right
  R,H,V,J . Toggle: 'R' Sort; 'H' Threads; 'V' Forest view; 'J' Num justify
  c,i,S,j . Toggle: 'c' Cmd name/line; 'i' Idle; 'S' Time; 'j' Str justify
  x,y     . Toggle highlights: 'x' sort field; 'y' running tasks
  z,b     . Toggle: 'z' color/mono; 'b' bold/reverse (only if 'x' or 'y')
  u,U,o,O . Filter by: 'u'/'U' effective/any user; 'o'/'O' other criteria
  n,#,^O  . Set: 'n'/'#' max tasks displayed; Show: Ctrl+'O' other filter(s)
  C,...   . Toggle scroll coordinates msg for: up,down,left,right,home,end

  k,r       Manipulate tasks: 'k' kill; 'r' renice
  d or s    Set update interval
  W,Y       Write configuration file 'W'; Inspect other output 'Y'
  q         Quit
          ( commands shown with '.' require a visible task display window ) 
Press 'h' or '?' for help with Windows,
Type 'q' or <Esc> to continue

’ 或者 ‘ ’: 刷新顯示

top命令默認在一個特定間隔(3秒)後刷新顯示。要手動刷新,用戶能夠輸入回車或者空格。

多核CPU監控

在top基本視圖中,按鍵盤數字「1」,可監控每一個邏輯CPU的情況:

//默認顯示%cpu
//按1後顯示%cpu0,%cpu1,由於本機是雙核
top - 01:50:31 up  4:44,  2 users,  load average: 0.00, 0.01, 0.05
Threads: 115 total,   2 running, 113 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  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 :   999720 total,   736128 free,   133392 used,   130200 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   714152 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                         
  1621 root      20   0  157684   2268   1604 R  0.1  0.2   0:02.27 top                                             
     1 root      20   0  125736   4248   2504 S  0.0  0.4   0:01.24 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.07 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.00 kworker/u256:0                                  
...下略...

shift+M 按內存佔用排序(默認(默認以CPU使用佔比排序,shift+p恢復默認)

top - 01:56:03 up  4:50,  2 users,  load average: 0.00, 0.01, 0.05
Threads: 115 total,   2 running, 113 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.5 sy,  0.0 ni, 99.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   999720 total,   736128 free,   133392 used,   130200 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   714152 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                         
  1070 root      20   0  562408  18644   5888 S  0.0  1.9   0:00.20 tuned                                           
  1111 root      20   0  562408  18644   5888 S  0.0  1.9   0:00.00 gmain                                           
  1112 root      20   0  562408  18644   5888 S  0.0  1.9   0:00.00 tuned                                           
  1113 root      20   0  562408  18644   5888 S  0.0  1.9   0:02.36 tuned                                           
  1114 root      20   0  562408  18644   5888 S  0.0  1.9   0:00.00 tuned                                           
   674 polkitd   20   0  534892  11816   4864 S  0.0  1.2   0:00.05 polkitd                                         
   693 polkitd   20   0  534892  11816   4864 S  0.0  1.2   0:00.00 gmain                                           
   695 polkitd   20   0  534892  11816   4864 S  0.0  1.2   0:00.00 gdbus                                           
   696 polkitd   20   0  534892  11816   4864 S  0.0  1.2   0:00.00 JS GC Helper                                    
   697 polkitd   20   0  534892  11816   4864 S  0.0  1.2   0:00.00 JS Sour~ Thread                                 
   707 polkitd   20   0  534892  11816   4864 S  0.0  1.2   0:00.00 polkitd                                         
   730 root      20   0  471816  10916   6612 S  0.0  1.1   0:00.17 NetworkManager                                  
   750 root      20   0  471816  10916   6612 S  0.0  1.1   0:00.41 gmain                                           
   765 root      20   0  471816  10916   6612 S  0.0  1.1   0:00.01 gdbus                                           
   670 root      20   0  210180   9908   3104 S  0.0  1.0   0:00.01 rsyslogd                                        
   692 root      20   0  210180   9908   3104 S  0.0  1.0   0:01.76 in:imjournal                                    
   694 root      20   0  210180   9908   3104 S  0.0  1.0   0:00.08 rs:main Q:Reg                                   
   673 root      20   0  305080   6160   4796 S  0.0  0.6   0:16.36 vmtoolsd                                        
   719 root      20   0  305080   6160   4796 S  0.0  0.6   0:00.00 gmain                                           
   672 root      20   0   99612   6080   4476 S  0.0  0.6   0:00.04 VGAuthService                                   
   654 root      20   0  219408   5644   3828 S  0.0  0.6   0:00.14 abrtd                                           
  1183 root      20   0  145704   5204   3940 S  1.0  0.5   0:02.50 sshd                                            
  1269 root      20   0  145704   5204   3940 S  0.0  0.5   0:00.25 sshd                                            
   667 root      20   0  216908   4724   3320 S  0.0  0.5   0:00.06 abrt-watch-log                                  
     1 root      20   0  125736   4248   2504 S  0.0  0.4   0:01.24 systemd                                         
  1069 root      20   0  105996   4092   3112 S  0.0  0.4   0:00.01 sshd

-f 默認顯示的字段

Fields Management for window 1:Def, whose current sort field is %MEM
   Navigate with Up/Dn, Right selects for move then <Enter> or Left commits,
   'd' or <Space> toggles display, 's' sets sort.  Use 'q' or <Esc> to end!

* PID     = Process Id             DATA    = Data+Stack (KiB)    
* USER    = Effective User Name    nMaj    = Major Page Faults   
* PR      = Priority               nMin    = Minor Page Faults   
* NI      = Nice Value             nDRT    = Dirty Pages Count   
* VIRT    = Virtual Image (KiB)    WCHAN   = Sleeping in Function
* RES     = Resident Size (KiB)    Flags   = Task Flags <sched.h>
* SHR     = Shared Memory (KiB)    CGROUPS = Control Groups      
* S       = Process Status         SUPGIDS = Supp Groups IDs     
* %CPU    = CPU Usage              SUPGRPS = Supp Groups Names   
* %MEM    = Memory Usage (RES)     TGID    = Thread Group Id     
* TIME+   = CPU Time, hundredths   ENVIRON = Environment vars    
* COMMAND = Command Name/Line      vMj     = Major Faults delta  
  PPID    = Parent Process pid     vMn     = Minor Faults delta  
  UID     = Effective User Id      USED    = Res+Swap Size (KiB) 
  RUID    = Real User Id           nsIPC   = IPC namespace Inode 
  RUSER   = Real User Name         nsMNT   = MNT namespace Inode 
  SUID    = Saved User Id          nsNET   = NET namespace Inode 
  SUSER   = Saved User Name        nsPID   = PID namespace Inode 
  GID     = Group Id               nsUSER  = USER namespace Inode
  GROUP   = Group Name             nsUTS   = UTS namespace Inode 
  PGRP    = Process Group Id    
  TTY     = Controlling Tty     
  TPGID   = Tty Process Grp Id  
  SID     = Session Id          
  nTH     = Number of Threads   
  P       = Last Used Cpu (SMP) 
  TIME    = CPU Time            
  SWAP    = Swapped Size (KiB)  
  CODE    = Code Size (KiB)

top -bn1 靜態顯示全部進程

[root@lanquark ~]# top -bn1
top - 20:33:37 up 6 min,  1 user,  load average: 0.00, 0.01, 0.01
Tasks: 115 total,   1 running, 114 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  1.6 sy,  0.0 ni, 98.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   999720 total,   267008 free,   577360 used,   155352 buff/cache
KiB Swap:  1999868 total,  1999868 free,        0 used.   255448 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
     1 root      20   0  191252   4212   2492 S   0.0  0.4   0:01.15 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.00 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.02 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:00.23 rcu_sched
    10 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 watchdog/0
    11 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 watchdog/1
    12 root      rt   0       0      0      0 S   0.0  0.0   0:00.01 migration/1
    13 root      20   0       0      0      0 S   0.0  0.0   0:00.01 ksoftirqd/1
    14 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0
...中間略...
  1121 root      20   0  105996   4084   3104 S   0.0  0.4   0:00.00 sshd
  1134 root      20   0  115388   1672   1360 S   0.0  0.2   0:00.00 mysqld_safe
  1277 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kdmflush
  1278 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 bioset
  1310 mysql     20   0 1300780 454336   5840 S   0.0 45.4   0:04.04 mysqld
  1372 root      20   0   89544   2080   1068 S   0.0  0.2   0:00.01 master
  1373 postfix   20   0   89648   4004   3004 S   0.0  0.4   0:00.01 pickup
  1374 postfix   20   0   89716   4028   3024 S   0.0  0.4   0:00.00 qmgr
  1407 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/2:1H
  1413 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:1H
  1417 root      20   0  145700   5168   3908 S   0.0  0.5   0:00.05 sshd
  1419 root      20   0  116316   2936   1648 S   0.0  0.3   0:00.04 bash
  1468 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/3:1H
  1482 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0
  1483 root      20   0  157584   2056   1480 R   0.0  0.2   0:00.00 top

htop命令具備更好的可視化界面。


sar命令

sar是system activity recorder的縮寫,它不只能夠用來收集、報告和存儲系統活動實時信息,還能夠從數據文件中查看歷史信息。

若是系統中沒有sar命令,需安裝sysstat軟件包

[root@lanquark demo]# yum -y install sysstat-10.1.5-13.el7.x86_64

實時信息

語法:sar [選項] [interval] [count]

活動信息依賴於interval和count兩個參數。如沒有count,則只會運行一次。

歷史信息

在/var/log/sa目錄中記錄了Linux系統的主要組件的數據,包括cpu、內存、磁盤、網絡等。天天的數據記錄在一個名爲sa[nn]文件中。nn是每個月的第nn天。

查看特定日期的sar數據用以下命令

//當月10日的信息
[root@lanquark demo]# sar -f  /var/log/sa/sa10 
Linux 3.10.0-693.el7.x86_64 (lanquark.com)  06/10/2018  _x86_64_    (2 CPU)

顯示系統cpu利用率的相關信息

//使用sar 3 5 或者 sar -u 3 5
[root@lanquark demo]# sar 3 5
Linux 3.10.0-693.el7.x86_64 (lanquark.com)  06/10/2018  _x86_64_    (2 CPU)

02:22:07 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
02:22:10 AM     all      0.00      0.00      0.00      0.00      0.00    100.00
02:22:13 AM     all      0.17      0.00      0.17      0.00      0.00     99.67
02:22:16 AM     all      0.00      0.00      0.00      0.00      0.00    100.00
02:22:19 AM     all      0.00      0.00      0.17      0.00      0.00     99.83
02:22:22 AM     all      0.00      0.00      0.00      0.00      0.00    100.00
Average:        all      0.03      0.00      0.07      0.00      0.00     99.90
//cpu信息說明
//CPU標識符;all表示全部CPU
//%user用戶級進行運行佔用的cpu利用率,包括運行虛擬進程的時間
//%nice 在nice優先級下用戶級進程佔用的cpu利用率
//%system 系統進程(內核)運行佔用的cpu百分比,包括軟、硬件的中斷時間
//%iowait等待進行I/O操做的cpu百分比,此時cpu處於空閒狀態可是系統有未完成的I/O請求
//%steal 在管理程序運行其餘虛擬化進程所形成的cpu等待的百分比
//%idle 等待工做的cpu空閒時間百分比

//顯示各個cpu的統計信息及總統計信息
[root@lanquark demo]# sar -P ALL 2 2
Linux 3.10.0-693.el7.x86_64 (lanquark.com)  06/10/2018  _x86_64_    (2 CPU)

02:50:27 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
02:50:29 AM     all      0.00      0.00      0.25      0.00      0.00     99.75
02:50:29 AM       0      0.00      0.00      0.00      0.00      0.00    100.00
02:50:29 AM       1      0.00      0.00      0.00      0.00      0.00    100.00

02:50:29 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
02:50:31 AM     all      0.00      0.00      0.00      0.00      0.00    100.00
02:50:31 AM       0      0.00      0.00      0.00      0.00      0.00    100.00
02:50:31 AM       1      0.00      0.00      0.00      0.00      0.00    100.00

Average:        CPU     %user     %nice   %system   %iowait    %steal     %idle
Average:        all      0.00      0.00      0.12      0.00      0.00     99.88
Average:          0      0.00      0.00      0.00      0.00      0.00    100.00
Average:          1      0.00      0.00      0.00      0.00      0.00    100.00

內存信息

//每隔1秒輸出一次內存信息,共輸出3次
[root@lanquark demo]# sar -R 1 3
Linux 3.10.0-693.el7.x86_64 (lanquark.com)  06/10/2018  _x86_64_    (2 CPU)

02:52:03 AM   frmpg/s   bufpg/s   campg/s
02:52:04 AM      6.00      0.00      0.00
02:52:05 AM      0.00      0.00      0.00
02:52:06 AM      0.00      0.00      0.00
Average:         1.99      0.00      0.00
//frmpg/s 系統每秒釋放的內存頁數量,若是值爲負表示系統已經分配了的內存頁數。依機器體系的不一樣,內存頁的大小可能爲4kb或8kb
//bufpg/s 系統每秒用於buffers的附加的內存頁數。若是值爲負表示只有較少的頁被用於系統buffers
//campg/s 系統每秒用於cached的附加的內存頁數。若是值爲負表示只有較少的頁被用於系統cached


//每隔1秒輸出一次內存利用率信息,共輸出3次
[root@lanquark demo]# sar -r 1 3
Linux 3.10.0-693.el7.x86_64 (lanquark.com)  06/10/2018  _x86_64_    (2 CPU)

02:53:30 AM kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
02:53:31 AM    250040    749680     74.99      2076    547132    258132      8.34    540072     85256         0
02:53:32 AM    250040    749680     74.99      2076    547132    258132      8.34    540080     85256         0
02:53:33 AM    250040    749680     74.99      2076    547132    258132      8.34    540084     85256         0
Average:       250040    749680     74.99      2076    547132    258132      8.34    540079     85256         0
//kbmemfree 此時可用的內存數量,以kb爲單位
//kbmemused 此時已用的內存數量,以kb爲單位,不包括內核使用的內存數
//%memused  已使用內存的百分比
//kbbuffers 內核使用的緩衝區(buffer)佔內存的百分比,以kb爲單位,即爲free命令中的buffer
//kbcached 內核使用的緩存(cache)佔內存的百分比,以kb爲單位,即爲free命令中的cache
//kbcommit 當前工做負載所須要的內存數量,以kb爲單位,這是對於當前所需RAM/swap的一個估值。以保證不會出現內存不足
//%commit 中kbcommit與內存總量(包括swap)的一個比值,該值有可能會超100%

//每隔1秒輸出一次SWAP利用率信息,共輸出3次
[root@lanquark demo]# sar -S 1 3
Linux 3.10.0-693.el7.x86_64 (lanquark.com)  06/10/2018  _x86_64_    (2 CPU)

03:05:01 AM kbswpfree kbswpused  %swpused  kbswpcad   %swpcad
03:05:02 AM   2097148         0      0.00         0      0.00
03:05:03 AM   2097148         0      0.00         0      0.00
03:05:04 AM   2097148         0      0.00         0      0.00
Average:      2097148         0      0.00         0      0.00
//kbswpfree 空閒可用的交換分區大小,以kb爲單位
//kbswpused 以使用的交換分區大小,以kb爲單位
//%swpused  已使用的交換分區所佔的百分比
//kbswpcad  用於cache的swap內存數,以kb爲單位。該內存指的是:曾經被swap out的內存又被swap back in。可是還存在於swap中。因爲它在swap中已經存在,因此若是該部份內存須要調用就不會再次swap out,節省I/O操做。
//%swpcad 用於cache的swap內存。

I/O

[root@lanquark demo]# sar -b 2 5
Linux 3.10.0-693.el7.x86_64 (lanquark.com)  06/10/2018  _x86_64_    (2 CPU)

03:21:20 AM       tps      rtps      wtps   bread/s   bwrtn/s
03:21:22 AM      0.00      0.00      0.00      0.00      0.00
03:21:24 AM      0.00      0.00      0.00      0.00      0.00
03:21:26 AM      0.00      0.00      0.00      0.00      0.00
03:21:28 AM      0.00      0.00      0.00      0.00      0.00
03:21:30 AM      0.00      0.00      0.00      0.00      0.00
Average:         0.00      0.00      0.00      0.00      0.00
//tps 每秒傳送到物理設備的I/O請求總數。多個邏輯請求可能會組合成一個I/O請求,每一個I/O請求的大小並不固定
//rtps 對物理設備的讀請求速率/秒
//wtps 對物理設備的寫請求速率/秒
//bread/s 每秒從塊設備讀取數據的速度,以塊(block)爲單位,一塊爲512Bytes
//bwrtn/s 每秒向塊設備寫入數據的速度

磁盤統計信息

//每隔2秒顯示一次信息,共顯示5次
[root@lanquark demo]# sar -d 2 5
Linux 3.10.0-693.el7.x86_64 (lanquark.com)  06/10/2018  _x86_64_    (2 CPU)

03:36:04 AM       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
03:36:06 AM    dev8-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
03:36:06 AM   dev11-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

03:36:06 AM       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
03:36:08 AM    dev8-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
03:36:08 AM   dev11-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

03:36:08 AM       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
03:36:10 AM    dev8-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
03:36:10 AM   dev11-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

03:36:10 AM       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
03:36:12 AM    dev8-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
03:36:12 AM   dev11-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

03:36:12 AM       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
03:36:14 AM    dev8-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
03:36:14 AM   dev11-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

Average:          DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
Average:       dev8-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:      dev11-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

//tps 每秒對設備進行的I/O操做數量
//rd_sec/s 每秒從磁盤讀取的扇區數,每一個扇區爲512字節
//wr_sec/s 每秒寫入磁盤的扇區數
//avgrq-sz 磁盤的平均請求扇區數
//avgqu-sz 磁盤的平均請求的隊列長度
//await 平均每次磁盤I/O請求花費的時間,包括服務時間的隊列等待時間
//svctm 平均每次磁盤I/O請求的服務時間
//%util 設備用於I/O操做的cpu百分比


nload命令

系統中默認沒有nload命令,需安裝軟件包nload

[root@lanquark demo]# yum -y install nload-0.7.4-4.el7.x86_64

用法

直接在命令行鍵入nload

RnvV9.jpg

nload 命令一旦執行就會開始監控網絡設備,你可使用下列快捷鍵操控 nload 應用程序。

你能夠按鍵盤上的 ← → 或者 Enter/Tab 鍵在設備間切換。
按 F2 顯示選項窗口。
按 F5 將當前設置保存到用戶配置文件。
按 F6 從配置文件從新加載設置。
按 q 或者 Ctrl+C 退出 nload。


6、監控I/O性能

iostat命令能夠查看cpu的統計信息,以及設備,磁盤分區和網絡文件系統等的I/O統計信息。

經常使用參數

-c 顯示cpu的使用狀況,默認顯示cpu的使用狀況
-d 顯示磁盤的使用狀況
-k 以千字節爲單位顯示數據
-x device 指定要統計的設備名稱,默認爲全部的設備

cpu利用率報告

[root@lanquark demo]# iostat -c 
Linux 3.10.0-693.el7.x86_64 (lanquark.com)  06/10/2018  _x86_64_    (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.04    0.00    0.10    0.06    0.00   99.80
//%user  cpu處在用戶模式下的時間百分比 
//%nice  cpu處在帶nice值的用戶模式下的時間百分比 
//%system cpu處在系統模式下的時間百分比
//%iowait cpu等待輸入/輸出完成的時間百分比
//%steal  管理程序維護另外一個虛擬處理器時,虛擬cpu的無心識等待時間百分比
//%idle 顯示cpu空閒時間的在百分比
//若是iowait的值太高,表示磁盤存在I/O瓶頸;若是idle值高,說明cpu較空閒,若是idle值高但系統響應慢,有多是cpu等待分配內存,此時應加在內存容量,若是idle值持續低於10,說明cpu存在瓶頸。

設備利用率

[root@lanquark demo]# iostat sda
Linux 3.10.0-693.el7.x86_64 (lanquark.com)  06/10/2018  _x86_64_    (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.04    0.00    0.10    0.06    0.00   99.81

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.54         9.48        15.04     248642     394548
//Device 監測的設備名稱
//tps 設備每秒傳輸的次數
//kB_read/s  每秒從該設備讀取的塊數
//kB_wrtn/s  每秒向該設備寫入的塊數
//kB_read  系統啓動讀取的總塊數
//kB_wrtn  系統啓動後寫入的總塊數

如需瞭解更加詳細的信息,須要使用-x參數
[root@lanquark demo]# iostat -x sda
Linux 3.10.0-693.el7.x86_64 (lanquark.com)  06/10/2018  _x86_64_    (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.04    0.00    0.09    0.06    0.00   99.81

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.01    0.34    0.20     9.39    14.90    91.09     0.01   25.73   11.22   50.71   2.87   0.15
//Device:  監測的設備名稱,默認全部的設備      
//rrqm/s   該設備每秒合併的請求數量
//wrqm/s   與讀取相似,表示該設備每秒合併的寫請求數
// r/s   每秒向該設備發出的讀請求數
// w/s   每秒每該設備發出的寫請求數
//rkB/s  每秒從該設備讀取的扇區數
//wkB/s  每秒寫入該設備的扇區數
//avgrq-sz 該設備讀請求的平均大小,以扇區爲單位
//avgqu-sz 該設備的請求隊列的平均長度
//await 該設備的每個I/O請求處理的平均時間
//r_await 該設備的每個I/O讀請求處理的平均時間
//w_await 該設備的每個I/O寫請求處理的平均時間
//svctm  該設備的I/O請求的平均服務時間,以毫秒爲單位
//%util 該設備在I/O請求過程當中CPU百分比。暗示了設備的繁忙程度。

iotop命令

iotop命令是相似於top命令的I/O監視器。iotop具備與top類似的UI,其中包括PID、用戶、I/O、進程等相關信息。Linux下的IO統計工具如iostat,nmon等大多數是隻能統計到每設備的讀寫狀況,若是你想知道每一個進程是如何使用IO的就比較麻煩,使用iotop命令能夠很方便的查看。

系統默認沒有安裝iotop,需安裝軟件包iotop

[root@lanquark demo]# yum -y install iotop-0.6-2.el7.noarch

用法

在命令行直接輸入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]
     6 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/u256:0]
     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]
    11 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [watchdog/1]
    12 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/1]
    13 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/1]
    15 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/1:0H]
    17 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kdevtmpfs]
    18 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [netns]
    19 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [khungtaskd]
    20 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [writeback]
    21 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kintegrityd]
    22 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [bioset]
    23 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kblockd]
    24 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [md]
  1114 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % python -Es /usr/sbin/tuned -l -P
    30 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kswapd0]
    31 be/5 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksmd]
    32 be/7 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [khugepaged]
    33 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [crypto]
    41 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthrotld]
    43 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/u256:1]
    44 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kmpath_rdacd]
...下略...

經常使用快捷

← →左右箭頭:改變排序方式,默認是按IO排序。
r:改變排序順序。
o:只顯示有IO輸出的進程。
p:進程/線程的顯示方式的切換。
a:顯示累積使用量。
q:退出。


7、free命令

free命令能夠查看系統內存的使用狀況。

-b,-k,-m,-g 以KB,MB,GB爲單位顯示輸出

[root@lanquark demo]# free -k
              total        used        free      shared  buff/cache   available
Mem:         999720      137740      227952        6760      634028      674568
Swap:       2097148           0     2097148
[root@lanquark demo]# free -m
              total        used        free      shared  buff/cache   available
Mem:            976         134         222           6         619         658
Swap:          2047           0        2047
[root@lanquark demo]# free -g
              total        used        free      shared  buff/cache   available
Mem:              0           0           0           0           0           0
Swap:             1           0           1

-h 以人類友好的方式輸出

[root@lanquark demo]# free -h
              total        used        free      shared  buff/cache   available
Mem:           976M        134M        222M        6.6M        619M        658M
Swap:          2.0G          0B        2.0G

-t 在輸出底部顯示物理內存和交換分區的總和

[root@lanquark demo]# free -t
              total        used        free      shared  buff/cache   available
Mem:         999720      137560      228112        6760      634048      674748
Swap:       2097148           0     2097148
Total:      3096868      137560     2325260

說明:

「total=used+free+buff/cache」

「available=free+buff/cache(空閒部分)」

buff(緩衝):主要是用在傳輸效率不一樣步或優先級不一樣設備之間傳輸數據,通常是先將一方數據臨時存放,而後待時機合適再將數據統一發送到另外一方,從而下降了系統的等待時間。

cache(緩存):緩存主要是在傳輸速度比較高的設備中爲傳輸速度比較低的設備開闢必定空間,用於存放速度低設備中數據的副本,這樣當要訪問數據時,就能夠從速度快設備訪問獲得,無需訪問低速度的設備。


8、ps命令

PS命令能夠查找特定的PID,檢查進程正在作什麼,而且概述系統中正在發生的每件事件。

使用UNIX選項的基本語法

ps [-aefFly] [-p pid] [-u uid]

使用BSD選項的基本語法

ps [ajluvx] [p pid] [u uid]

二種選項使用任意一種便可。

經常使用組合

ps 顯示本身的進程

[root@lanquark demo]# ps
   PID TTY          TIME CMD
  1185 pts/0    00:00:01 bash
  3004 pts/0    00:00:00 ps

ps -ef,顯示全部的用戶進程,完整輸出

[root@lanquark demo]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 Jun09 ?        00:00:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root          2      0  0 Jun09 ?        00:00:00 [kthreadd]
root          3      2  0 Jun09 ?        00:00:00 [ksoftirqd/0]
root          5      2  0 Jun09 ?        00:00:00 [kworker/0:0H]
root          6      2  0 Jun09 ?        00:00:00 [kworker/u256:0]
root          7      2  0 Jun09 ?        00:00:00 [migration/0]
root          8      2  0 Jun09 ?        00:00:00 [rcu_bh]
root          9      2  0 Jun09 ?        00:00:09 [rcu_sched]
root         10      2  0 Jun09 ?        00:00:00 [watchdog/0]
...中間略...
root       1271   1269  0 Jun09 pts/1    00:00:00 -bash
root       1403      2  0 Jun09 ?        00:00:14 [kworker/0:0]
root       1603   1271  0 Jun09 pts/1    00:00:00 man vmstat
root       1613   1603  0 Jun09 pts/1    00:00:00 less -s
root       2979      2  0 05:10 ?        00:00:00 [kworker/1:0]
postfix    2994   1170  0 05:26 ?        00:00:00 pickup -l -t unix -u
root       3001      2  0 05:30 ?        00:00:00 [kworker/1:1]
root       3002      2  0 05:32 ?        00:00:00 [kworker/0:1]
root       3003      2  0 05:37 ?        00:00:00 [kworker/0:2]
root       3005      2  0 05:38 ?        00:00:00 [kworker/1:2]
root       3006   1185  0 05:38 pts/0    00:00:00 ps -ef

ps -a 顯示全部非守護進程

[root@lanquark demo]# ps -a
   PID TTY          TIME CMD
  1603 pts/1    00:00:00 man
  1613 pts/1    00:00:00 less
  3007 pts/0    00:00:00 ps

ps -t -

[root@lanquark demo]# ps -t -
   PID TTY          TIME CMD
     1 ?        00:00:01 systemd
     2 ?        00:00:00 kthreadd
     3 ?        00:00:00 ksoftirqd/0
     5 ?        00:00:00 kworker/0:0H
     6 ?        00:00:00 kworker/u256:0
     7 ?        00:00:00 migration/0
     8 ?        00:00:00 rcu_bh
...中間略...
  1070 ?        00:00:04 tuned
  1170 ?        00:00:00 master
  1172 ?        00:00:00 qmgr
  1183 ?        00:00:08 sshd
  1269 ?        00:00:00 sshd
  1403 ?        00:00:14 kworker/0:0
  2979 ?        00:00:00 kworker/1:0
  2994 ?        00:00:00 pickup
  3001 ?        00:00:00 kworker/1:1
  3002 ?        00:00:00 kworker/0:1
  3003 ?        00:00:00 kworker/0:2
  3005 ?        00:00:00 kworker/1:2

ps ax 顯示全部進程

[root@lanquark demo]# ps ax
   PID TTY      STAT   TIME COMMAND
     1 ?        Ss     0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
     2 ?        S      0:00 [kthreadd]
     3 ?        S      0:00 [ksoftirqd/0]
   ...中間略...
  1403 ?        R      0:14 [kworker/0:0]
  1603 pts/1    S+     0:00 man vmstat
  1613 pts/1    S+     0:00 less -s
  2979 ?        S      0:00 [kworker/1:0]
  2994 ?        S      0:00 pickup -l -t unix -u
  3001 ?        S      0:00 [kworker/1:1]
  3002 ?        S      0:00 [kworker/0:1]
  3003 ?        S      0:00 [kworker/0:2]
  3005 ?        S      0:00 [kworker/1:2]
  3014 pts/0    R+     0:00 ps ax

ps aux 顯示全部進程,完整輸出

[root@lanquark demo]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.4 125736  4256 ?        Ss   Jun09   0:01 /usr/lib/systemd/systemd --switched-root --system -
root          2  0.0  0.0      0     0 ?        S    Jun09   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    Jun09   0:00 [ksoftirqd/0]
...中間略...
root       1271  0.0  0.3 116316  3020 pts/1    Ss   Jun09   0:00 -bash
root       1403  0.0  0.0      0     0 ?        S    Jun09   0:14 [kworker/0:0]
root       1603  0.0  0.1 119208  1952 pts/1    S+   Jun09   0:00 man vmstat
root       1613  0.0  0.0 110260   952 pts/1    S+   Jun09   0:00 less -s
root       2979  0.0  0.0      0     0 ?        S    05:10   0:00 [kworker/1:0]
postfix    2994  0.0  0.4  89648  4016 ?        S    05:26   0:00 pickup -l -t unix -u
root       3001  0.0  0.0      0     0 ?        S    05:30   0:00 [kworker/1:1]
root       3003  0.0  0.0      0     0 ?        S    05:37   0:00 [kworker/0:2]
root       3005  0.0  0.0      0     0 ?        R    05:38   0:00 [kworker/1:2]
root       3015  0.0  0.0      0     0 ?        S    05:42   0:00 [kworker/0:1]
root       3016  0.0  0.1 151064  1832 pts/0    R+   05:43   0:00 ps aux

常見列標題

PID 進程ID

PRI 優先級

RSS 內存駐留空間大小(內存管理)

TTY 控制終端的完整名稱

UID 用戶標誌

%CPU CPU(處理器)使用百分比

%MEM 真實內存使用百分比

JOBC 做業控制統計

SL 睡眠時間(單位爲秒)

STAT 進程狀態

進程狀態包括:

D:不能中斷的進程
R:run狀態的進程
S:sleep狀態的進程
s:主進程
T:暫停的進程
Z:殭屍進程
<:高優先級進程
N:低優先級進程
L:內存中被鎖定了內存分頁
l:多線程進程
+:前臺進程


9、查看網絡狀態

netstat命令

經過netstat命令能夠列出系統中與IP、TCP、UDP及ICMP等協議相關的統計信息。還能夠用於檢測各個端口的監聽狀態。

經常使用參數說明

-r 顯示路由表,功能與route相同
-n 不解析域名,只是列出IP地址及端口
-a 列出全部的聯機狀態,包括tcp/udp UNIX socket等
-p 顯示PID及程序名
-e 顯示更多其餘信息
-c 配置幾秒後更新一次數據
-l 列出正在listen(監聽)的服務的網絡狀態
-t 列出tcp數據包的鏈接
-u 列出udp數據包的鏈接

顯示當前路由表

[root@lanquark ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 ens32
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 ens32

顯示目前已經創建的全部的有效鏈接信息列表

[root@lanquark ~]# netstat -an
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.1.211:22        192.168.1.9:2068        ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
tcp6       0      0 :::3306                 :::*                    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     11524    /run/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     22755    public/cleanup
unix  2      [ ACC ]     SEQPACKET  LISTENING     11540    /run/udev/control
unix  2      [ ]         DGRAM                    11542    /run/systemd/shutdownd
unix  2      [ ACC ]     STREAM     LISTENING     21147    private/tlsmgr
unix  2      [ ACC ]     STREAM     LISTENING     21150    private/rewrite
unix  2      [ ACC ]     STREAM     LISTENING     21153    private/bounce
unix  2      [ ACC ]     STREAM     LISTENING     21156    private/defer
...中間略...
unix  3      [ ]         STREAM     CONNECTED     18050    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     21178    
unix  3      [ ]         STREAM     CONNECTED     17856    /run/systemd/journal/stdout
unix  2      [ ]         DGRAM                    17010    
unix  2      [ ]         DGRAM                    16946

查看監聽端口

[root@lanquark ~]# 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      1121/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1372/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1121/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1372/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1310/mysqld         
udp        0      0 127.0.0.1:323           0.0.0.0:*                           751/chronyd         
udp6       0      0 ::1:323                 :::*                                751/chronyd         
raw6       0      0 :::58                   :::*                    7           784/NetworkManager  
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     11524    1/systemd            /run/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     22755    1372/master          public/cleanup
unix  2      [ ACC ]     SEQPACKET  LISTENING     11540    1/systemd            /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     21147    1372/master          private/tlsmgr
...中間略...
unix  2      [ ACC ]     STREAM     LISTENING     21180    1372/master          public/showq
unix  2      [ ACC ]     STREAM     LISTENING     13769    1/systemd            /run/lvm/lvmetad.socket
unix  2      [ ACC ]     STREAM     LISTENING     22812    1310/mysqld          /tmp/mysql.sock
unix  2      [ ACC ]     STREAM     LISTENING     16351    735/VGAuthService    /var/run/vmware/guestServicePipe
unix  2      [ ACC ]     STREAM     LISTENING     16889    714/abrtd            /var/run/abrt/abrt.socket

只看tcp協議鏈接,不看socket

[root@lanquark ~]# 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      1121/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1372/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1121/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1372/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1310/mysqld

列出目前已經啓動的TCP/UDP網絡服務

[root@lanquark ~]# netstat -nltup
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      1121/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1372/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1121/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1372/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1310/mysqld         
udp        0      0 127.0.0.1:323           0.0.0.0:*                           751/chronyd         
udp6       0      0 ::1:323                 :::*                                751/chronyd

查看tcp協議狀態的命令

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

ss命令
ss 是 socket statistics 的縮寫。顧名思義,ss 命令能夠用來獲取socket 統計信息,它能夠顯示和netstat 相似的內容。但 ss 的優點在於它可以顯示更多更詳細的有關TCP和鏈接狀態的信息,並且比netstat更快速更高效。缺點是不會顯示進程的名稱。

語法:ss 參數
ss 參數 過濾

經常使用參數說明

-n 不解析服務名稱
-r 解析主機名
-l 顯示監聽狀態的套接字
-a 顯示全部套接字
-t 僅顯示tcp套接字
-u 僅顯示udp套接字

顯示TCP鏈接

[root@lanquark ~]# ss -t -a
State      Recv-Q Send-Q   Local Address:Port                    Peer Address:Port                
LISTEN     0      128                  *:ssh                                *:*                    
LISTEN     0      100          127.0.0.1:smtp                               *:*                    
ESTAB      0      0        192.168.1.211:ssh                      192.168.1.9:avauthsrvprtcl       
LISTEN     0      128                 :::ssh                               :::*                    
LISTEN     0      100                ::1:smtp                              :::*                    
LISTEN     0      80                  :::mysql                             :::*

顯示全部UDP sockets

[root@lanquark ~]# ss   -u  -a
State      Recv-Q Send-Q   Local Address:Port                    Peer Address:Port                
ESTAB      0      0        192.168.1.211:53905                  5.103.139.163:ntp                  
UNCONN     0      0            127.0.0.1:323                                *:*                    
UNCONN     0      0                  ::1:323                               :::*

顯示 sockets 摘要

[root@lanquark ~]# ss   -s
Total: 572 (kernel 1122)
TCP:   6 (estab 1, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 0

Transport Total     IP        IPv6
*     1122      -         -        
RAW   1         0         1        
UDP   2         1         1        
TCP   6         3         3        
INET      9         4         5        
FRAG      0         0         0

顯示全部的聯機狀態

[root@lanquark ~]# ss -an
Netid  State      Recv-Q Send-Q  Local Address:Port                 Peer Address:Port              
nl     UNCONN     0      0                   0:0                                *                   

...中間略...
          
tcp    ESTAB      0      5376    192.168.1.211:22                    192.168.1.9:2068               
tcp    LISTEN     0      128                :::22                             :::*                  
tcp    LISTEN     0      100               ::1:25                             :::*                  
tcp    LISTEN     0      80                 :::3306                           :::*


10、linux下抓包

tcpdump命令

用簡單的話來定義tcpdump,就是:dump the traffic on a network,根據使用者的定義對網絡上的數據包進行截獲的包分析工具。 tcpdump能夠將網絡中傳送的數據包的「頭」徹底截獲下來提供分析。它支持針對網絡層、協議、主機、網絡或端口的過濾,並提供and、or、not等邏輯語句來幫助你去掉無用的信息。

不帶任何選項的tcpdump,默認會抓取第一個網絡接口,且只有將tcpdump進程終止纔會中止抓包。

最小化安裝默認沒有安裝tcpdump,需安裝tcpdump軟件包

[root@lanquark ~]# yum -y install tcpdump.x86_64

抓包選項:

-c:指定要抓取的包數量
-i interface:指定tcpdump須要監聽的接口。若未指定該選項,將從系統接口列表中搜尋編號最小的已配置好的接口(不包括loopback接口,要抓取loopback接口使用tcpdump -i lo),
-n:對地址以數字方式顯式,不然顯式爲主機名,也就是說-n選項不作主機名解析。
-nn:除了-n的做用外,還把端口顯示爲數值,不然顯示端口服務名。
-N:不打印出host的域名部分。例如tcpdump將會打印'nic'而不是'nic.ddn.mil'。
-P:指定要抓取的包是流入仍是流出的包。能夠給定的值爲"in"、"out"和"inout",默認爲"inout"。
-w:將抓包數據輸出到文件中而不是標準輸出。能夠同時配合"-G time"選項使得輸出文件每time秒就自動切換到另外一個文件。可經過"-r"選項載入這些文件以進行分析和打印。
-r:從給定的數據包文件中讀取數據。使用"-"表示從標準輸入中讀取。

抓取指定網絡接口全部的流量包

//爲方便觀察,只抓取5次
[root@lanquark ~]# tcpdump -c 5 -nn -i ens32
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens32, link-type EN10MB (Ethernet), capture size 262144 bytes
23:08:41.653674 IP 192.168.1.211.22 > 192.168.1.9.2068: Flags [P.], seq 3174207465:3174207677, ack 2481139789, win 296, length 212
23:08:41.653874 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 212, win 2048, length 0
23:08:41.653951 IP 192.168.1.211.22 > 192.168.1.9.2068: Flags [P.], seq 212:408, ack 1, win 296, length 196
23:08:41.654040 IP 192.168.1.211.22 > 192.168.1.9.2068: Flags [P.], seq 408:668, ack 1, win 296, length 260
23:08:41.654150 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 668, win 2053, length 0
5 packets captured
6 packets received by filter
0 packets dropped by kernel

抓取ping包

[root@lanquark ~]# tcpdump -c 5 -nn -i ens32 icmp 
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens32, link-type EN10MB (Ethernet), capture size 262144 bytes
23:17:32.068866 IP 192.168.1.9 > 192.168.1.211: ICMP echo request, id 1, seq 1015, length 40
23:17:32.069009 IP 192.168.1.211 > 192.168.1.9: ICMP echo reply, id 1, seq 1015, length 40
23:17:33.071919 IP 192.168.1.9 > 192.168.1.211: ICMP echo request, id 1, seq 1016, length 40
23:17:33.072198 IP 192.168.1.211 > 192.168.1.9: ICMP echo reply, id 1, seq 1016, length 40
23:17:34.079352 IP 192.168.1.9 > 192.168.1.211: ICMP echo request, id 1, seq 1017, length 40
5 packets captured
6 packets received by filter
0 packets dropped by kernel

指定端口爲22

[root@lanquark ~]# tcpdump -c 5 -nn -i ens32 port 22
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens32, link-type EN10MB (Ethernet), capture size 262144 bytes
23:29:07.456966 IP 192.168.1.211.22 > 192.168.1.9.2068: Flags [P.], seq 3182328865:3182329077, ack 2481164789, win 318, length 212
23:29:07.457186 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 212, win 2053, length 0
23:29:07.457379 IP 192.168.1.211.22 > 192.168.1.9.2068: Flags [P.], seq 212:504, ack 1, win 318, length 292
23:29:07.457848 IP 192.168.1.211.22 > 192.168.1.9.2068: Flags [P.], seq 504:668, ack 1, win 318, length 164
23:29:07.457974 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 668, win 2051, length 0
5 packets captured
5 packets received by filter
0 packets dropped by kernel

指定多個條件

[root@lanquark ~]# tcpdump -c 5 -nn -i ens32 port 22 and host 192.168.1.9
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens32, link-type EN10MB (Ethernet), capture size 262144 bytes
23:31:40.918767 IP 192.168.1.211.22 > 192.168.1.9.2068: Flags [P.], seq 3182331785:3182331997, ack 2481166625, win 318, length 212
23:31:40.918954 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 212, win 2047, length 0
23:31:40.918994 IP 192.168.1.211.22 > 192.168.1.9.2068: Flags [P.], seq 212:504, ack 1, win 318, length 292
23:31:40.919120 IP 192.168.1.211.22 > 192.168.1.9.2068: Flags [P.], seq 504:668, ack 1, win 318, length 164
23:31:40.919232 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 668, win 2053, length 0
5 packets captured
6 packets received by filter
0 packets dropped by kernel

抓取到本機22端口包

[root@lanquark ~]#  tcpdump -c 10 -nn -i ens32 tcp dst port 22  
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens32, link-type EN10MB (Ethernet), capture size 262144 bytes
23:19:09.125587 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 3177829277, win 2053, length 0
23:19:09.175139 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 165, win 2052, length 0
23:19:09.226469 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 313, win 2051, length 0
23:19:09.278981 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 461, win 2051, length 0
23:19:09.331111 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 609, win 2050, length 0
23:19:09.383613 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 757, win 2050, length 0
23:19:09.435084 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 905, win 2049, length 0
23:19:09.486671 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 1053, win 2049, length 0
23:19:09.538120 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 1201, win 2048, length 0
23:19:09.589617 IP 192.168.1.9.2068 > 192.168.1.211.22: Flags [.], ack 1349, win 2047, length 0
10 packets captured
10 packets received by filter
0 packets dropped by kernel

解析包數據

[root@lanquark ~]# tcpdump -c 2 -q -XX -vvv -nn -i ens32 tcp dst port 22
tcpdump: listening on ens32, link-type EN10MB (Ethernet), capture size 262144 bytes
23:20:36.654263 IP (tos 0x0, ttl 128, id 9028, offset 0, flags [DF], proto TCP (6), length 40)
    192.168.1.9.2068 > 192.168.1.211.22: tcp 0
    0x0000:  000c 292f 92ee 1c1b 0da8 e183 0800 4500  ..)/..........E.
    0x0010:  0028 2344 4000 8006 535f c0a8 0109 c0a8  .(#D@...S_......
    0x0020:  01d3 0814 0016 93e3 62a9 bd69 e0f9 5010  ........b..i..P.
    0x0030:  0804 8689 0000 0000 0000 0000            ............
23:20:36.705318 IP (tos 0x0, ttl 128, id 9029, offset 0, flags [DF], proto TCP (6), length 40)
    192.168.1.9.2068 > 192.168.1.211.22: tcp 0
    0x0000:  000c 292f 92ee 1c1b 0da8 e183 0800 4500  ..)/..........E.
    0x0010:  0028 2345 4000 8006 535e c0a8 0109 c0a8  .(#E@...S^......
    0x0020:  01d3 0814 0016 93e3 62a9 bd69 e2cd 5010  ........b..i..P.
    0x0030:  0802 84b7 0000 0000 0000 0000            ............
2 packets captured
3 packets received by filter
0 packets dropped by kernel

指定抓包數量和存放位置

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

使用tcpdump -r命令查看1.cap內容

[root@lanquark ~]# tcpdump -r /tmp/1.cap 
reading from file /tmp/1.cap, link-type EN10MB (Ethernet)
23:22:24.839744 IP bogon.ssh > bogon.avauthsrvprtcl: Flags [P.], seq 3177835125:3177835273, ack 2481154401, win 296, length 148
23:22:24.839977 IP bogon.avauthsrvprtcl > bogon.ssh: Flags [.], ack 148, win 2047, length 0
23:22:25.444077 IP bogon.mdns > 224.0.0.251.mdns: 0*- [0q] 10/0/4 (Cache flush) PTR localhost.local., (Cache flush) TXT "deviceid=32:1e:d7:3d:d6:eb" "features=0x5A7FFFF7,0x1E" "flags=0x4" "model=AppleTV3,2" "pk=99FD4299889422515FBD27949E4E1E21B2AF50A454499E3D4BE75A4E0F55FE63" "pi=b08f5a79-db29-4384-b456-a4784d9e6055" "srcvers=220.68" "vv=2", PTR _airplay._tcp.local., PTR M-eM-.M-"M-eM-^NM-^EM-gM-^TM-5M-hM-'M-^F._airplay._tcp.local., (Cache flush) TXT "cn=0,1,3" "da=true" "et=0,3,5" "ft=0x5A7FFFF7,0x1E" "md=0,1,2" "am=AppleTV3,2" "pk=99FD4299889422515FBD27949E4E1E21B2AF50A454499E3D4BE75A4E0F55FE63" "sf=0x4" "tp=UDP" "vn=65537" "vs=220.68" "vv=2" "pw=false", PTR _raop._tcp.local., PTR 32:1e:d7:3d:d6:eb@M-eM-.M-"M-eM-^NM-^EM-gM-^TM-5M-hM-'M-^F._raop._tcp.local., (Cache flush) A 192.168.1.153, (Cache flush) SRV localhost.local.:7011 0 0, (Cache flush) SRV localhost.local.:47011 0 0 (757)
23:22:25.444277 IP bogon.mdns > bogon.mdns: 0*- [0q] 10/0/4 (Cache flush) PTR localhost.local., (Cache flush) TXT "deviceid=32:1e:d7:3d:d6:eb" "features=0x5A7FFFF7,0x1E" "flags=0x4" "model=AppleTV3,2" "pk=99FD4299889422515FBD27949E4E1E21B2AF50A454499E3D4BE75A4E0F55FE63" "pi=b08f5a79-db29-4384-b456-a4784d9e6055" "srcvers=220.68" "vv=2", PTR _airplay._tcp.local., PTR M-eM-.M-"M-eM-^NM-^EM-gM-^TM-5M-hM-'M-^F._airplay._tcp.local., (Cache flush) TXT "cn=0,1,3" "da=true" "et=0,3,5" "ft=0x5A7FFFF7,0x1E" "md=0,1,2" "am=AppleTV3,2" "pk=99FD4299889422515FBD27949E4E1E21B2AF50A454499E3D4BE75A4E0F55FE63" "sf=0x4" "tp=UDP" "vn=65537" "vs=220.68" "vv=2" "pw=false", PTR _raop._tcp.local., PTR 32:1e:d7:3d:d6:eb@M-eM-.M-"M-eM-^NM-^EM-gM-^TM-5M-hM-'M-^F._raop._tcp.local., (Cache flush) A 192.168.1.153, (Cache flush) SRV localhost.local.:7011 0 0, (Cache flush) SRV localhost.local.:47011 0 0 (757)
23:22:25.946465 IP bogon.55228 > 239.253.0.1.lhtp: UDP, length 171
23:22:26.110629 IP bogon.54514 > 255.255.255.255.7423: UDP, length 173
23:22:26.281494 IP 183.94.104.158.4562 > bogon.8558: UDP, length 31
23:22:26.281510 IP bogon.8558 > 183.94.104.158.4562: UDP, length 31
23:22:26.441221 IP bogon.49349 > 119.29.29.29.domain: 62080+ A? pan.baidu.com. (31)
23:22:26.456893 IP 119.29.29.29.domain > bogon.49349: 62080 2/0/0 CNAME yiyun.n.shifen.com., A 111.206.37.70 (76)

tshark命令

tsshark也是開源的抓包工具,很流行。有圖形前端。

系統默認沒有安裝,需安裝wareshark包

[root@lanquark ~]# yum -y install wireshark

用法

查看指定網卡80端口的1個web服務的訪問狀況(相似於web的訪問日誌):

[root@lanquark ~]# 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" 
tshark: -R without -2 is deprecated. For single-pass filtering use -Y.
Running as user "root" and group "root". This could be dangerous.
Capturing on 'nflog'
^C0 packets captured
//因當前沒有安裝web服務,因此沒有結果

tsahrk參考連接:

https://www.wireshark.org/docs/man-pages/tshark.html

https://wiki.wireshark.org/

https://wiki.wireshark.org/CaptureFilters

https://wiki.wireshark.org/Statistics


11、Linux網絡相關

ifconfig命令

Centos7默認沒有安裝,需安裝net-tools包

查看ip

[root@lanquark ~]# ifconfig
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.211  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::5114:2b77:d59a:bc78  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:2f:92:ee  txqueuelen 1000  (Ethernet)
        RX packets 59711  bytes 28497195 (27.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 59181  bytes 15741371 (15.0 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 64  bytes 5312 (5.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 64  bytes 5312 (5.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
//顯示指定接口
[root@lanquark ~]# ifconfig ens32
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.211  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::5114:2b77:d59a:bc78  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:2f:92:ee  txqueuelen 1000  (Ethernet)
        RX packets 59778  bytes 28511154 (27.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 59207  bytes 15745059 (15.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

設定臨時IP

[root@lanquark ~]# ifconfig ens32 192.168.1.222 255.255.255.0

ifdown和ifup命令 中止和啓動網卡。

//注意不要分開執行,這樣會致使你連不上去。
[root@lanquark ~]# ifdown ens32 && ifup ens32
Device 'ens32' successfully disconnected.
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)

虛擬網卡設置

[root@lanquark ~]# cd /etc/sysconfig/network-scripts/
[root@lanquark network-scripts]# cp ifcfg-ens32 ifcfg-ens32\:0
[root@lanquark network-scripts]# vim !$
vim ifcfg-ens32\:0
  1 TYPE="Ethernet"                                                                                 
  2 PROXY_METHOD="none"
  3 BROWSER_ONLY="no"
  4 BOOTPROTO="none"
  5 DEFROUTE="yes"
  6 IPV4_FAILURE_FATAL="no"
  7 IPV6INIT="yes"
  8 IPV6_AUTOCONF="yes"
  9 IPV6_DEFROUTE="yes"
 10 IPV6_FAILURE_FATAL="no"
 11 IPV6_ADDR_GEN_MODE="stable-privacy"
 12 NAME="ens32"
 13 UUID="f78a6b73-f367-498a-b3ee-47f6b29ceab4"
 14 DEVICE="ens32:0"
 15 ONBOOT="yes"
 16 IPADDR="192.168.1.211"
 17 PREFIX="24"
 18 GATEWAY="192.168.1.1"
 19 DNS1="8.8.8.8"
"ifcfg-ens32:0" 21L, 419C                                                         1,1           Top
  1 TYPE="Ethernet"
  2 PROXY_METHOD="none"
  3 BROWSER_ONLY="no"
  4 BOOTPROTO="none"
  5 DEFROUTE="yes"
  6 IPV4_FAILURE_FATAL="no"
  7 IPV6INIT="yes"
  8 IPV6_AUTOCONF="yes"
  9 IPV6_DEFROUTE="yes"
 10 IPV6_FAILURE_FATAL="no"
 11 IPV6_ADDR_GEN_MODE="stable-privacy"
 12 NAME="ens32:0"
 13 UUID="f78a6b73-f367-498a-b3ee-47f6b29ceab4"
 14 DEVICE="ens32:0"
 15 ONBOOT="yes"
 16 IPADDR="192.168.1.212"
 17 PREFIX="24"                                                                           
 21 IPV6_PRIVACY="no"
"ifcfg-ens32:0" 21L, 424C written   
//將設備名改成與配置文件名一致ens32:0
//網關,dns不須要,刪除便可。
//保存退出
//重啓網卡
[root@lanquark ~]# ifdown ens32 && ifup ens32
Device 'ens32' successfully disconnected.
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)
[root@lanquark ~]# ifconfig
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.211  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::5114:2b77:d59a:bc78  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:2f:92:ee  txqueuelen 1000  (Ethernet)
        RX packets 62799  bytes 29182237 (27.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 60343  bytes 15911826 (15.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
//虛擬網卡正常出現
ens32:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.212  netmask 255.255.255.0  broadcast 192.168.1.255
        ether 00:0c:29:2f:92:ee  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 64  bytes 5312 (5.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 64  bytes 5312 (5.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

mii-tool命令查看網卡是否鏈接

[root@lanquark ~]# mii-tool ens32
ens32: negotiated 1000baseT-FD flow-control, link ok

ethtool命令查看網卡是否鏈接

[root@lanquark ~]# ethtool ens32
Settings for ens32:
    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修改主機名

[root@lanquark ~]# hostnamectl set-hostname lanqaurk.com
//相應的配置文件爲/etc/hostname

DNS配置文件:/etc/resolv.conf

hosts文件: /etc/hosts


12、擴展

三次握手過程示意圖

所謂三次握手(Three-Way Handshake)即創建TCP鏈接,就是指創建一個TCP鏈接時,須要客戶端和服務端總共發送3個包以確認鏈接的創建。

(1)第一次握手:
Client將標誌位SYN置爲1,隨機產生一個值seq=J,並將該數據包發送給Server,Client進入SYN_SENT狀態,等待Server確認。

(2)第二次握手:
Server收到數據包後由標誌位SYN=1知道Client請求創建鏈接,Server將標誌位SYN和ACK都置爲1,ack=J+1,隨機產生一個值seq=K,並將該數據包
發送給Client以確認鏈接請求,Server進入SYN_RCVD狀態。

(3)第三次握手:
Client收到確認後,檢查ack是否爲J+1,ACK是否爲1,若是正確則將標誌位ACK置爲1,ack=K+1,並將該數據包發送給Server,Server檢查ack是否爲K+1,
ACK是否爲1,若是正確則鏈接創建成功,Client和Server進入ESTABLISHED狀態,完成三次握手,隨後Client與Server之間能夠開始傳輸數據了。

四次揮手過程示意圖

因爲TCP鏈接時全雙工的,所以,每一個方向都必需要單獨進行關閉,這一原則是當一方完成數據發送任務後,發送一個FIN來終止這一方向的鏈接,
收到一個FIN只是意味着這一方向上沒有數據流動了,即不會再收到數據了,可是在這個TCP鏈接上仍然可以發送數據,直到這一方向也發送了FIN。
首先進行關閉的一方將執行主動關閉,而另外一方則執行被動關閉,上圖描述的便是如此。

第一次揮手: Client發送一個FIN,用來關閉Client到Server的數據傳送,Client進入FIN_WAIT_1狀態。 第二次揮手: Server收到FIN後,發送一個ACK給Client,確認序號爲收到序號+1(與SYN相同,一個FIN佔用一個序號),Server進入CLOSE_WAIT狀態。 第三次揮手: Server發送一個FIN,用來關閉Server到Client的數據傳送,Server進入LAST_ACK狀態。 第四次揮手: Client收到FIN後,Client進入TIME_WAIT狀態,接着發送一個ACK給Server,確認序號爲收到序號+1,Server進入CLOSED狀態,完成四次揮手。

相關文章
相關標籤/搜索