ss命令取代netstat

ss命令可以從內核空間直接獲得信息,ss命令選項與netstat很是類似,容易替代.html

1      
2      
3      
4      
5      
6      
7      
8      
9      
10      
11      
12      
13      
14      
15      
16      
17      
18      
19      
20      
21      
22      
23      
24      
25      
26      
27      
28      
29      
root@10.1.1.43:~ # ss -help      
Usage: ss [ OPTIONS ]      
        ss [ OPTIONS ] [ FILTER ]      
    -h, --help       this message                           幫助      
    -V, --version    output version information             版本信息      
    -n, --numeric    don't resolve service names            不解析服務名稱      
    -r, --resolve       resolve host names                  解析主機名      
    -a, --all        display all sockets                    顯示全部套接字sockets      
    -l, --listening  display listening sockets              顯示監聽狀態的套接字sockets      
    -o, --options       show timer information              顯示計時器信息      
    -e, --extended      show detailed socket information    顯示詳細的套接字信息      
    -m, --memory        show socket memory usage            顯示套接字內存使用狀況      
    -p, --processes  show process using socket              顯示使用套接字的進程      
    -i, --info       show internal TCP information          顯示TCP內部信息      
    -s, --summary    show socket usage summary              顯示套接字使用概況      
    -4, --ipv4          display only IP version 4 sockets   顯示ipv4的套接字      
    -6, --ipv6          display only IP version 6 sockets   顯示ipv6的套接字      
    -0, --packet display PACKET sockets                     顯示PACKET套接字      
    -t, --tcp        display only TCP sockets               僅顯示TCP套接字      
    -u, --udp        display only UDP sockets               僅顯示UDP套接字      
    -d, --dccp       display only DCCP sockets              僅顯示DCCP套接字      
    -w, --raw        display only RAW sockets               僅顯示RAW套接字      
    -x, --unix       display only Unix domain sockets       僅顯示Unix套接字      
    -f, --family=FAMILY display sockets of type FAMILY      顯示 FAMILY類型的套接字(sockets),FAMILY可選,支持  unix, inet, inet6, link, netlink      
    -A, --query=QUERY, --socket=QUERY      
        QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]      
    -D, --diag=FILE     Dump raw information about TCP sockets to FILE  將原始TCP套接字信息轉儲到文件      
    -F, --filter=FILE   read filter information from FILE    從文件中都去過濾器信息      
        FILTER := [ state TCP-STATE ] [ EXPRESSION ]      

      如下是一些關於ss命令檢查網絡鏈接和套接字狀態python

1.列出全部鏈接mysql

   該輸出包括全部tcp,udp,unix套接字鏈接細節.nginx

1      
2      
3      
4      
5      
6      
7      
8      
9      
10      
11      
12      
root@10.1.1.176:~ # ss | less      
State      Recv-Q Send-Q      Local Address:Port          Peer Address:Port        
ESTAB      0      0              10.1.1.176:6379            10.1.1.176:46622        
ESTAB      0      0              10.1.1.176:46618           10.1.1.176:6379         
ESTAB      0      0              10.1.1.176:46619           10.1.1.176:6379         
ESTAB      0      0              10.1.1.176:6379            10.1.1.176:46619        
ESTAB      0      52             10.1.1.176:22000            10.1.6.56:51512        
ESTAB      0      0              10.1.1.176:6379            10.1.1.176:46618        
ESTAB      0      0              10.1.1.176:46622           10.1.1.176:6379         
ESTAB      0      0              10.1.1.176:46620           10.1.1.176:6379         
ESTAB      0      0              10.1.1.176:6379            10.1.1.176:46620        
ESTAB      0      0              10.1.1.176:22000           10.1.6.139:45300      


2.輸出過濾tcp,udp 或者unix 鏈接web

   默認'-t' 參數僅報告創建鏈接established和已鏈接connected.它不報告tcp套接字監聽listening,與'-t'一塊兒使用'-a',一次顯示全部.redis

