1. 程式人生 > >free看記憶體使用,ps看系統程序,netstat檢視網路,tcpdump抓包

free看記憶體使用,ps看系統程序,netstat檢視網路,tcpdump抓包

監控磁碟使用

命令:iostat 
檢視磁碟的使用情況,著重看%util這項,值越大表示磁碟越忙。

[root@shuai-01 ~]# iostat -x
Linux 3.10.0-514.el7.x86_64 (shuai-01)  2017年11月28日     _x86_64_    (1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.90    0.00    2.18    1.66    0.00   94.26

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
sdb               0.00     0.00    0.31    0.00     1.92     0.00    12.33     0.00   13.14   13.14    0.00  11.42   0.36
sda               0.00     0.06    6.46    2.69   204.21    68.73    59.63     0.14   14.94   13.02   19.56   4.59   4.20
dm-0              0.00     0.00    0.06    0.00     0.47     0.00    15.20     0.00    8.27    8.27    0.00   8.18   0.05

檢視io最高的程序iotop

安裝iotop包

[root@shuai-01 ~]# yum install -y iotop

free檢視記憶體使用情況

命令:free 
命令格式:free [選項] 
選項: 
-m 用M顯示單位 
-g 用G做單位 
-h 用合適的做單位

[root@shuai-01 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           976M        119M        546M        6.7M        310M        673M
Swap:          2.0G          0B        2.0G

計算機組成原理中:磁碟和CPU的讀取速度差太多了。中間需要一個高速儲存器來平衡一下。 
這裡寫圖片描述 
total = used + free + cache + buffer

ps命令檢視程序

檢視linux系統有哪些程序在執行,就像Windows的工作管理員一樣 
命令:ps 
靜態顯示全部程序

[root@shuai-01 ~]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.6 128104  6708 ?        Ss   08:38   0:05 /usr/lib/syst
root          2  0.0  0.0      0     0 ?        S    08:38   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    08:38   0:01 [ksoftirqd/0]
root          6  0.0  0.0      0     0 ?        S    08:38   0:01 [kworker/u256
root          7  0.0  0.0      0     0 ?        S    08:38   0:00 [migration/0]
root          8  0.0  0.0      0     0 ?        S    08:38   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        R    08:38   0:01 [rcu_sched]
root         10  0.0  0.0      0     0 ?        S    08:38   0:00 [watchdog/0]
root         12  0.0  0.0      0     0 ?        S<   08:38   0:00 [khelper]
root         13  0.0  0.0      0     0 ?        S    08:38   0:00 [kdevtmpfs]
root         14  0.0  0.0      0     0 ?        S<   08:38   0:00 [netns]
root         15  0.0  0.0      0     0 ?        S    08:38   0:00 [khungtaskd]
root         16  0.0  0.0      0     0 ?        S<   08:38   0:00 [writeback]
root         17  0.0  0.0      0     0 ?        S<   08:38   0:00 [kintegrityd]
root         18  0.0  0.0      0     0 ?        S<   08:38   0:00 [bioset]
root         19  0.0  0.0      0     0 ?        S<   08:38   0:00 [kblockd]
root         20  0.0  0.0      0     0 ?        S<   08:38   0:00 [md]
  • PID 表示程序ID,可以靠PID來殺死程序(kill -9 pid)
  • VSZ 虛擬記憶體
  • RSS 實體記憶體
  • STAT 表示程序的狀態

    • D 表示不能中斷的程序(很佔用CPU的)
    • R 正在執行中的程序
    • S 表示已經中斷的程序(sleep)
    • T 表示已近停止或者暫停的程序(Ctrl + z 停止的程序)
    • Z 表示殭屍程序
    • < 表示優先順序高的程序
    • N 表示優先順序低的程序
    • s 表示主程序
    • + 表示前臺執行的程序

    檢視網路狀態

    netstat 命令用來列印網路情況,系統開放埠,路由表等資訊。

列印當前系統開啟那些埠

[root@shuai-01 ~]# 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     1368/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     1974/master         
tcp6       0      0 :::22                   :::*                    LISTEN     1368/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN     1974/master         
udp        0      0 127.0.0.1:323           0.0.0.0:*                          541/chronyd         
udp6       0      0 ::1:323                 :::*                               541/chronyd         
raw6       0      0 :::58                   :::*                    7          588/NetworkManager  
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     12581    1/systemd           /run/lvm/lvmetad.socket
unix  2      [ ACC ]     STREAM     LISTENING     19828    1974/master         public/pickup
unix  2      [ ACC ]     STREAM     LISTENING     19834    1974/master         public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     19837    1974/master  

列印網路的連線狀況

[root@shuai-01 ~]# 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.176.135:22      192.168.176.1:50396     ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
udp        0      0 127.0.0.1:323           0.0.0.0:*                          
udp6       0      0 ::1:323                 :::*                               
raw6       0      0 :::58                   :::*                    7          
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     12581    /run/lvm/lvmetad.socket
unix  2      [ ACC ]     STREAM     LISTENING     19828    public/pickup
unix  2      [ ACC ]     STREAM     LISTENING     19834    public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     19837    public/qmgr
unix  2      [ ACC ]     STREAM     LISTENING     19859    public/flush

檢視tcp連線狀態

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

tcpdump 抓包

命令:tcpdump 
安裝

[root@shuai-01 ~]# yum install -y tcpdump

抓網絡卡的包:

[root@shuai-01 ~]# tcpdump -nn -i ens33
22:33:15.760001 IP 192.168.176.135.22 > 192.168.176.1.50396: Flags [P.], seq 280912:281188, ack 53, win 308, length 276
22:33:15.760297 IP 192.168.176.1.50396 > 192.168.176.135.22: Flags [P.], seq 53:105, ack 280912, win 158, length 52

第三列第四列:IP+port 連線ip +port

抓埠的包:

[root@shuai-01 ~]# tcpdump -nn port 80

抓那個地址的不是22埠的包:

[root@shuai-01 ~]# tcpdump -nn not port 22 and host 192.168.172.135

抓100個包儲存在那個檔案:

[root@shuai-01 ~]# tcpdump -nn -c 100 -w /tmp/1.cap

wireshark工具抓包 
安裝wireshake

yum install -y wireshake

用法:

[root@shuai-01 ~]# tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.sre" -e "http.host" -e "http.request.method" -e "http.request.uri"

實際中,抓包分析:

主要是看源ip,源port,目標ip,目標port,還有協議之類的。再深入的就得藉助圖形化的抓包工具去分析了。