2018-06-08筆記(平常運維一)

第十章 平常運維

10.1 使用w、uptime查看系統負載

[root@localhost ~]# w
 22:02:05 up  1:12,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.66.1     20:51    5.00s  0.03s  0.00s w

第一行從左面開始顯示的信息依次爲:時間,系統運行時間,登陸用戶數,系統平均負載(1分鐘、5分鐘、15分鐘、使用cpu的活動的進程有多少個)。
第二行開始以及下面全部的行,當前登陸的都有哪些用戶,以及他們是從哪裏登陸的等等。linux

load averag第一個數值表示1分鐘內系統的平均負載值;第二個數值表示5分鐘內系統的平均負載值;第三個數值表示15分鐘系統的平均負載值。這個值的意義是,單位時間段內CPU活動進程數。固然這個值越大就說明你的服務器壓力越大。通常狀況下這個值只要不超過服務器的cpu數量就沒有關係。
查看cup信息
#cat /proc/cpuinfoios

[root@localhost ~]# cat /proc/cpuinfo
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 60
model name  : Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz
stepping    : 3
microcode   : 0x1e
cpu MHz     : 2393.712
cache size  : 6144 KB
physical id : 0
siblings    : 1
core id     : 0

processor數量+1表示有多少個邏輯cpu,若是數字爲1,則表示有2個邏輯cpu,以此類推,物理cpu能夠查看physical id這個字段
#uptime命令能夠查看機器運行時間,登錄用戶有幾個,系統平均負載web

[root@localhost ~]# uptime
 20:24:09 up  1:37,  2 users,  load average: 0.00, 0.01, 0.05

10.2vmstat命令

[root@localhost ~]# 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 775360   2076 121164    0    0    16     3   56   61  0  0 100  0  0

vmstat命令打印的結果共分爲6部分:procs, memory, swap, io, system, cpu. 重點關注一下r b si so bi bo幾列
1)procs 顯示進程相關信息緩存

r :表示運行和等待cpu時間片的進程數,若是長期大於服務器cpu的個數,則說明cpu不夠用了;bash

b :表示等待資源的進程數,好比等待I/O, 內存等,這列的值若是長時間大於1,則須要關注一下了;服務器

2)memory 內存相關信息網絡

swpd :表示切換到交換分區中的內存數量 ;多線程

free :當前空閒的內存數量;運維

buff :緩衝大小,(即將寫入磁盤的);dom

cache :緩存大小,(從磁盤中讀取的);

3)swap 內存交換狀況

si :由交換區寫入到內存的數據量;

so :由內存寫入到交換區的數據量;

4)io 磁盤使用狀況

bi :從塊設備讀取數據的量(讀磁盤);

bo: 從塊設備寫入數據的量(寫磁盤);

5)system 顯示採集間隔內發生的中斷次數

in :表示在某一時間間隔中觀測到的每秒設備中斷數;

cs :表示每秒產生的上下文切換次數;

6)CPU 顯示cpu的使用狀態

us :顯示了用戶下所花費 cpu 時間的百分比;

sy :顯示系統花費cpu時間百分比;

id :表示cpu處於空閒狀態的時間百分比;

wa :表示I/O等待所佔用cpu時間百分比;

st :表示被偷走的cpu所佔百分比

vmstat 查看系統狀態的時候,一般都是使用這樣的形式來看的

[root@localhost ~]# vmstat 1 5

或者

[root@localhost ~]# vmstat 1

前面表示,每隔一秒鐘打印一次狀態,共打印5次,然後面的表示每隔1秒打印一次狀態,一直打印,能夠按 Ctrl + c 結束

10.3 top 命令

top 命令能夠動態顯示進程所佔系統資源

[root@localhost ~]# top
top - 20:39:26 up  1:52,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  87 total,   1 running,  86 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 :   999700 total,   774940 free,   101488 used,   123272 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   750596 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                           
  1604 root      20   0  157632   2184   1548 R  0.3  0.2   0:00.01 top                               
     1 root      20   0  125376   3812   2540 S  0.0  0.4   0:01.16 systemd                           
     2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd                          
     3 root      20   0       0      0      0 S  0.0  0.0   0:00.06 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.14 kworker/u256:0                    
     7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0                       
     8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh                            
     9 root      20   0       0      0      0 S  0.0  0.0   0:00.27 rcu_sched                         
    10 root      rt   0       0      0      0 S  0.0  0.0   0:00.03 watchdog/0                        
    12 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs                         
    13 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns                             
    14 root      20   0       0      0      0 S  0.0  0.0   0:00.00 khungtaskd                        
    15 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 writeback

