溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Linux系統(tǒng)中如何使用ss命令

發(fā)布時(shí)間:2022-02-18 10:55:21 來源:億速云 閱讀:157 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Linux系統(tǒng)中如何使用ss命令,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

ss是Socket Statistics的縮寫。顧名思義,ss命令可以用來獲取socket統(tǒng)計(jì)信息,它可以顯示和netstat類似的內(nèi)容。但ss的優(yōu)勢(shì)在于它能夠顯示更多更詳細(xì)的有關(guān)TCP和連接狀態(tài)的信息,而且比netstat更快速更高效。

Linux系統(tǒng)中如何使用ss命令

命令格式

ss [參數(shù)]

命令功能

ss(Socket Statistics 的縮寫)命令可以用來獲取 socket統(tǒng)計(jì)信息,此命令輸出的結(jié)果類似于 netstat輸出的內(nèi)容,但它能顯示更多更詳細(xì)的 TCP連接狀態(tài)的信息, 且比 netstat 更快速高效。它使用了 TCP協(xié)議棧中 tcp_diag(是一個(gè)用于分析統(tǒng)計(jì)的模塊),能直接從獲得第一手內(nèi)核信息,這就使 得 ss命令快捷高效。在沒有 tcp_diag,ss也可以正常運(yùn)行。

命令參數(shù)

-h, --help 幫助信息
-V, --version 程序版本信息
-n, --numeric 不解析服務(wù)名稱
-r, --resolve        解析主機(jī)名
-a, --all 顯示所有套接字(sockets)
-l, --listening 顯示監(jiān)聽狀態(tài)的套接字(sockets)
-o, --options        顯示計(jì)時(shí)器信息
-e, --extended       顯示詳細(xì)的套接字(sockets)信息
-m, --memory         顯示套接字(socket)的內(nèi)存使用情況
-p, --processes 顯示使用套接字(socket)的進(jìn)程
-i, --info 顯示 TCP內(nèi)部信息
-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),F(xiàn)AMILY可選,支持  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)信息轉(zhuǎn)儲(chǔ)到文件
-F, --filter=FILE   從文件中都去過濾器信息
       FILTER := [ state TCP-STATE ] [ EXPRESSION ]

使用實(shí)例

實(shí)例1:顯示TCP連接

[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

實(shí)例2:顯示 Sockets 摘要

[root@localhost ~]# ss -sTotal: 34 (kernel 48)
TCP:   4 (estab 1, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 3

Transport Total     IP        IPv6
*         48        -         -        
RAW       0         0         0        
UDP       5         5         0        
TCP       4         4         0        
INET      9         9         0        
FRAG      0         0         0

**說明:**列出當(dāng)前的established, closed, orphaned and waiting TCP sockets

實(shí)例3:列出所有打開的網(wǎng)絡(luò)連接端口

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

實(shí)例4:查看進(jìn)程使用的socket

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

實(shí)例5:找出打開套接字/端口應(yīng)用程序

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

實(shí)例6:顯示所有UDP Sockets

[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

實(shí)例7:顯示所有狀態(tài)為established的SMTP連接

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

實(shí)例8:顯示所有狀態(tài)為Established的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

實(shí)例9:列舉出處于 FIN-WAIT-1狀態(tài)的源端口為 80或者 443,目標(biāo)網(wǎng)絡(luò)為 193.233.7/24所有 tcp套接字

[root@localhost ~]#  ss -o state fin-wait-1 '( sport = :http or sport = :https )' dst 193.233.7/24Recv-Q Send-Q                                       Local Address:Port                                           Peer Address:Port

實(shí)例10:用TCP 狀態(tài)過濾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 
1      11094                                                  75.126.153.214:http                                                      192.168.10.42:4669

說明:
FILTER-NAME-HERE 可以代表以下任何一個(gè):
established
syn-sent
syn-recv
fin-wait-1
fin-wait-2
time-wait
closed
close-wait
last-ack
listen
closing

all : 所有以上狀態(tài)
connected : 除了listen and closed的所有狀態(tài)
synchronized :所有已連接的狀態(tài)除了syn-sent
bucket : 顯示狀態(tài)為maintained as minisockets,如:time-wait和syn-recv.
big : 和bucket相反.

實(shí)例11:匹配遠(yuǎn)程地址和端口號(hào)
命令:

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

實(shí)例12:匹配本地地址和端口號(hào)
命令:

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

實(shí)例13:將本地或者遠(yuǎn)程端口和一個(gè)數(shù)比較
命令:

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 \
[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 遠(yuǎn)程端口和一個(gè)數(shù)比較;ss sport OP PORT 本地端口和一個(gè)數(shù)比較。
OP 可以代表以下任意一個(gè):
= or ge : 大于或等于端口號(hào)
== or eq : 等于端口號(hào)
!= or ne : 不等于端口號(hào)
or lt : 大于端口號(hào)

實(shí)例14:ss 和 netstat 效率對(duì)比
命令:

time netstat -at
time ss

輸出:

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

**說明:**用time 命令分別獲取通過netstat和ss命令獲取程序和概要占用資源所使用的時(shí)間。在服務(wù)器連接數(shù)比較多的時(shí)候,netstat的效率完全沒法和ss比。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Linux系統(tǒng)中如何使用ss命令”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI