2018-1-23 7周2次課 iostat,free,ps,netstat,tcpdump
iostat命令和sar屬於同一個包,安裝sysstat就可以
·查看磁盤使用情況iostat:(使用方法與vmstat一致)
·每隔一秒查看一次磁盤使用情況
·每隔一秒查看磁盤情況一次,共查看兩次
·iostat -x
(%util表示一秒內IO操作所占的比例)
如果數字很大,那麽磁盤讀寫也會很大;但是如果讀寫不大,而%util很大,那麽磁盤可能存在故障
·iotop命令:
安裝iotop命令:[root@localhost ~]#yum install -y iotop
iotop和top命令類似,都是動態顯示
10.7 free命令
·查看內存使用情況
[root@localhost ~]# free total used free shared buff/cache available Mem: 999696 120152 621148 6820 258396 696728 Swap: 2097148 0 2097148
·指定單位為M
[root@localhost ~]# free -m total used free shared buff/cache available Mem: 976 117 605 6 253 680 Swap: 2047 0 2047
·可視化,方便查看
[root@localhost ~]# free -h total used free shared buff/cache available Mem: 976M 117M 605M 6.7M 253M 680M Swap: 2.0G 0B 2.0G
cache緩存:磁盤——>內存(cache)——>CPU
buffer緩沖:CPU——>內存(buffer)——>磁盤
公式:
total = free + used + buff/cache
available = free + buff/cache的剩余部分
關註點:available
10.8 ps命令
·查看系統進程
[root@localhost ~]# ps aux
·查看某個進程是否在運行:ps aux |grep 關鍵字
[root@localhost ~]# ps aux |grep mysql root 1812 0.0 0.0 112676 980 pts/0 S+ 16:48 0:00 grep --color=auto mysql
·查看系統所有進程,除了ps aux還可以使用ps -elf
[root@localhost ~]# ps -elf
PID的用處:1,kill進程用 kill + 進程號
2,查看進程所在目錄 ll /proc/數字
STAT部分說明:
D 不能中斷的進程
R run狀態的進程(某個時間段內在使用)
S sleep狀態進程 (vmstat運行時間很短,運行一下就sleep,並不會消耗CPU資源)
T 暫停的進程
+ 前臺進程
Z 僵屍進程(很少會有,但也會有)
< 高優先級進程
N 低優先級進程(不急)
L 內存中被鎖了內存分頁(不常見)
s 主進程
l 多線程進程(一個進程中有多個線程)
10.9 查看網絡狀態
·查看網絡狀態:netstat
(不詳細列舉)
·查看監聽端口
netstat -lnp (l=listen)
·查看所有連接狀態:netstat -an
(不詳細列舉)
·只看tcp的,不包含socket:netstat -lntp
[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 903/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1006/master tcp6 0 0 :::22 :::* LISTEN 903/sshd tcp6 0 0 ::1:25 :::* LISTEN 1006/master
·只看tcp和udp的:netstat -luntp
[root@localhost netstat -luntp 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 903/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1006/master tcp6 0 0 :::22 :::* LISTEN 903/sshd tcp6 0 0 ::1:25 :::* LISTEN 1006/master udp 0 0 127.0.0.1:323 0.0.0.0:* 547/chronyd udp6 0 0 ::1:323 :::* 547/chronyd
·ss -an 和 netstat異曲同工(缺點是不顯示進程的名字)
[root@localhost ~]# ss -an |grep -i listen u_str LISTEN 0 100 private/tlsmgr 19537 * 0 u_str LISTEN 0 100 private/rewrite 19540 * 0 u_str LISTEN 0 100 private/bounce 19543 * 0 u_str LISTEN 0 100 private/defer 19546 * 0 u_str LISTEN 0 100 private/trace 19549 * 0 u_str LISTEN 0 100 private/verify 19552 * 0 u_str LISTEN 0 128 /run/systemd/private 12570 * 0 u_str LISTEN 0 32 /var/run/vmware/guestServicePipe 16424 * 0 u_str LISTEN 0 100 public/qmgr 19533 * 0 u_str LISTEN 0 100 public/flush 19555 * 0 u_str LISTEN 0 100 public/showq 19570 * 0 u_str LISTEN 0 100 private/proxymap 19558 * 0 u_str LISTEN 0 100 private/proxywrite 19561 * 0 u_str LISTEN 0 100 private/smtp 19564 * 0 u_str LISTEN 0 100 private/relay 19567 * 0 u_str LISTEN 0 100 private/error 19573 * 0 u_str LISTEN 0 100 private/retry 19576 * 0 u_str LISTEN 0 100 private/discard 19579 * 0 u_str LISTEN 0 100 private/local 19582 * 0 u_str LISTEN 0 100 private/virtual 19585 * 0 u_str LISTEN 0 100 private/lmtp 19588 * 0 u_str LISTEN 0 100 private/anvil 19591 * 0 u_str LISTEN 0 100 private/scache 19594 * 0 u_seq LISTEN 0 128 /run/udev/control 12662 * 0 u_str LISTEN 0 100 public/pickup 19526 * 0 u_str LISTEN 0 100 public/cleanup 19530 * 0 u_str LISTEN 0 128 /run/lvm/lvmetad.socket 12664 * 0 u_str LISTEN 0 128 /run/lvm/lvmpolld.socket 12762 * 0 u_str LISTEN 0 128 /var/run/dbus/system_bus_socket 15327 * 0 u_str LISTEN 0 128 /run/systemd/journal/stdout 8432 * 0 tcp LISTEN 0 128 *:22 *:* tcp LISTEN 0 100 127.0.0.1:25 *:* tcp LISTEN 0 128 :::22 :::* tcp LISTEN 0 100 ::1:25 :::*
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 4 ESTABLISHED 1
(關註ESTABLISHED,數字越大,越忙)
10.10 Linux下抓包
抓包工具:tcpdump
安裝tcpdump:yum install -y tcpdump
·使用方法:tcpdump -nn
[root@localhost ~]# tcpdump -nn tcpdump: packet printing is not supported for link type NFLOG: use -w
(因為網卡名比較特殊,默認時eth0,所以無法使用,需要指定)
·指定網卡抓包:tcpdump -nn -i 網卡名
(內容過多,中間省略)
-nn的第一個n表示IP用數字顯示出來,如果不加n,則會顯示主機名
-nn用數字顯示,比較直觀
(關註點:數據流向,length)
·指定端口:tcpdump -nn port 端口號
[root@localhost ~]# tcpdump -nn -i ens33 port 22
·指定不要的端口:tcpdump -nn not port 端口號
[root@localhost ~]# tcpdump -nn -i ens33 not port 22
·同時指定不要的端口和指定IP的包:tcpdump -nn not port 端口號 and host 指定IP地址
[root@localhost ~]# tcpdump -nn -i ens33 not port 22 and host 192.168.65.1
·指定數據包個數:tcpdump -nn -c 個數
[root@localhost ~]# tcpdump -nn -i ens33 -c 20 ##抓20個包
·將指定個數抓的包存到指定文件中:tcpdump -nn -c 個數 -w 絕對路徑
[root@localhost ~]# tcpdump -nn -i ens33 -c 100 -w /tmp/1.cap tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes 100 packets captured 100 packets received by filter 0 packets dropped by kernel
(1.cap是無法cat的)
·查看.cap文件:tcpdump -r 目標文件
[root@localhost ~]# tcpdump -r /tmp/1.cap
·tshark命令
安裝:yum install -y wireshark
查看指定網卡80端口web訪問情況:
[root@localhost ~]# 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"
(類似於訪問日誌,可以查看什麽IP訪問什麽鏈接)
2018-1-23 7周2次課 iostat,free,ps,netstat,tcpdump