從上面能夠看出
總共87個任務(進程),1個任務處於運行中,86個進程處於休眠狀態,0箇中止的,0個殭屍進程

zombie:殭屍進程,主進程已經意外中止,剩下一些子進程

st:表示被偷走的cpu百分比,若是服務器作了虛擬化,那麼一些子機會偷走主機的cpu

若是us長時間佔用cpu長期大於20-30%,會使cpu匱乏,對cpu有害

進程排序:默認按cpu佔用率排序,若是按下M鍵,則按照內存佔用大小排序,再按下P,恢復爲cpu排序

PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                           
   778 root      20   0  562428  18644   5936 S  0.0  1.9   0:00.99 tuned                             
   509 root      20   0  210220  13912   3244 S  0.0  1.4   0:00.40 rsyslogd                          
   526 polkitd   20   0  534172  12040   4628 S  0.0  1.2   0:00.13 polkitd                           
   515 root      20   0  556272   9308   6952 S  0.0  0.9   0:00.67 NetworkManager                    
   522 root      20   0  305332   6280   4900 S  0.0  0.6   0:05.76 vmtoolsd                          
   521 root      20   0   99644   6116   4512 S  0.0  0.6   0:00.09 VGAuthService                     
   848 root      20   0  148360   5412   4112 S  0.0  0.5   0:00.27 sshd                              
  1515 root      20   0  148360   5408   4112 S  0.0  0.5   0:00.17 sshd                              
   780 root      20   0  106040   4096   3116 S  0.0  0.4   0:00.08 sshd

RES :實際佔用的物理內存大小,以KB爲單位
按下數字1:顯示全部cpu的佔用率

%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

按下q鍵能夠退出top
top -c 能夠查看到全局的進程命令

[root@localhost ~]# top -c
top - 20:59:03 up  2:12,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  88 total,   1 running,  87 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   999700 total,   774800 free,   101620 used,   123280 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   750460 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                           
     1 root      20   0  125376   3812   2540 S  0.0  0.4   0:01.17 /usr/lib/systemd/systemd --switch+
     2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 [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.18 [kworker/u256:0]                  
     7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 [migration/0]                     
     8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 [rcu_bh]                          
     9 root      20   0       0      0      0 S  0.0  0.0   0:00.29 [rcu_sched]                       
    10 root      rt   0       0      0      0 S  0.0  0.0   0:00.03 [watchdog/0]                      
    12 root      20   0       0      0      0 S  0.0  0.0   0:00.00 [kdevtmpfs]                       
    13 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 [netns]                           
    14 root      20   0       0      0      0 S  0.0  0.0   0:00.00 [khungtaskd]

top -bn1 靜態的一次性地顯示全部的信息,適合寫腳本時使用

[root@localhost ~]# top -bn1
top - 20:59:43 up  2:13,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  88 total,   1 running,  87 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 :   999700 total,   774924 free,   101496 used,   123280 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   750584 avail Mem 
   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
     1 root      20   0  125376   3812   2540 S  0.0  0.4   0:01.17 systemd
     2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 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.18 kworker/u256:0
     7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
     8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh
     9 root      20   0       0      0      0 S  0.0  0.0   0:00.29 rcu_sched
    10 root      rt   0       0      0      0 S  0.0  0.0   0:00.03 watchdog/0
    12 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs
    13 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns
    14 root      20   0       0      0      0 S  0.0  0.0   0:00.00 khungtaskd
    15 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 writeback
    16 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kintegrityd

10.4 sar命令

yum install -y sysstat   若是沒有sar命令能夠安裝這個包
sar -n DEV  網卡流量
 sar -q  系統負載
 sar -b  磁盤讀寫
 sar -f /var/log/sa/saxx  歷史文件

sar命令第一次使用會提示沒有文件或目錄,那是由於sar不加選項會調用系統保留的歷史文件 /var/log/sa下的文件,要想正常調用,必須等到10分鐘的整數時間點纔會有這個文件

[root@localhost ~]# sar
沒法打開 /var/log/sa/sa10: 沒有那個文件或目錄

sar -n DEV 1 10 查看網卡流量,每一秒打印一次,總共打印10次

[root@localhost ~]# sar -n DEV 1 10
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain)     2018年06月10日     _x86_64_    (1 CPU)
21時10分31秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時10分32秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時10分32秒     ens33      1.00      1.00      0.06      0.20      0.00      0.00      0.00
21時10分32秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時10分33秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時10分33秒     ens33      1.00      1.00      0.06      0.40      0.00      0.00      0.00
21時10分33秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時10分34秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時10分34秒     ens33      1.00      1.00      0.06      0.40      0.00      0.00      0.00
21時10分34秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時10分35秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時10分35秒     ens33      1.01      1.01      0.06      0.40      0.00      0.00      0.00
21時10分35秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時10分36秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時10分36秒     ens33      2.00      2.00      0.12      0.46      0.00      0.00      0.00

