歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(51)ss命令

每天一個Linux命令(51)ss命令

日期:2017/3/3 11:52:20   编辑:Linux技術

[b] ss命令用來顯示處於活動狀態的套接字信息。[/b][b] (1)用法:[/b]

[b] 用法: [/b] ss [參數]     ss [參數] [過濾]

[b] (2)功能:[/b][b] 功能: ss是類似netstat的工具。能顯示查看網絡狀態信息,包括TCP、UDP連接,端口。[/b]

  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也可以正常運行,只是效率會變得稍慢。

[b] (3)選項參數[/b] 1) -t, --tcp         僅顯示TCP套接字(sockets)

2) -a, --all         顯示所有套接字(sockets) 3) -s, --summary      顯示套接字(socket)使用概況

4) -l             顯示LISTEN狀態的連接(連接打開) 5) -p, --processes    顯示使用套接字(socket)的進程

6) -u, --udp        僅顯示 UCP套接字(sockets) 7) -d, --dccp        僅顯示 DCCP套接字(sockets)

8) -w, --raw         僅顯示 RAW套接字(sockets) 9) -x, --unix         僅顯示 Unix套接字(sockets)

[b] (4)實例:[/b] 1)[root@localhost sunjimeng]# ss -t -a      顯示TCP連接

[root@localhost sunjimeng]# ss -t -a
State      Recv-Q Send-Q      Local Address:Port          Peer Address:Port   
LISTEN     0      128                     *:ssh                      *:*       
LISTEN     0      128             127.0.0.1:ipp                      *:*       
LISTEN     0      100             127.0.0.1:smtp                     *:*       
TIME-WAIT  0      0         192.168.142.128:52859       115.28.122.210:http    
TIME-WAIT  0      0         192.168.142.128:53367      112.124.140.210:http    
LISTEN     0      128                    :::ssh                     :::*       
LISTEN     0      128                   ::1:ipp                     :::*       
LISTEN     0      100                   ::1:smtp                    :::*       
[root@localhost sunjimeng]# netstat -ta
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN     
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN     
tcp        0      0 192.168.142.128:52926   ec2-52-36-148-12.:https ESTABLISHED
tcp        0      0 192.168.142.128:54577   ec2-52-26-186-108:https ESTABLISHED
tcp        0      0 192.168.142.128:54576   ec2-52-26-186-108:https ESTABLISHED
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN     
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN     
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN
2)[root@localhost sunjimeng]# ss -s      顯示套接字使用概況(列出當前的established, closed, orphaned and waiting TCP sockets)

[root@localhost sunjimeng]# ss -s
Total: 1133 (kernel 1377)
TCP:   7 (estab 0, closed 1, orphaned 0, synrecv 0, timewait 0/0), ports 0

Transport Total     IP        IPv6
*      1377      -         -        
RAW      1         0         1        
UDP      9         6         3        
TCP      6         3         3        
INET      16        9         7        
FRAG      0         0         0
3)[root@localhost sunjimeng]# ss -l        列出所有打開的網絡連接端口,即已經連接的網絡端口

[root@localhost sunjimeng]# ss -l
Netid  State      Recv-Q Send-Q           Local Address:Port               Peer Address:Port   
nl     UNCONN     0      0                         rtnl:NetworkManager/929                        *       
nl     UNCONN     0      0                         rtnl:gnome-shell/3126                        *       
......
u_dgr  UNCONN     0      0                            * 246524                        * 1492   
u_dgr  UNCONN     0      0                            * 158303                        * 1492   
raw    UNCONN     0      0                           :::ipv6-icmp                      :::*       
tcp    UNCONN     0      0                            *:ipproto-68                       *:*       
tcp    UNCONN     0      0                            *:ptp                           *:*       
tcp    UNCONN     0      0                    127.0.0.1:ipproto-323                       *:*       
tcp    UNCONN     0      0                            *:ipproto-5353                       *:*       
tcp    UNCONN     0      0                            *:ipproto-52525                       *:*       
tcp    UNCONN     0      0                            *:ipproto-16290                       *:*       
tcp    UNCONN     0      0                           :::ptp                          :::*       
tcp    UNCONN     0      0                          ::1:ipproto-323                      :::*       
tcp    UNCONN     0      0                           :::ipproto-44068                      :::*       
tcp    LISTEN     0      128                          *:ssh                           *:*       
tcp    LISTEN     0      128                  127.0.0.1:ipp                           *:*       
tcp    LISTEN     0      100                  127.0.0.1:smtp                          *:*       
tcp    LISTEN     0      128                         :::ssh                          :::*       
tcp    LISTEN     0      128                        ::1:ipp                          :::*       
tcp    LISTEN     0      100                        ::1:smtp                         :::*
4)[root@localhost sunjimeng]# ss -t -a -p      顯示使用套接字的進程的信息

[root@localhost sunjimeng]# ss -t -a
State      Recv-Q Send-Q              Local Address:Port                  Peer Address:Port   
LISTEN     0      128                             *:ssh                              *:*       
LISTEN     0      128                     127.0.0.1:ipp                              *:*       
LISTEN     0      100                     127.0.0.1:smtp                             *:*       
LISTEN     0      128                            :::ssh                             :::*       
LISTEN     0      128                           ::1:ipp                             :::*       
LISTEN     0      100                           ::1:smtp                            :::*       
[root@localhost sunjimeng]# ss -t -a -p
State      Recv-Q Send-Q              Local Address:Port                  Peer Address:Port   
LISTEN     0      128                             *:ssh                              *:*        users:(("sshd",1324,3))
LISTEN     0      128                     127.0.0.1:ipp                              *:*        users:(("cupsd",3130,12))
LISTEN     0      100                     127.0.0.1:smtp                             *:*        users:(("master",2575,13))
LISTEN     0      128                            :::ssh                             :::*        users:(("sshd",1324,4))
LISTEN     0      128                           ::1:ipp                             :::*        users:(("cupsd",3130,11))
LISTEN     0      100                           ::1:smtp                            :::*        users:(("master",2575,14))
5)[root@localhost sunjimeng]# ss -pl |grep 1487   顯示端口號為1487的已連接的套接字的進程信息

[root@localhost sunjimeng]# ss -pl |grep 1487
u_str  LISTEN     0      128    /run/systemd/journal/stdout 1487                  * 0       users:(("systemd-journal",617,3),("systemd",1,25))
[root@localhost sunjimeng]#
6)兩個命令的時間對比:

[root@localhost sunjimeng]# time ss

real    0m0.081s
user    0m0.000s
sys    0m0.004s
[root@localhost sunjimeng]# time netstat

real    0m0.018s
user    0m0.004s
sys    0m0.000s
[root@localhost sunjimeng]#

Copyright © Linux教程網 All Rights Reserved