1      
2      
3      
4      
5      
6      
7      
8      
9      
10      
11      
12      
root@10.1.1.176:~ # ss -t      
State       Recv-Q Send-Q                                             Local Address:Port                                                 Peer Address:Port        
ESTAB       0      0                                                     10.1.1.176:6379                                                   10.1.1.176:46622        
ESTAB       0      0                                                     10.1.1.176:46618                                                  10.1.1.176:6379         
ESTAB       0      0                                                     10.1.1.176:46619                                                  10.1.1.176:6379         
ESTAB       0      0                                                     10.1.1.176:6379                                                   10.1.1.176:46619        
ESTAB       0      0                                                     10.1.1.176:22000                                                   10.1.6.56:51512        
ESTAB       0      0                                                     10.1.1.176:6379                                                   10.1.1.176:46618        
ESTAB       0      0                                                     10.1.1.176:46622                                                  10.1.1.176:6379         
ESTAB       0      0                                                     10.1.1.176:46620                                                  10.1.1.176:6379         
ESTAB       0      0                                                     10.1.1.176:6379                                                   10.1.1.176:46620        
ESTAB       0      0                                                     10.1.1.176:22000                                                  10.1.6.139:45300         

 

1      
2      
3      
4      
5      
6      
7      
8      
9      
10      
11      
12      
root@10.1.1.176:~ # ss -A tcp #意義同上      
State       Recv-Q Send-Q                                             Local Address:Port                                                 Peer Address:Port        
ESTAB       0      0                                                     10.1.1.176:6379                                                   10.1.1.176:46622        
ESTAB       0      0                                                     10.1.1.176:46618                                                  10.1.1.176:6379         
ESTAB       0      0                                                     10.1.1.176:46619                                                  10.1.1.176:6379         
ESTAB       0      0                                                     10.1.1.176:6379                                                   10.1.1.176:46619        
ESTAB       0      0                                                     10.1.1.176:22000                                                   10.1.6.56:51512        
ESTAB       0      0                                                     10.1.1.176:6379                                                   10.1.1.176:46618        
ESTAB       0      0                                                     10.1.1.176:46622                                                  10.1.1.176:6379         
ESTAB       0      0                                                     10.1.1.176:46620                                                  10.1.1.176:6379         
ESTAB       0      0                                                     10.1.1.176:6379                                                   10.1.1.176:46620        
ESTAB       0      0                                                     10.1.1.176:22000                                                  10.1.6.139:45300      


3.不解析主機名sql

   爲了輸出更快,使用'n'參數能夠避免解析ip到主機名ruby

 

1      
2      
3      
4      
5      
6      
7      
8      
9      
10      
11      
12      
root@10.1.1.176:~ # ss -nt      
State       Recv-Q Send-Q                                               Local Address:Port                                                 Peer Address:Port      
ESTAB       0      0                                                       10.1.1.176:6379                                                   10.1.1.176:46622      
ESTAB       0      0                                                       10.1.1.176:46618                                                  10.1.1.176:6379       
ESTAB       0      0                                                       10.1.1.176:46619                                                  10.1.1.176:6379       
ESTAB       0      0                                                       10.1.1.176:6379                                                   10.1.1.176:46619      
ESTAB       0      0                                                       10.1.1.176:22000                                                   10.1.6.56:51512      
ESTAB       0      0                                                       10.1.1.176:6379                                                   10.1.1.176:46618      
ESTAB       0      0                                                       10.1.1.176:46622                                                  10.1.1.176:6379       
ESTAB       0      0                                                       10.1.1.176:46620                                                  10.1.1.176:6379       
ESTAB       0      0                                                       10.1.1.176:6379                                                   10.1.1.176:46620      
ESTAB       0      0                                                       10.1.1.176:22000                                                  10.1.6.139:45300      


4.只顯示監聽套接字服務器

 

1      
2      
3      
4      
5      
6      
7      
8      
9      
root@10.1.1.176:~ # ss -ltn      
State       Recv-Q Send-Q                                               Local Address:Port                                                 Peer Address:Port      
LISTEN      0      128                                                     10.1.1.176:8080                                                            *:*          
LISTEN      0      128                                                              *:80                                                              *:*          
LISTEN      0      128                                                              *:22000                                                           *:*          
LISTEN      0      10                                                               *:8000                                                            *:*          
LISTEN      0      50                                                      10.1.1.176:3306                                                            *:*          
LISTEN      0      50                                                      10.1.1.176:3307                                                            *:*          
LISTEN      0      128                                                     10.1.1.176:6379                                                            *:*      


5.輸出進程名和pid網絡

 

1      
2      
3      
4      
5      
6      
7      
8      
9      
root@10.1.1.176:~ # ss -ltp      
State       Recv-Q Send-Q                                             Local Address:Port                                                 Peer Address:Port        
LISTEN      0      128                                                   10.1.1.176:http-alt                                                         *:*        users :(( "ruby" ,13744,13),( "ruby" ,13704,13),( "ruby" ,13676,13))      
LISTEN      0      128                                                            *:http                                                            *:*        users :(( "nginx" ,11650,8),( "nginx" ,11649,8),( "nginx" ,11648,8),( "nginx" ,11647,8),( "nginx" ,11646,8))      
LISTEN      0      128                                                            *:22000                                                           *:*        users :(( "sshd" ,1885,3))      
LISTEN      0      10                                                             *:8000                                                            *:*        users :(( "python" ,16496,3))      
LISTEN      0      50                                                    10.1.1.176:mysql                                                           *:*        users :(( "mysqld" ,13394,10))      
LISTEN      0      50                                                    10.1.1.176:3307                                                            *:*        users :(( "mysqld" ,17257,10))      
LISTEN      0      128                                                   10.1.1.176:6379                                                            *:*        users :(( "redis-server" ,12198,4))      


6.輸出彙總統計

 

1      
2      
3      
4      
5      
6      
7      
8      
9      
10      
11      
root@10.1.1.176:~ # ss -s      
Total: 66 (kernel 67)      
TCP:   17 (estab 10, closed 0, orphaned 0, synrecv 0, timewait 0 /0 ), ports 11      
       
Transport Total     IP        IPv6      
*     67        -         -             
RAW   0         0         0             
UDP   0         0         0             
TCP   17        17        0             
INET      17        17        0             
FRAG      0         0         0      


7.顯示時間信息

  使用'-o'選項.每一個鏈接時間信息將被顯示.

 

1      
2      
3      
4      
5      
6      
7      
8      
9      
10      
11      
12      
root@10.1.1.176:~ # ss -tn -o      
State       Recv-Q Send-Q                                               Local Address:Port                                                 Peer Address:Port      
ESTAB       0      0                                                       10.1.1.176:6379                                                   10.1.1.176:46622      
ESTAB       0      0                                                       10.1.1.176:46618                                                  10.1.1.176:6379       
ESTAB       0      0                                                       10.1.1.176:46619                                                  10.1.1.176:6379       
ESTAB       0      0                                                       10.1.1.176:6379                                                   10.1.1.176:46619      
ESTAB       0      300                                                     10.1.1.176:22000                                                   10.1.6.56:51512  timer:(on,372ms,0)      
ESTAB       0      0                                                       10.1.1.176:6379                                                   10.1.1.176:46618      
ESTAB       0      0                                                       10.1.1.176:46622                                                  10.1.1.176:6379       
ESTAB       0      0                                                       10.1.1.176:46620                                                  10.1.1.176:6379       
ESTAB       0      0                                                       10.1.1.176:6379                                                   10.1.1.176:46620      
ESTAB       0      0                                                       10.1.1.176:22000                                                  10.1.6.139:45300      


8. 顯示ipv4 或 ipv6 套接字鏈接

  只顯示ipv4套接字鏈接使用'-f inet' 或者'-4'

 

1      
2      
3      
4      
5      
6      
7      
8      
9      
root@10.1.1.176:~ # ss -tl -f inet      
State       Recv-Q Send-Q                                             Local Address:Port                                                 Peer Address:Port        
LISTEN      0      128                                                   10.1.1.176:http-alt                                                         *:*            
LISTEN      0      128                                                            *:http                                                            *:*            
LISTEN      0      128                                                            *:22000                                                           *:*            
LISTEN      0      10                                                             *:8000                                                            *:*            
LISTEN      0      50                                                    10.1.1.176:mysql                                                           *:*            
LISTEN      0      50                                                    10.1.1.176:3307                                                            *:*            
LISTEN      0      128                                                   10.1.1.176:6379                                                            *:*      

     只顯示ipv4套接字鏈接使用'-f inet6 ' 或者'-6'

 

1      
2      
root@10.1.1.176:~ # ss -tl6      
State       Recv-Q Send-Q                                             Local Address:Port                                                 Peer Address:Port      


9.過濾tcp鏈接狀態

   ss命令支持過濾只顯示特殊的鏈接.   

$ ss [ OPTIONS ] [ STATE-FILTER ] [ ADDRESS-FILTER ]

  顯示ipv4 tcp 套接字 鏈接狀態

 