其中IFACE表示網卡的名字,r表示recieve接受的,t表示transport傳輸出去的

rxpck表示接收到的數據包量,幾千的數據包量屬於正常的,若是上萬就可能被×××了,須要借用抓包工具查看

txpck表示傳輸出去的數據包量

rxkB表示接收到的數據量爲多少KB

txkB表示傳輸出去的數據量爲多少KB
sar -f /var/log/sa/saxx 查看歷史信息

[root@localhost ~]# sar -n DEV -f /var/log/sa/sa10
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain)     2018年06月10日     _x86_64_    (1 CPU)
20時20分01秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
20時30分01秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
20時30分01秒     ens33      0.79      0.24      0.05      0.02      0.00      0.00      0.00
20時40分01秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
20時40分01秒     ens33      0.71      0.22      0.04      0.13      0.00      0.00      0.00
20時50分01秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00

sar -n DEV -f /var/log/sa/sa10 能夠查看歷史文件10號的網卡流量信息,sa10文件爲10號的文件,/var/log/sa目錄下的文件最多保留1個月, 當天/var/log/sa/目錄下會生成相似日期爲sa10的二進制文件,次日這個目錄下會生成sar10的文件,sar10的文件能夠cat查看
sar -q查看歷史負載

[root@localhost ~]# sar -q
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain)     2018年06月10日     _x86_64_    (1 CPU)
20時20分01秒   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
20時30分01秒         1       103      0.00      0.01      0.05         0
20時40分01秒         2       104      0.00      0.01      0.05         0
20時50分01秒         1       105      0.02      0.03      0.05         0
21時00分01秒         1       104      0.00      0.01      0.05         0
21時10分01秒         1       105      0.00      0.02      0.05         0
21時20分01秒         1       104      0.00      0.01      0.05         0
21時30分01秒         1       103      0.00      0.01      0.05         0
平均時間:         1       104      0.00      0.01      0.05         0

sar -b 查看磁盤

[root@localhost ~]# sar -b
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain)     2018年06月10日     _x86_64_    (1 CPU)

20時20分01秒       tps      rtps      wtps   bread/s   bwrtn/s
20時30分01秒      0.08      0.00      0.07      0.04      0.81
20時40分01秒      0.05      0.00      0.05      0.11      0.69
20時50分01秒      0.05      0.00      0.05      0.00      0.46

10.5 nload命令(實時動態查看網卡流量)

若是沒有nload命令,須要安裝包,由於默認源裏面沒有nload包,因此須要安裝擴展源

yum install epel-release 
yum install nload
Device ens33 [192.168.66.130] (1/2):
=======================================================================================================
Incoming:
                                                                      Curr: 944.00 Bit/s
                                                                      Avg: 1.11 kBit/s
                                                                      Min: 936.00 Bit/s
                                                                      Max: 1.84 kBit/s
                                                                      Ttl: 529.79 kByte
Outgoing:

                                                                      Curr: 8.27 kBit/s
                                                                      Avg: 8.14 kBit/s
                                                                      Min: 4.23 kBit/s
                                                                      Max: 8.73 kBit/s
                                                                      Ttl: 3.89 MByte

按左右方向鍵能夠切換網卡,按q退出

有×××時Incoming的流量會比較大,

佔用帶寬看的是Outgoing的流量

10.6iostat命令 iotop命令,查看磁盤io

有時候查看到的cpu和內存還有剩餘,可是系統負載仍是很高,咱們用vmstat命令查看發現b列或wa列值比較大,說明系統磁盤有瓶頸,這時候就須要去查看磁盤的狀態了
安裝包:yum install -y sysstat 和sar命令是同一個包
直接輸入iostat命令

[root@localhost ~]# iostat
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain)     2018年06月10日     _x86_64_    (1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.03    0.00    0.10    0.17    0.00   99.70

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               1.06        10.01         2.35      97333      22884
scd0              0.00         0.11         0.00       1028          0

能夠看到cpu負載和磁盤的讀寫數據信息
iostat -x
加上-x選項指標會更多,其中一個比較重要的是%util,這個指標表明的是cpu等待io進程的時間比例,若是這個值很大,說明磁盤io很忙,則讀寫rkB/s wkB/s 也會很大,若是讀寫值不大,則說明磁盤有問題或故障

[root@localhost ~]# iostat -x
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain)     2018年06月10日     _x86_64_    (1 CPU)
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.03    0.00    0.10    0.16    0.00   99.70
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.02    0.47    0.57     9.83     2.35    23.37     0.01         12.47   12.36   12.56   2.96   0.31
scd0              0.00         0.00    0.00    0.00     0.10     0.00   114.22     0.00         23.83   23.83    0.00  22.78   0.00

iotop
安裝包yum install -y iotop
iotop動態顯示磁盤信息,將磁盤使用狀況根據佔用IO的比例大小排序後展現出來

[root@localhost ~]# 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:     190.20 K/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-r~stem --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]
   523 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % rsyslogd -n [rs:main Q:Reg]
    12 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kdevtmpfs]
    13 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [netns]
    14 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [khungtaskd]
    15 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [writeback]
    16 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kintegrityd]
    17 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [bioset]
    18 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kblockd]
    19 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [md]
   533 be/4 chrony  0.00 B/s    0.00 B/s  0.00 %  0.00 % chronyd
   535 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % agetty --nocl

10.7free命令,查看內存使用狀況

[root@localhost ~]# free
                    total           used        free             shared  buff/cache   available
Mem:         999700      102480      608556        6868      288664      725444
Swap:       2097148           0     2097148
[root@localhost ~]# free -m
                    total        used        free      shared  buff/cache   available
Mem:            976         100         594           6         281         708
Swap:          2047           0        2047
[root@localhost ~]# free -h
                     total        used        free      shared  buff/cache   available
Mem:           976M        100M        594M        6.7M        281M        708M
Swap:          2.0G          0B        2.0G

直接輸入free能夠看到內存的使用信息,默認爲kb,-m又MB顯示,-h又合適的單位顯示,其中:
total=used+free+buff/cache,available包含了free和buff/cache剩餘部分
buff:緩衝,當cpu寫數據到磁盤時,被暫放在內存中 的數據
cache:緩存,當cpu從磁盤讀取數據時,被暫放在內存中的數據

10.8ps命令 查看系統進程

ps命令用於報告當前系統的進程狀態。能夠搭配kill指令隨時中斷、刪除沒必要要的程序。ps命令是最基本同時也是很是強大的進程查看命令,使用該命令能夠肯定有哪些進程正在運行和運行的狀態、進程是否結束、進程有沒有僵死、哪些進程佔用了過多的資源等等,總之大部分信息都是能夠經過執行該命令獲得的。
用法:ps aux或者ps -elf
a:顯示現行終端機下的全部程序,包括其餘用戶的程序
u:以用戶爲主的格式來顯示程序情況
x:顯示全部程序,不以終端機來區分
-e:此選項的效果和a選項相同
e:列出程序時,顯示每一個程序所使用的環境變量
-f:顯示UID,PPIP,C與STIME欄位
f:用ASCII字符顯示樹狀結構,表達程序間的相互關係
-l或l:採用詳細的格式來顯示程序情況

[root@localhost ~]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.3 125376  3812 ?        Ss   18:46   0:01 /usr/lib/systemd/systemd --switched-r
root          2  0.0  0.0      0     0 ?        S    18:46   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    18:46   0:00 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<   18:46   0:00 [kworker/0:0H]
root          6  0.0  0.0      0     0 ?        S    18:46   0:00 [kworker/u256:0]
root          7  0.0  0.0      0     0 ?        S    18:46   0:00 [migration/0]
root          8  0.0  0.0      0     0 ?        S    18:46   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        R    18:46   0:00 [rcu_sched]
root         10  0.0  0.0      0     0 ?        S    18:46   0:00 [watchdog/0]
root         12  0.0  0.0      0     0 ?        S    18:46   0:00 [kdevtmpfs]
root         13  0.0  0.0      0     0 ?        S<   18:46   0:00 [netns]
root         14  0.0  0.0      0     0 ?        S    18:46   0:00 [khungtaskd]
root         15  0.0  0.0      0     0 ?        S<   18:46   0:00 [writeback]
root         16  0.0  0.0      0     0 ?        S<   18:46   0:00 [kintegrityd]
root         17  0.0  0.0      0     0 ?        S<   18:46   0:00 [bioset]
root         18  0.0  0.0      0     0 ?        S<   18:46   0:00 [kblockd]
root         19  0.0  0.0      0     0 ?        S<   18:46   0:00 [md]
root         25  0.0  0.0      0     0 ?        S    18:46   0:00 [kswapd0]
root         26  0.0  0.0      0     0 ?        SN   18:46   0:00 [ksmd]
root         27  0.0  0.0      0     0 ?        SN   18:46   0:00 [khugepaged]
root         28  0.0  0.0      0     0 ?        S<   18:46   0:00 [crypto]
root         36  0.0  0.0      0     0 ?        S<   18:46   0:00 [kthrotld]
root         37  0.0  0.0      0     0 ?        S    18:46   0:00 [kworker/u256:1]

