温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux系统中如何使用ss命令

发布时间:2022-02-18 10:55:21 来源:亿速云 阅读:159 作者:小新 栏目:开发技术

这篇文章主要介绍了Linux系统中如何使用ss命令,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

ss是Socket Statistics的缩写。顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容。但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信息,而且比netstat更快速更高效。

Linux系统中如何使用ss命令

命令格式

ss [参数]

命令功能

ss(Socket Statistics 的缩写)命令可以用来获取 socket统计信息,此命令输出的结果类似于 netstat输出的内容,但它能显示更多更详细的 TCP连接状态的信息, 且比 netstat 更快速高效。它使用了 TCP协议栈中 tcp_diag(是一个用于分析统计的模块),能直接从获得第一手内核信息,这就使 得 ss命令快捷高效。在没有 tcp_diag,ss也可以正常运行。

命令参数

-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 ]

使用实例

实例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

实例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

**说明:**列出当前的established, closed, orphaned and waiting TCP sockets

实例3:列出所有打开的网络连接端口

[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                                                     *:*

实例4:查看进程使用的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))

实例5:找出打开套接字/端口应用程序

[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))

实例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

实例7:显示所有状态为established的SMTP连接

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

实例8:显示所有状态为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

实例9:列举出处于 FIN-WAIT-1状态的源端口为 80或者 443,目标网络为 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

实例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 
1      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

实例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

实例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 \
[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 ge : 大于或等于端口号
== or eq : 等于端口号
!= or ne : 不等于端口号
or lt : 大于端口号

实例14:ss 和 netstat 效率对比
命令:

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命令获取程序和概要占用资源所使用的时间。在服务器连接数比较多的时候,netstat的效率完全没法和ss比。

感谢你能够认真阅读完这篇文章,希望小编分享的“Linux系统中如何使用ss命令”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI