linux命令:ss,linux命令ss

來源:互聯網
上載者:User

linux命令:ss,linux命令ss

ss是Socket Statistics的縮寫。顧名思義,ss命令可以用來擷取socket統計資訊,它可以顯示和netstat類似的內容。但ss的優勢在於它能夠顯示更多更詳細的有關TCP和串連狀態的資訊,而且比netstat更快速更高效。

當伺服器的socket串連數量變得非常大時,無論是使用netstat命令還是直接cat /proc/net/tcp,執行速度都會很慢。可能你不會有切身的感受,但請相信我,當伺服器維持的串連達到上萬個的時候,使用netstat等於浪費 生命,而用ss才是節省時間。

天下武功唯快不破。ss快的秘訣在於,它利用到了TCP協議棧中tcp_diag。tcp_diag是一個用於分析統計的模組,可以獲得Linux 核心中第一手的資訊,這就確保了ss的快捷高效。當然,如果你的系統中沒有tcp_diag,ss也可以正常運行,只是效率會變得稍慢。(但仍然比 netstat要快。)

1.命令格式:

ss [參數]

ss [參數] [過濾]

2.命令功能:

ss(Socket Statistics的縮寫)命令可以用來擷取 socket統計資訊,此命令輸出的結果類似於 netstat輸出的內容,但它能顯示更多更詳細的 TCP串連狀態的資訊,且比 netstat 更快速高效。它使用了 TCP協議棧中 tcp_diag(是一個用於分析統計的模組),能直接從獲得第一手核心資訊,這就使得 ss命令快捷高效。在沒有 tcp_diag,ss也可以正常運行。

3.命令參數:

-h, --help協助資訊

-V, --version程式版本資訊

-n, --numeric不解析服務名稱

-r, --resolve 解析主機名稱

-a, --all顯示所有通訊端(sockets)

-l, --listening顯示監聽狀態的通訊端(sockets)

-o, --options 顯示計時器資訊

-e, --extended 顯示詳細的通訊端(sockets)資訊

-m, --memory 顯示通訊端(socket)的記憶體使用量情況

-p, --processes顯示使用通訊端(socket)的進程

-i, --info顯示 TCP內部資訊

-s, --summary顯示通訊端(socket)使用概況

-4, --ipv4 僅顯示IPv4的通訊端(sockets)

-6, --ipv6 僅顯示IPv6的通訊端(sockets)

-0, --packet 顯示 PACKET 通訊端(socket)

-t, --tcp僅顯示 TCP通訊端(sockets)

-u, --udp僅顯示 UCP通訊端(sockets)

-d, --dccp僅顯示 DCCP通訊端(sockets)

-w, --raw僅顯示 RAW通訊端(sockets)

-x, --unix僅顯示 Unix通訊端(sockets)

-f, --family=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 將原始TCP通訊端(sockets)資訊轉儲到檔案

-F, --filter=FILE 從檔案中都去過濾器資訊

FILTER := [ state TCP-STATE ] [ EXPRESSION ]

4.使用執行個體:

執行個體1:顯示TCP串連

命令:

ss -t -a

輸出:

[root@localhost ~]# ss -t -aState      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   LISTEN     0      0                                         127.0.0.1:smux                                               *:*       LISTEN     0      0                                                 *:3690                                               *:*       LISTEN     0      0                                                 *:ssh                                                *:*       ESTAB      0      0                                   192.168.120.204:ssh                                        10.2.0.68:49368   [root@localhost ~]#

執行個體2:顯示 Sockets 摘要

命令:

ss -s

輸出:

[root@localhost ~]# ss -sTotal: 34 (kernel 48)TCP:   4 (estab 1, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 3Transport Total     IP        IPv6*         48        -         -        RAW       0         0         0        UDP       5         5         0        TCP       4         4         0        INET      9         9         0        FRAG      0         0         0        [root@localhost ~]#

說明:

列出當前的established, closed, orphaned and waiting TCP sockets

執行個體3:列出所有開啟的網路連接連接埠

命令:

ss -l

輸出:

[root@localhost ~]# ss -lRecv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port        0                                              127.0.0.1:smux                                                    *:*            0                                                      *:3690                                                    *:*            0                                                      *:ssh                                                     *:*       [root@localhost ~]#

執行個體4:查看進程使用的socket

命令:

ss -pl

輸出:

[root@localhost ~]# ss -plRecv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port        0                                              127.0.0.1:smux                                                    *:*        users:(("snmpd",2716,8))     0                                                      *:3690                                                    *:*        users:(("svnserve",3590,3))     0                                                      *:ssh                                                     *:*        users:(("sshd",2735,3))[root@localhost ~]#

執行個體5:找出開啟通訊端/連接埠應用程式

命令:

ss -lp | grep 3306

輸出:

[root@localhost ~]# ss -lp|grep 1935     0                            *:1935                          *:*        users:(("fmsedge",2913,18))     0                    127.0.0.1:19350                         *:*        users:(("fmsedge",2913,17))[root@localhost ~]# ss -lp|grep 3306     0                            *:3306                          *:*        users:(("mysqld",2871,10))[root@localhost ~]#

執行個體6:顯示所有UDP Sockets

命令:

ss -u -a

輸出:

[root@localhost ~]# ss -u -aState      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   UNCONN     0      0                                         127.0.0.1:syslog                                             *:*       UNCONN     0      0                                                 *:snmp                                               *:*       ESTAB      0      0                                   192.168.120.203:39641                                  10.58.119.119:domain [root@localhost ~]#

執行個體7:顯示所有狀態為established的SMTP串連

命令:

ss -o state established '( dport = :smtp or sport = :smtp )'

輸出:

[root@localhost ~]# ss -o state established '( dport = :smtp or sport = :smtp )' Recv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port   [root@localhost ~]#

執行個體8:顯示所有狀態為Established的HTTP串連

命令:

ss -o state established '( dport = :http or sport = :http )'

輸出:

[root@localhost ~]# ss -o state established '( dport = :http or sport = :http )' Recv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port   0      0                                              75.126.153.214:2164                                        192.168.10.42:http    [root@localhost ~]# 

執行個體9:列舉出處於 FIN-WAIT-1狀態的源連接埠為 80或者 443,目標網路為 193.233.7/24所有 tcp通訊端

命令:

ss -o state fin-wait-1 '( sport = :http or sport = :https )' dst 193.233.7/24

輸出:

說明:

執行個體10:用TCP 狀態過濾Sockets:

命令:

ss -4 state FILTER-NAME-HERE

ss -6 state FILTER-NAME-HERE

輸出:

[root@localhost ~]#ss -4 state closing Recv-Q Send-Q                                                  Local Address:Port                                                      Peer Address:Port      11094                                                  75.126.153.214:http                                                      192.168.10.42:4669

說明:

FILTER-NAME-HERE 可以代表以下任何一個:

established

syn-sent

syn-recv

fin-wait-1

fin-wait-2

time-wait

closed

close-wait

last-ack

listen

closing

all : 所有以上狀態

connected : 除了listen and closed的所有狀態

synchronized :所有已串連的狀態除了syn-sent

bucket : 顯示狀態為maintained as minisockets,如:time-wait和syn-recv.

big : 和bucket相反.

執行個體11:匹配遠程地址和連接埠號碼

命令:

ss dst ADDRESS_PATTERN

ss dst 192.168.1.5

ss dst 192.168.119.113:http

ss dst 192.168.119.113:smtp

ss dst 192.168.119.113:443

輸出:

[root@localhost ~]# ss dst 192.168.119.113State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:20229   ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:61056   ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:61623   ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:60924   ESTAB      0      0                                   192.168.119.103:16050                                192.168.119.113:43701   ESTAB      0      0                                   192.168.119.103:16073                                192.168.119.113:32930   ESTAB      0      0                                   192.168.119.103:16073                                192.168.119.113:49318   ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:3844    [root@localhost ~]# ss dst 192.168.119.113:httpState      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   [root@localhost ~]# ss dst 192.168.119.113:3844State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   ESTAB      0      0                                   192.168.119.103:16014                                192.168.119.113:3844    [root@localhost ~]#

執行個體12:匹配本地地址和連接埠號碼

命令:

ss src ADDRESS_PATTERN

ss src 192.168.119.103

ss src 192.168.119.103:http

ss src 192.168.119.103:80

ss src 192.168.119.103:smtp

ss src 192.168.119.103:25

輸出:

[root@localhost ~]# ss src 192.168.119.103:16021State      Recv-Q Send-Q                                Local Address:Port                                    Peer Address:Port   ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:63054   ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:62894   ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:63055   ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:2274    ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:44784   ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:7233    ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.103:58660   ESTAB      0      0                                   192.168.119.103:16021                                192.168.119.201:44822   ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:56737   ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:57487   ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:56736   ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:64652   ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:56586   ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:64653   ESTAB      0      0                                   192.168.119.103:16021                                     10.2.1.206:56587   [root@localhost ~]#

執行個體13:將本地或者遠程連接埠和一個數比較

命令:

ss dport OP PORT

ss sport OP PORT

輸出:

[root@localhost ~]# ss  sport = :http [root@localhost ~]# ss  dport = :http [root@localhost ~]# ss  dport \> :1024 [root@localhost ~]# ss  sport \> :1024 [root@localhost ~]# ss sport \< :32000 [root@localhost ~]# ss  sport eq :22 [root@localhost ~]# ss  dport != :22 [root@localhost ~]# ss  state connected sport = :http [root@localhost ~]# ss \( sport = :http or sport = :https \) [root@localhost ~]# ss -o state fin-wait-1 \( sport = :http or sport = :https \) dst 192.168.1/24

說明:

ss dport OP PORT 遠程連接埠和一個數比較;ss sport OP PORT 本地連接埠和一個數比較。

OP 可以代表以下任意一個:

<= or le : 小於或等於連接埠號碼

>= or ge : 大於或等於連接埠號碼

== or eq : 等於連接埠號碼

!= or ne : 不等於連接埠號碼

< or gt : 小於連接埠號碼

> or lt : 大於連接埠號碼

執行個體14:ss 和 netstat 效率對比

命令:

time netstat -at

time ss

輸出:

[root@localhost ~]# time ss   real    0m0.739suser    0m0.019ssys     0m0.013s[root@localhost ~]# [root@localhost ~]# time netstat -atreal    2m45.907suser    0m0.063ssys     0m0.067s[root@localhost ~]#

說明:

用time 命令分別擷取通過netstat和ss命令擷取程式和概要佔用資源所使用的時間。在伺服器串連數比較多的時候,netstat的效率完全沒法和ss比。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.