ss命令用來顯示處於活動狀態的套接字信息。
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也可以正常運行,只是效率會變得稍慢。
語法
ss(選項)
選項
-h:顯示幫助信息;
-V:顯示指令版本信息;
-n:不解析服務名稱,以數字方式顯示;
-a:顯示所有的套接字;
-l:顯示處於監聽狀態的套接字;
-o:顯示計時器信息;
-m:顯示套接字的內存使用情況;
-p:顯示使用套接字的進程信息;
-i:顯示內部的TCP信息;
-4:只顯示ipv4的套接字;
-6:只顯示ipv6的套接字;
-t:只顯示tcp套接字;
-u:只顯示udp套接字;
-d:只顯示DCCP套接字;
-w:僅顯示RAW套接字;
-x:僅顯示UNIX域套接字。
實例:
顯示ICP連接
[root@localhost ~]# ss -t -a
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 0 *:3306 :
LISTEN 0 0 *:http :
LISTEN 0 0 *:ssh :
LISTEN 0 0 127.0.0.1:smtp :
ESTAB 0 0 112.124.15.130:42071 42.156.166.25:http
ESTAB 0 0 112.124.15.130:ssh 121.229.196.235:33398
顯示 Sockets 摘要
[root@localhost ~]# ss -s
Total: 172 (kernel 189)
TCP: 10 (estab 2, closed 4, orphaned 0, synrecv 0, timewait 0/0), ports 5
Transport Total ip IPv6
-
189 - -
RAW 0 0 0
UDP 5 5 0
TCP 6 6 0
INET 11 11 0
FRAG 0 0 0
列出當前的established, closed, orphaned and waiting TCP sockets
列出所有打開的網絡連接端口
[root@localhost ~]# ss -l
Recv-Q Send-Q Local Address:Port Peer Address:Port
0 0 *:3306 :
0 0 *:http :
0 0 *:ssh :
0 0 127.0.0.1:smtp :
查看進程使用的socket
[root@localhost ~]# ss -pl
Recv-Q Send-Q Local Address:Port Peer Address:Port
0 0 *:3306 : users:(("mysqld",1718,10))
0 0 *:http : users:(("nginx",13312,5),("nginx",13333,5))
0 0 *:ssh : users:(("sshd",1379,3))
0 0 127.0.0.1:smtp : us
找出打開套接字/端口應用程序
[root@localhost ~]# ss -pl | grep 3306
0 0 *:3306 : users:(("mysqld",1718,10))
顯示所有UDP Sockets
[root@localhost ~]# ss -u -a
State Recv-Q Send-Q Local Address:Port Peer Address:Port
UNCONN 0 0 *:syslog :
UNCONN 0 0 112.124.15.130:ntp :
UNCONN 0 0 10.160.7.81:ntp :
UNCONN 0 0 127.0.0.1:ntp :
UNCONN 0 0 *:ntp :
[root@Mysql ~]# ss -ntpl | grep 3306
LISTEN 0 80 *:3306 : users:(("mysqld",26375,28))