linux經常使用的命令和工具

 

 screen     管理會話工具java

與之類似的工具還有tmuxpython

# screen     // 進入一個回話 。還能夠給會話取名  screen -S modify_screen 
#vim screen.txt 
hello world
 // 編輯一半ctrl+a+d 在後臺運行不會中斷。暫時退出 
 [detached from 28063.pts-0.tencent]
# screen -ls      //查看screen
There is a screen on:
    26046.pts-0.tencent    (Detached)
1 Socket in /var/run/screen/S-root. 

# screen -r 26046   //從新鏈接會話 
hello world
# exit  //結束會話

若是因爲某種緣由其中一個會話死掉了,這時screen -ls會顯示該會話爲dead狀態。
#  screen -wipe   //清除該會話

  screen讓進程後臺運行的正常狀態應該爲(Detached)。
  screen -ls,若是狀態爲Attached,此時用screen -r <session-id>也登不上
  解決方法:screen -D -r <session-id>  //先踢掉前一用戶,再登錄。ios

 
screen直接把命令放入後臺執行
screen -dmS 'jenkins' java -jar /usr/lib/jenkins/jenkins.war

# screen -ls 查看
There is a screen on:
12220.jenkins (Detached)
1 Socket in /var/run/screen/S-root.nginx

 

 

supervisord    進程守護工具

supervisord將配置進程做爲本身的子進程啓動。而且能夠配置爲當進程意外中止或者服務器重啓後,啓動進程。vim

安裝服務器

# pip install supervisor 

生成配置文件網絡

# echo_supervisord_conf>/etc/supervisord.conf 

修改配置文件 session