1      
2      
3      
4      
5      
6      
7      
8      
9      
10      
11      
12      
root@10.1.1.176:~ # ss -t4 state established      
Recv-Q Send-Q                                                  Local Address:Port                                                      Peer Address:Port        
0      0                                                          10.1.1.176:6379                                                        10.1.1.176:46622        
0      0                                                          10.1.1.176:46618                                                       10.1.1.176:6379         
0      0                                                          10.1.1.176:46619                                                       10.1.1.176:6379         
0      0                                                          10.1.1.176:6379                                                        10.1.1.176:46619        
0      0                                                          10.1.1.176:22000                                                        10.1.6.56:51512        
0      0                                                          10.1.1.176:6379                                                        10.1.1.176:46618        
0      0                                                          10.1.1.176:46622                                                       10.1.1.176:6379         
0      0                                                          10.1.1.176:46620                                                       10.1.1.176:6379         
0      0                                                          10.1.1.176:6379                                                        10.1.1.176:46620        
0      0                                                          10.1.1.176:22000                                                       10.1.6.139:45300      

    顯示套接字狀態爲 time-wait

 

1      
2      
root@10.1.1.176:~ # ss -t4 state time-wait      
Recv-Q Send-Q                                                  Local Address:Port                                                      Peer Address:Port      

   能夠是如下狀態

1. established
2. syn-sent
3. syn-recv
4. fin-wait-1
5. fin-wait-2
6. time-wait
7. closed
8. close-wait
9. last-ack
10. closing
11. all - All of the above states
12. connected - All the states except for listen and closed
13. synchronized - All the connected states except for syn-sent
14. bucket - Show states, which are maintained as minisockets, i.e. time-wait and syn-recv.
15. big - Opposite to bucket state.

    像不少狀態如syn-sent,syn-recv 大部分時間不顯示任何套接字,停留在套接字的時間很短,咱們能夠使用watch命令在一段時間的檢測這些套接字的狀態.

 

1      
root@10.1.1.176:~ # watch -n 1 "ss -t4 state syn-sent"      


10.過濾鏈接地址和端口號

    除了tcp套接字,ss 命令也支持過濾基於地址和端口好的套接字.

 

1      
2      
3      
4      
5      
6      
7      
8      
root@10.1.1.176:sites-enabled # ss -nt sport = :80 or sport = :22000      
State       Recv-Q Send-Q                                               Local Address:Port                                                 Peer Address:Port      
ESTAB       0      0                                                       10.1.1.176:80                                                      10.1.6.56:52634      
ESTAB       0      0                                                       10.1.1.176:80                                                      10.1.6.56:52636      
ESTAB       0      0                                                       10.1.1.176:22000                                                   10.1.6.56:51512      
ESTAB       0      0                                                       10.1.1.176:80                                                      10.1.6.56:52635      
ESTAB       0      0                                                       10.1.1.176:80                                                      10.1.6.56:52637      
ESTAB       0      0                                                       10.1.1.176:22000                                                  10.1.6.139:45300      

 

1      
2      
3      
4      
5      
6      
7      
8      
9      
10      
11      
12      
root@10.1.1.176:sites-enabled # ss -nt dport gt :1024      
State       Recv-Q Send-Q                                               Local Address:Port                                                 Peer Address:Port      
ESTAB       0      0                                                       10.1.1.176:6379                                                   10.1.1.176:46622      
ESTAB       0      0                                                       10.1.1.176:46618                                                  10.1.1.176:6379       
ESTAB       0      0                                                       10.1.1.176:46619                                                  10.1.1.176:6379       
ESTAB       0      0                                                       10.1.1.176:6379                                                   10.1.1.176:46619      
ESTAB       0      264                                                     10.1.1.176:22000                                                   10.1.6.56:51512      
ESTAB       0      0                                                       10.1.1.176:6379                                                   10.1.1.176:46618      
ESTAB       0      0                                                       10.1.1.176:46622                                                  10.1.1.176:6379       
ESTAB       0      0                                                       10.1.1.176:46620                                                  10.1.1.176:6379       
ESTAB       0      0                                                       10.1.1.176:6379                                                   10.1.1.176:46620      
ESTAB       0      0                                                       10.1.1.176:22000                                                  10.1.6.139:45300      

下面是支持的語法

 

1      
2      
3      
4      
5      
6      
<= or le : Less than or equal to port      
>= or ge : Greater than or equal to port      
== or eq : Equal to port      
!= or ne : Not equal to port      
< or gt : Less than to port      
> or lt : Greater than to port      


附錄:
TCP正常鏈接創建和終止所對應的狀態





狀態變遷:

1.所有11種狀態:
   1.1 客戶端獨有的:(1)SYN_SENT (2)FIN_WAIT1 (3)FIN_WAIT2 (4)CLOSING (5)TIME_WAIT
   1.2 服務器獨有的:(1)LISTEN (2)SYN_RCVD (3)CLOSE_WAIT (4)LAST_ACK
   1.3 共有的: (1)CLOSED (2) ESTABLISHED

2.創建鏈接時的狀態變遷:
    開始,創建鏈接以前服務器和客戶端的狀態都爲CLOSED.服務器建立socket後開始監聽,變爲LISTEN狀態.客戶端請求創建鏈接,向服務器發送SYN報文,客戶端的狀態變爲SYN_SENT.服務器收到客戶端的報文後向客戶端發送ACK和SYN報文,此時服務器的狀態變爲SYN_RCVD.而後,客戶端收到ACK,SYN,就向服務器發送ACK,客戶端狀態變爲ESTABLISHED,服務器收到客戶端的ACK後也變爲ESTABLISHED.此時,3次握手完成,鏈接創建!

3.斷開鏈接時的狀態變遷
    因爲tcp鏈接是全雙工的.客戶端先向服務器發送FIN報文,請求斷開鏈接,其狀態變爲FIN_WAIT1.服務器收到FIN後向客戶端發生ACK,服務器狀態變爲CLOSE_WAIT.客戶端收到ACK後就進入FIN_WAIT2狀態.此時鏈接已經斷開了一半.若是服務器還有數據要發送給客戶端,就會繼續發送.直到發完了,就發送FIN報文,此時服務器進入LAST_ACK狀態.客戶端收到服務器的FIN後,立刻發送ACK給服務器,此時客戶端進入TIME_WAIT狀態,在過了2MSL長的時間後進入CLOSED狀態.服務器收到客戶端的ACK就進入CLOSED狀態.
    CLOSING狀態.CLOSEING狀態表示客戶端發生了FIN,但沒有收到服務器的ACK,卻收到了服務器的FIN.這種狀況發送在服務器發送的ACK丟包的時候,由於網絡傳輸有時會有意外.


CLOSED:表示初始狀態

LISTEN:表示服務器端的某個socket處於監聽狀態,能夠接受鏈接.

SYN_SENT:在服務端監聽後,客戶端socket執行connect鏈接時,客戶端發生SYN報文,此時客戶端就進入SYN_SENT狀態,等待服務端的確認.

SYN_RCVD:表示服務端接受到了SYN報文,在正常狀況下,這個狀態是服務器端的socket在創建TCP鏈接時的三次握手會話過程當中的一箇中間狀態,很短暫.除非故意將三次TCP握手過程當中最後一個ACK報文不發送.所以這種狀態時,當收到客戶端的ACK報文後,它會進入到ESTABLISED狀態.

ESTABLISHED:表示鏈接已經創建了.

FIN_WAIT_1:這個是已經創建鏈接以後,其中一方請求終止鏈接,等待對方的FIN報文.FIN_WAIT_1狀態時當socket在ESTABLISHED狀態時,它想主動關閉鏈接,向對方發送了FIN報文,此時該socket即進入到FIN_WAIT_1狀態.而當對方迴應ACK報文後,則進入到FIN_WAIT_2狀態,固然在實際的正常狀況下,不管對方何種狀況下,都應該立刻迴應ACK報文,因此FIN_WAIT_1狀態通常比較難見,而FIN_WAIT_2狀態還有能夠看到.

FIN_WAIT_2:實際上FIN_WAIT_2狀態下的socket,表示半鏈接,也即有一方要求close鏈接,但另外還告訴對方,我暫時還有點數據須要傳送給你,稍後再關閉鏈接.

TIME_WAIT:表示收到了對方的FIN報文,併發送出了ACK報文,就等2MSL後便可回到closed可用狀態.若是FIN_WAIT_1狀態下,收到了對方同時帶FIN標誌和ACK標誌的報文時,能夠直接進入到TIME_WAIT狀態,而無須通過FIN_WAIT_2狀態.

CLOSE_WAIT:表示在等待關閉

LAST_ACK:它是被動關閉一方在發送FIN報文後,最後等待對方的ACK報文.當收到ACK報文後,也便可以進入到CLOSED可用狀態了.




更多語法細節能夠安裝iproute2-doc

查看文件 /usr/share/doc/iproute2-doc/ss.html

相關文章
相關標籤/搜索