PID :進程的id,這個id頗有用,在linux中內核管理進程就得靠pid來識別和管理某一個程,好比我想終止某一個進程,則用 ‘kill 進程的pid 有時並不能殺掉,則須要加一個-9選項了 kill -9 進程pid
STAT :表示進程的狀態,進程狀態分爲如下幾種(不要求記住,但要了解)
D 不能中斷的進程(一般爲IO)
R 正在運行中的進程
S 已經中斷的進程,一般狀況下,系統中大部分進程都是這個狀態
T 已經中止或者暫停的進程,若是咱們正在運行一個命令,好比說 sleep 10 若是咱們按一下ctrl -z 讓他暫停,那麼咱們用ps查看就會顯示T這個狀態
W 這個好像是說,從內核2.6xx 之後,表示爲沒有足夠的內存頁分配
X 已經死掉的進程(這個好像歷來不會出現)
Z 殭屍進程,殺不掉,打不死的垃圾進程,佔系統一小點資源,不過沒有關係。若是太多,就有問題了。通常不會出現。
< 高優先級進程
N 低優先級進程
L 在內存中被鎖了內存分頁
s 主進程
l 多線程進程

  • 表明在前臺運行的進程
    ps aux用途:檢查某個進程有沒有,使用命令 # ps aux |grep 進程名
    [root@localhost ~]# ps aux |grep sshd
    root        780  0.0  0.4 106040  4096 ?        Ss   18:46   0:00 /usr/sbin/sshd -D
    root        848  0.0  0.5 148360  5412 ?        Ss   18:48   0:00 sshd: root@pts/0
    root       1515  0.0  0.5 148360  5408 ?        Ss   20:16   0:00 sshd: root@pts/1
    root       1958  0.0  0.0 112720   968 pts/1    R+   22:03   0:00 grep --color=auto sshd

    ls -l /proc/進程號,能夠查看某個進程的啓動位置

    [root@localhost ~]# ls -l /proc/780
    總用量 0
    dr-xr-xr-x 2 root root 0 6月  10 22:04 attr
    -rw-r--r-- 1 root root 0 6月  10 22:04 autogroup
    -r-------- 1 root root 0 6月  10 22:04 auxv
    -r--r--r-- 1 root root 0 6月   9 20:49 cgroup
    --w------- 1 root root 0 6月  10 22:04 clear_refs
    -r--r--r-- 1 root root 0 6月   9 20:49 cmdline
    -rw-r--r-- 1 root root 0 6月   9 20:49 comm
    -rw-r--r-- 1 root root 0 6月  10 22:04 coredump_filter
    -r--r--r-- 1 root root 0 6月  10 22:04 cpuset
    lrwxrwxrwx 1 root root 0 6月  10 22:04 cwd -> /
    -r-------- 1 root root 0 6月  10 21:53 environ
    lrwxrwxrwx 1 root root 0 6月   9 20:49 exe -> /usr/sbin/sshd
    ...

    10.9netstat命令查看網絡情況

    2018-06-08筆記(平常運維一)
    netstat -lnp查看監聽端口,l表示listen,n以數字顯示,p表示port端口

    [root@localhost ~]# 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      780/sshd            
    tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1475/sendmail: acce 
    tcp6       0      0 :::22                   :::*                    LISTEN      780/sshd            
    udp        0      0 127.0.0.1:323           0.0.0.0:*                           533/chronyd         
    udp6       0      0 ::1:323                 :::*                                533/chronyd         
    raw6       0      0 :::58                   :::*                    7           515/NetworkManager  
    Active UNIX domain sockets (only servers)
    Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
    unix  2      [ ACC ]     STREAM     LISTENING     12554    1/systemd            /run/systemd/private
    unix  2      [ ACC ]     SEQPACKET  LISTENING     12570    1/systemd            /run/udev/control
    unix  2      [ ACC ]     STREAM     LISTENING     14921    1/systemd            /var/run/dbus/system_bus_socket
    unix  2      [ ACC ]     STREAM     LISTENING     8423     1/systemd            /run/systemd/journal/stdout
    unix  2      [ ACC ]     STREAM     LISTENING     16111    521/VGAuthService    /var/run/vmware/guestServicePipe

    同一臺服務器的兩個進程間使用socket協議通訊
    netstat -lntp 只查看tcp協議端口狀態,t表示tcp

    [root@localhost ~]# 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      780/sshd            
    tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1475/sendmail: acce 
    tcp6       0      0 :::22                   :::*                    LISTEN      780/sshd

    netstat -an 查看全部的鏈接狀態

    [root@localhost ~]# 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.66.130:22       192.168.66.1:55443      ESTABLISHED
    tcp        0      0 192.168.66.130:22       192.168.66.1:52857      ESTABLISHED
    tcp6       0      0 :::22                   :::*                    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      [ ]         DGRAM                    15872    /var/run/chrony/chronyd.sock
    unix  2      [ ACC ]     STREAM     LISTENING     12554    /run/systemd/private
    unix  2      [ ACC ]     SEQPACKET  LISTENING     12570    /run/udev/control
    unix  2      [ ]         DGRAM                    12613    /run/systemd/shutdownd
    unix  2      [ ACC ]     STREAM     LISTENING     14921    /var/run/dbus/system_bus_socket
    unix  2      [ ]         DGRAM                    8413     /run/systemd/notify
    unix  2      [ ]         DGRAM                    8415     /run/systemd/cgroups-agent
    unix  2      [ ACC ]     STREAM     LISTENING     8423     /run/systemd/journal/stdout
    unix  5      [ ]         DGRAM                    8426     /run/systemd/journal/socket
    unix  13     [ ]         DGRAM                    8428     /dev/log
    unix  2      [ ACC ]     STREAM     LISTENING     16111    /var/run/vmware/guestServicePipe
    unix  3      [ ]         STREAM     CONNECTED     18586    /var/run/dbus/system_bus_socket
    unix  3      [ ]         STREAM     CONNECTED     14992    
    unix  3      [ ]         STREAM     CONNECTED     15309    
    unix  3      [ ]         STREAM     CONNECTED     14953    
    unix  2      [ ]         DGRAM                    28422    
    unix  3      [ ]         STREAM     CONNECTED     15310    /run/systemd/journal/stdout
    unix  2      [ ]         DGRAM                    16114    
    unix  3      [ ]         STREAM     CONNECTED     15124    /var/run/dbus/system_bus_socket
    unix  2      [ ]         DGRAM                    27899    
    unix  3      [ ]         STREAM     CONNECTED     15560    /run/systemd/journal/stdout
    unix  2      [ ]         DGRAM                    28249    
    unix  2      [ ]         DGRAM                    19064    
    unix  3      [ ]         STREAM     CONNECTED     15559    
    unix  2      [ ]         DGRAM                    16113    
    unix  3      [ ]         STREAM     CONNECTED     15358    /run/systemd/journal/stdout
    unix  3      [ ]         STREAM     CONNECTED     15357    
    unix  3      [ ]         STREAM     CONNECTED     17883    
    unix  3      [ ]         STREAM     CONNECTED     17953    
    unix  3      [ ]         STREAM     CONNECTED     18585    
    unix  3      [ ]         STREAM     CONNECTED     14993    /run/systemd/journal/stdout
    unix  2      [ ]         DGRAM                    12955    
    unix  3      [ ]         STREAM     CONNECTED     16180    /var/run/dbus/system_bus_socket
    unix  3      [ ]         STREAM     CONNECTED     17884    /run/systemd/journal/stdout
    unix  3      [ ]         STREAM     CONNECTED     17954    /run/systemd/journal/stdout
    unix  3      [ ]         STREAM     CONNECTED     16010    /var/run/dbus/system_bus_socket
    unix  2      [ ]         DGRAM                    16024    
    unix  3      [ ]         STREAM     CONNECTED     15821    
    unix  3      [ ]         STREAM     CONNECTED     15822    /run/systemd/journal/stdout
    unix  3      [ ]         STREAM     CONNECTED     14784    
    unix  3      [ ]         STREAM     CONNECTED     15180    
    unix  3      [ ]         STREAM     CONNECTED     16009    
    unix  3      [ ]         STREAM     CONNECTED     14783    
    unix  3      [ ]         STREAM     CONNECTED     16043    
    unix  3      [ ]         STREAM     CONNECTED     15123    
    unix  2      [ ]         DGRAM                    15995    
    unix  2      [ ]         DGRAM                    15661    
    unix  3      [ ]         STREAM     CONNECTED     15181    /run/systemd/journal/stdout
    unix  3      [ ]         STREAM     CONNECTED     13347    /run/systemd/journal/stdout
    unix  2      [ ]         DGRAM                    15183    
    unix  2      [ ]         DGRAM                    15933    
    unix  3      [ ]         STREAM     CONNECTED     16179    
    unix  3      [ ]         DGRAM                    13373    
    unix  2      [ ]         DGRAM                    14774    
    unix  3      [ ]         STREAM     CONNECTED     15668    /var/run/dbus/system_bus_socket
    unix  3      [ ]         STREAM     CONNECTED     13346    
    unix  3      [ ]         STREAM     CONNECTED     15667    
    unix  3      [ ]         STREAM     CONNECTED     16044    /var/run/dbus/system_bus_socket
    unix  3      [ ]         DGRAM                    13374    
    unix  3      [ ]         STREAM     CONNECTED     15122    
    unix  2      [ ]         DGRAM                    15824    
    unix  2      [ ]         DGRAM                    13353

    關於網絡鏈接的一些狀態能夠查看 tcp三次握手四次揮手的文章 :
    http://www.javashuo.com/article/p-ucrnmzvv-gg.html
    統計tcp鏈接各類狀態的數量
    netstat -an |awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}'

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

    若是處於ESTABLISHED的狀態不少,說明系統很忙,通常1000之內 的數字服務器都可以接受,若是幾萬個說明有可能受到×××
    ss -an 用這個命令查看網絡狀態時不會顯示進程的名字和PID

    [root@localhost ~]# ss -an
    Netid State      Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
    nl    UNCONN     0      0                     0:2055209475                         *                   
    nl    UNCONN     0      0                     0:0                                  *                   
    nl    UNCONN     0      0                     0:2055209475                         *                   
    nl    UNCONN     768    0                     4:0                                  *                   
    nl    UNCONN     4352   0                     4:2025                               *                   
    nl    UNCONN     0      0                     6:0                                  *                   
    nl    UNCONN     0      0                     7:0                                  *                   
    nl    UNCONN     0      0                     9:1                                  *                   
    nl    UNCONN     0      0                     9:0                                  *                   
    nl    UNCONN     0      0                     9:486                                *                   
    nl    UNCONN     0      0                    10:0                                  *                   
    nl    UNCONN     0      0                    11:0                                  *                   
    nl    UNCONN     0      0                    15:-4117                              *                   
    nl    UNCONN     0      0                    15:527                                *                   
    nl    UNCONN     0      0                    15:-4119                              *                   
    nl    UNCONN     0      0                    15:-4120                              *                   
    nl    UNCONN     0      0                    15:0                                  *                   
    nl    UNCONN     0      0                    15:778                                *                   
    nl    UNCONN     0      0                    15:-4107                              *                   
    nl    UNCONN     0      0                    15:515                                *

    10.10Linux下抓包

    抓包工具:tcpdump
    用法:tcpdump -nn

    [root@localhost ~]# tcpdump -nn
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

    若是出現上面狀況,說明網卡名字比較特殊,須要在命令中指定網卡名字
    tcpdump -nn -i 網卡名字
    命令中的兩個n表示將下面中的第一IP和端口以數字的方式展現出來,不指定的話會顯示爲主機名和端口名稱

    22:22:04.599754 IP 192.168.66.1.55443 > 192.168.66.130.22: Flags [.], ack 1205320, win 2049, length 0
    22:22:04.599762 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1205320:1205500, ack 157, win 274, length 180
    22:22:04.599866 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1205500:1205776, ack 157, win 274, length 276
    22:22:04.599975 IP 192.168.66.1.55443 > 192.168.66.130.22: Flags [.], ack 1205776, win 2047, length 0
    22:22:04.599983 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1205776:1205956, ack 157, win 274, length 180
    22:22:04.600087 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1205956:1206232, ack 157, win 274, length 276
    22:22:04.600199 IP 192.168.66.1.55443 > 192.168.66.130.22: Flags [.], ack 1206232, win 2053, length 0
    22:22:04.600207 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1206232:1206412, ack 157, win 274, length 180
    22:22:04.600310 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1206412:1206688, ack 157, win 274, length 276
    22:22:04.600418 IP 192.168.66.1.55443 > 192.168.66.130.22: Flags [.], ack 1206688, win 2051, length 0
    22:22:04.600426 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1206688:1206868, ack 157, win 274, length 180
    22:22:04.600535 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1206868:1207144, ack 157, win 274, length 276
    22:22:04.600644 IP 192.168.66.1.55443 > 192.168.66.130.22: Flags [.], ack 1207144, win 2049, length 0
    22:22:04.600653 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1207144:1207324, ack 157, win 274, length 180
    22:22:04.600757 IP 192.168.66.130.22 > 192.168.66.1.55443: Flags [P.], seq 1207324:1207600, ack 157, win 274, length 276

    咱們主要查看的是兩個IP間數據的流向和length,通常咱們看到的包都是TCP的包,若是看到UDP的包,頗有多是被×××了
    # tcpdump -nn port 端口號 //抓取指定端口號
    # tcpdump -nn not port 端口號 //不抓取指定端口號
    指定包的長度
    \ # tcpdump -nn -c 長度值
    將抓取到的數據存儲到指定文件-w
    \ # tcpdump -nn -w /tmp/1.cap

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

    1.cap文件是不能直接cat的,不然會亂碼,由於這個文件是從服務器網卡中捕獲的數據包,就是咱們真正通訊的數據,若是想查看這個文件,使用命令# tcpdump -r /tmp/1.cap

    [root@localhost ~]# tcpdump -r /tmp/1.cap 
    reading from file /tmp/1.cap, link-type EN10MB (Ethernet)
    22:26:18.404881 IP localhost.localdomain.ssh > 192.168.66.1.55443: Flags [P.], seq 831942262:831942410, ack 1313270563, win 296, length 148
    22:26:18.405084 IP 192.168.66.1.55443 > localhost.localdomain.ssh: Flags [.], ack 148, win 2053, length 0
    22:26:18.683738 ARP, Request who-has gateway tell 192.168.66.1, length 46
    22:26:19.395351 ARP, Request who-has gateway tell 192.168.66.1, length 46
    22:26:20.394756 ARP, Request who-has gateway tell 192.168.66.1, length 46
    22:26:21.684527 ARP, Request who-has gateway tell 192.168.66.1, length 46

    tshark命令