# vim /etc/supervisord.conf
[include]
files = /etc/supervisord.d/*.conf

# vim /etc/supervisord.d/logstash.conf
[program:logstash]
command=/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/nginx.conf 
autostart=true     ;是否隨supervisord啓動
autorestart=true   ;是否在服務掛了以後重啓
startretries=3                           ;啓動嘗試次數
stderr_logfile=/tmp/supervisord.err.log  ;標準輸出的位置
stdout_logfile=/tmp/supervisord.out.log  ;標準錯誤輸出的位置
loglevel=info                            ;日誌的級別

查看全部子進程狀態:curl

# supervisorctl statustcp

打開|關閉子進程:

# supervisorctl start|stop logstash

打開|關閉全部進程:
# supervisorctl start|stop all

 

tcpdump  抓包工具

tcpdump 能夠將網絡中傳送的數據包徹底截獲下來提供分析。

-vv   輸出詳細的報文信息。
-n   不把網絡地址轉換成名字
-nn  不進行端口名稱的轉換
-i   指定監聽的網絡接口
-c   指定抓多少個包 (有時候流量大的時候,如若不指定,靠人工CTRL+C仍是抓的太多,影響服務器)
-w   將分析寫入文件,而不是直接打印
-r    從文件中讀取包(-w產生的文件)
-s  0 抓取完整的數據包,而不是默認的68個字節。

默認啓動

tcpdump -vv   # 直接啓動tcpdump將監視第一個網絡接口上全部流過的數據包

過濾主機

tcpdump -i eth1 host 192.186.1.1 #全部通過eth1 目標或源地址是192這個ip的網絡數據包
tcpdump -i eth1 src host 192.168.1.1 #指定源地址
tcpdump -i eth1 dst host 192.168.1.1 #指定目標地址

過濾端口

tcpdump -i eth0 port 80    #抓取全部通過eth0,目標或者源端口是80的
tcpdump -i eth0 src/dst port 80  #指定目標/源地址

網絡過濾

tcpdump -i eth0 [src/dst] net 192.168.1.0/24

協議過濾

tcpdump -i eth0 tcp/udp/icmp/arp/ip


經常使用表達式
非 : ! not
與 : && and
或 : || or

例子:
抓取全部通過eth1,目的地址是192.168.1.254或192.168.1.200端口是80的TCP數據包

# tcpdump -i eth1 '((tcp) and (port 80) and ((dst host 192.168.1.254) or (dst host 192.168.1.200)))'

 

# tcpdump tcp -i eth1 -t -s 0 -c 100 and dst port 8080 and src net 192.168.1.0/24 -w target.cap

tcp: ip/icmp/tcp/udp/icmp這些協議等都要放到第一個參數的位置,用來過濾數據報的類型

-i eth1 :  只抓通過接口eth1的包
-t :  不顯示時間戳
-s 0 :  抓取數據包時默認抓取長度爲68字節。加上-s 0 後能夠抓到完整的數據包
-c 100 :  只抓取100個數據包
dst port 8080 :  抓取目標端口是8080的數據包
src net 192.168.1.0/24 : 數據包的源網絡地址爲192.168.1.0/24
-w target.cap :  保存成cap文件,方便用wireshark分析

 

經常使用方式:

 

查看數據內容,建議用tcpdump -s 0 -w filename把數據包都保存下來,而後用wireshark的Follow TCP Stream/Follow UDP Stream來查看整個會話的內容。-s 0是抓取完整數據包,不然默認只抓68字節。

 

 

 

 抓取到的包能夠經過-r來讀取

# tcpdump -i any -s 0 -n -vvv tcp and host www.baidu.com -c 100 -w baidu.cap  #抓包
而後發送一個curl請求測試
# tcpdump -r test.cap   #讀取
reading from file test.cap, link-type LINUX_SLL (Linux cooked)
19:02:21.002689 IP abao.40194 > 14.215.177.39.http: Flags [S], seq 1091719838, win 29200, options [mss 1460,nop,nop,TS val 2274559162 ecr 0,nop,wscale 7], length 0
19:02:21.042510 IP 14.215.177.39.http > abao.40194: Flags [S.], seq 1769338520, ack 1091719839, win 8192, options [mss 1424,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,wscale 5], length 0
19:02:21.042566 IP abao.40194 > 14.215.177.39.http: Flags [.], ack 1, win 229, length 0
19:02:21.042723 IP abao.40194 > 14.215.177.39.http: Flags [P.], seq 1:78, ack 1, win 229, length 77: HTTP: GET / HTTP/1.1
19:02:21.082772 IP 14.215.177.39.http > abao.40194: Flags [.], ack 78, win 776, length 0
19:02:21.084201 IP 14.215.177.39.http > abao.40194: Flags [P.], seq 1:1449, ack 78, win 776, length 1448: HTTP: HTTP/1.1 200 OK
19:02:21.084225 IP abao.40194 > 14.215.177.39.http: Flags [.], ack 1449, win 251, length 0
19:02:21.084204 IP 14.215.177.39.http > abao.40194: Flags [P.], seq 1449:2782, ack 78, win 776, length 1333: HTTP
19:02:21.084240 IP abao.40194 > 14.215.177.39.http: Flags [.], ack 2782, win 273, length 0
19:02:21.084414 IP abao.40194 > 14.215.177.39.http: Flags [F.], seq 78, ack 2782, win 273, length 0
19:02:21.124333 IP 14.215.177.39.http > abao.40194: Flags [.], ack 79, win 776, length 0
19:02:21.124408 IP 14.215.177.39.http > abao.40194: Flags [F.], seq 2782, ack 79, win 776, length 0
19:02:21.124449 IP abao.40194 > 14.215.177.39.http: Flags [.], ack 2783, win 273, length 0

 

Tcpdump + Wireshark組合實現:在Linux 裏用tcpdump抓包,而後用wireshake分析包。

 

 

防火牆規則端口轉發

我從外部8888端口訪問nginx的80端口:  
# iptables -t nat -A PREROUTING -p tcp --dport 8888 -j REDIRECT --to-port 80

 


tcp鏈接狀態    netstat   ss 
# netstat -n |awk '/^tcp/{++state[$NF]} END{for(key in state) print key,state[key]}'
# ss -ant |awk 'NR>1{++s[$1]} END {for(k in s) print k,s[k]}'
SYN-RECV 1
LISTEN 11
ESTAB 3
CLOSE-WAIT 128
FIN-WAIT-1 1
TIME-WAIT 29

# curl myip.ipip.net  查看出口ip

性能測試工具   nmon nmon有個分析工具
網絡分析工具   ping     nslookup   traceroute     mtr
網絡流量      nethogs   nload   iftop
磁盤io     iotop     iostat  dstat

vmstat   能夠用來監控CUP、虛擬內存、IO等多個服務器指標。

cup密集型服務器vmstatus輸出一般是一個很高的值,即cup花費在非內核代碼上的cup時間佔比應該很高。

IO密集型服務器cup會花費大量時間等待IO請求完成,則意味着不少任務處於非中斷休眠狀態(b列),而且wa數字也很高(等待IO時間)。

 

traceroute  

tracert [-d] [-h maximum_hops] [-j computer-list] [-w timeout] target_name

-d    指定不對計算機名解析地址。
-h maximum_hops    指定查找目標的跳轉的最大數目。
-j computer-list    指定在 computer-list 中鬆散源路由。
-w timeout       等待由 timeout 對每一個應答指定的毫秒數。
target_name      目標計算機的名稱。

 

iftop  查看網絡流量

-B 以bytes爲單位顯示流量(默認是bits),  # iftop -B
-n使host信息默認直接都顯示IP,   # iftop -n

按n 顯示本機ip
按p   顯示兩端的端口信息
按T 顯示每一個鏈接的總流量

 

TX:發送流量     cum: 運行iftop到目前時間的總流量    peak: 流量峯值                                 rates:分別表示過去 2s 10s 40s 的平均流量
RX:接收流量
TOTAL:總流量

  

ioftop   查看磁盤io

r:改變排序順序。
o:只顯示有IO輸出的進程。
p:進程/線程的顯示方式的切換。 a:顯示累積使用量。 q:退出。

 

mtr   網絡診斷工具

使用Internet控制消息協議(ICMP)數據包來測試Internet上兩點之間的鏈接和傳輸。   

-r      --report以報告的方式顯示,不動態顯示
-c     發送ping數據包的個數,默認10個,等同ping -c
-n     --no-dns不對IP地址作域名解析

# mtr -r www.elastic.co -n -c 20 
Start: Sun Sep 29 18:29:03 2019
HOST: test                        Loss%   Snt   Last   Avg  Best  Wrst StDev 
  1.|-- 100.99.93.130              0.0%    20    4.2   9.8   1.4  20.6   5.4
  2.|-- 100.99.119.234             0.0%    20    0.8   0.9   0.8   1.7   0.0
  3.|-- 10.200.159.205            20.0%    20    1.0   0.8   0.6   1.4   0.0
  4.|-- 10.196.0.34                0.0%    20    2.7   4.0   2.4  30.2   6.1
  5.|-- 10.196.0.78                0.0%    20    2.1   2.1   2.0   2.5   0.0
  6.|-- 118.112.16.109            55.0%    20    4.2   9.7   3.9  52.5  16.0
  7.|-- 182.140.229.205           90.0%    20    3.3   3.3   3.3   3.3   0.0
  8.|-- 182.140.220.9              0.0%    20   10.1   6.4   2.7  10.1   2.3
  9.|-- 202.97.65.197              0.0%    20   29.8  28.8  27.1  30.4   0.9
 10.|-- 202.97.94.126             75.0%    20   28.6  28.6  28.5  28.7   0.0
 11.|-- 202.97.12.33              25.0%    20   38.7  41.7  38.7  45.0   2.0
 12.|-- 202.97.60.214              5.0%    20  153.5 151.8 140.3 164.7   7.0
 13.|-- 129.250.2.155             30.0%    20  154.2 161.3 154.2 172.4   4.4
 14.|-- 129.250.6.133             30.0%    20  163.6 163.5 162.9 163.7   0.0
 15.|-- 117.103.177.78             0.0%    20  101.5 102.7  97.2 105.6   2.7
 16.|-- 151.101.110.217           30.0%    20  159.6 163.4 159.6 168.3   1.7
結果說明:
Loss% 表示每跳的丟包百分比 Snt 表示發送10個數據包 Last 最後發送的數據包的延遲,單位ms Avg 全部數據包的平均延時 Best 最短的延時 Wrst 最長的延時 StDev 延遲標準誤差。若是很高,說明測量值不一致,就要查看最佳和最差延遲測量,以確保平均值是實際延遲的表示,而不是太大的波動

 在大多數狀況下,根據配置,前2或3跳一般表明源主機的ISP,而最後2或3跳錶明目標主機的ISP。中間的跳數是數據包遍歷到達目的地的路由器。

mtr報告分析:  https://cloud.tencent.com/developer/article/1332118

 

httping 測量網站延遲

HTTP請求的鏈接、發送請求、等待迴應的時間。相似於ping這個網絡工具,不過ping是發送icmp請求,httping是發送http請求。
經常使用參數:
-c ping幾回
-l --use-ssl發送https請求
-Y 返回結果帶顏色

相關文章
相關標籤/搜索