先要安裝# yum install wireshark 包

wireshark工具和tcpdump工具相似,wireshark就有一個tshak
使用下述命令能夠查看80端口web的訪問狀況

\ # 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"

[root@izwz97d3catjonpf5xybg0z ~]#  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 'eth0'
"Jun 10, 2018 22:31:37.137837401 CST"   172.18.158.103      POST    /agent/metrics/putLines

10.11 linux網絡相關

一、使用# ifconfig查看網卡ip

若是沒有命令須要安裝對應的包# yum install net-tools

# ifconfig

[root@izwz97d3catjonpf5xybg0z ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.158.103  netmask 255.255.240.0  broadcast 172.18.159.255
        ether 00:16:3e:0e:82:2c  txqueuelen 1000  (Ethernet)
        RX packets 3686075  bytes 766773356 (731.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3150028  bytes 890353393 (849.1 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
        loop  txqueuelen 1  (Local Loopback)
        RX packets 692866  bytes 42992964 (41.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 692866  bytes 42992964 (41.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

二、 ifconfig -a
選項-a表示查看全部的網卡,包括被down掉的網卡或者沒有ip的網卡
三、 ifdown 網卡名 && ifup 網卡名
開關閉卡後當即開啓網卡,以防止主機斷網而致使遠程登陸終端鏈接不到主機

[root@localhost ~]# ifdown ens33 && ifup ens33
成功斷開設備 'ens33'。
鏈接已成功激活(D-Bus 活動路徑:/org/freedesktop/NetworkManager/ActiveConnection/3)

四、查看網卡是否鏈接
mii-tool 網卡名字

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

link ok說明鏈接正常
或者另外一個命令查看# ethtool 網卡名字

[root@localhost ~]# 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 新主機名

[root@localhost ~]# hostnamectl set-hostname luo
[root@localhost ~]# bash
[root@luo ~]#

退出當前終端後從新登陸或者從新打開一個新的bash便可看到新的主機名
要想永久生效,編輯配置文件/etc/hostname
六、DNS配置文件/etc/resolv.conf

# Generated by NetworkManager
nameserver 114.114.114.114
nameserver 8.8.8.8

七、/etc/hosts文件
這個文件用來定義域名指向到哪一個ip

[root@localhost ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

八、其餘一些擴展的知識
ip命令使用

  1. 設置和刪除IP
    ip addr add 192.168.10.10/24 dev eth0ip addr show eth0ip addr del 192.168.10.10/24 dev eth0
  2. 路由相關設置ip route showip route add default via 192.168.10.1ip route add 192.168.5.0/24 dev eth0ip route del 192.168.10.1另外用route命令來管理路由:(a)、網絡路由# route add -net 192.168.1.0/24 gw 172.16.1.106(b)、主機路由# route add -host 192.168.1.110 gw 172.16.1.10(c)、默認路由# route add default gw 172.16.1.106或# route add -net 0.0.0.0 gw 172.16.1.106(d)、刪除路由# route del -host 192.168.1.110# route del -net 192.168.1.0/243. 顯示網絡統計數據ip -s linkip -s -s link ls eth03. 中止開啓網卡ip link set eth0 downip linke set eth0 up更改網卡名字ip link set ens37 name eth1
相關文章
相關標籤/搜索