SS命令使用介绍

ss


一. 命令介绍
先使用手册查看命令介绍信息

NAME
ss - another utility to investigate sockets

DESCRIPTION
ss is used to dump socket statistics. It allows showing information similar
to netstat. It can display more TCP and state informations than other tools.

ss(Socket Statistics)命令主要功能是显示套接字信息。与netstat命令的使用十分的相似,都是用于显示套接字信息,而ss命令的优势在于它能够显示更多TCP和连接状态的详情信息,并且速度更快更高效。

二. 语法格式
ss的语法格式是:ss 【参数】【过滤器】

SYNOPSIS
ss [options] [ FILTER ]

三. 基本参数
命令的基本参数有以下这些,参数会比较多,当然也可以说明这个命令的用法会挺丰富的。


-n  
 
 不解析域名
-a  
 
 显示所有套接字
-l    
 
显示处于监听状态的套接字
-o  
 
 显示计时器信息
-e  
 
 显示详细的套接字信息
-m  
 
 显示socket的内存情况
-p  
 
 显示使用套接字的过程
-i    
 
显示内部的TCP信息
-s    
 
显示socket使用情况
-4    
 
显示ipv4的套接字信息
-6    
 
显示ipv6的套接字信息
-0    
 
显示PACKET套接字信息
-t  
 
 显示TCP的套接字信息
-u  
 
 显示UDP套接字信息
-d    
 
显示DCCP套接字信息
-w    
 
显示RAW套接字信息
-D   将原始TCP套接字信息转储到文件
-r    解析IP和端口号


四. 各列所代表的含义

Netid    网络标识符
State    每个服务的连接状态
Recv-Q    接收socket(套接字)队列中的数据量,以字节为单位
Send-Q    发送socket(套接字)队列中的数据量,以字节为单位
Local Address    运行本地的计算机地址
Port    服务端口
Peer Address    对等端口


五. 参考实例


1.列出已建立的连接
默认情况,只使用ss命令,不加任何参数的情况下,它会显示所有已建立连接的套接字列表信息

[root@localhost ~]# ss |wc -l
716
[root@localhost ~]# ss | head -n 3
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
u_str  ESTAB      0      0       * 37084                 * 37928                
u_str  ESTAB      0      0       * 36812                 * 36814                


2.监听TCP协议的套接字信息
可以使用-l参数监听所有tcp协议的套接字内容

<pre>[root@localhost ~]# ss -lt
State      Recv-Q Send-Q  Local Address:Port                   Peer Address:Port                
LISTEN     0      100         127.0.0.1:smtp                              *:*                    
LISTEN     0      128                 *:sunrpc                            *:*                    
LISTEN     0      5       192.168.122.1:domain                            *:*                    
LISTEN     0      128                 *:ssh                               *:*                    
LISTEN     0      128         127.0.0.1:ipp                               *:*                    
LISTEN     0      100             [::1]:smtp                           [::]:*                    
LISTEN     0      128              [::]:sunrpc                         [::]:*                    
LISTEN     0      128              [::]:ssh                            [::]:*                    
LISTEN     0      128             [::1]:ipp                            [::]:*                    


3. 查看主机监听的端口
-n表示不解析域名,显示的是IP+端口的格式

[root@localhost ~]# ss -tnl 
State      Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN     0      100           127.0.0.1:25                                *:*                  
LISTEN     0      128                   *:111                               *:*                  
LISTEN     0      5         192.168.122.1:53                                *:*                  
LISTEN     0      128                   *:22                                *:*                  
LISTEN     0      128           127.0.0.1:631                               *:*              


4. 显示在运行进程的信息
需要使用到-p的参数,结合-tl是显示tcp协议的服务并且处于监听状态下的信息

[root@localhost ~]# ss -tlp  |head -10
State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
LISTEN     0      100    127.0.0.1:smtp                     *:*                     users:(("master",pid=1284,fd=13))
LISTEN     0      128        *:sunrpc                   *:*                     users:(("rpcbind",pid=704,fd=8))
LISTEN     0      5      192.168.122.1:domain                   *:*                     users:(("dnsmasq",pid=1533,fd=6))
LISTEN     0      128        *:ssh                      *:*                     users:(("sshd",pid=1176,fd=3))
LISTEN     0      128    127.0.0.1:ipp                      *:*                     users:(("cupsd",pid=1168,fd=11))
LISTEN     0      100    [::1]:smtp                  [::]:*                     users:(("master",pid=1284,fd=14))


可以看到在最后一列会显示进程的名称、PID、fd文件描述符等信息。
还可通过grep过滤信息来获取想知道的服务信息

[root@localhost ~]# ss -ltp | grep ssh
LISTEN     0      128        *:ssh                      *:*                     users:(("sshd",pid=1176,fd=3))
LISTEN     0      128     [::]:ssh                   [::]:*                     users:(("sshd",pid=1176,fd=4))


5. 显示所有已经建立的信息
需要使用-a参数,表示显示所有的连接信息

[root@localhost ~]# ss -a | wc -l
963
[root@localhost ~]# ss  | wc -l
716
1
2
3
4


因为信息太多,这里就不列出了,可以看下显示所有与默认的情况下,会多出来比较多行信息。
还可以在加上-t -n参数使用

[root@localhost ~]# ss -ant 
State      Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN     0      100           127.0.0.1:25                                *:*                  
LISTEN     0      128                   *:111                               *:*                  
LISTEN     0      5         192.168.122.1:53                                *:*                  
LISTEN     0      128                   *:22                                *:*                  
LISTEN     0      128           127.0.0.1:631                               *:*                  
ESTAB      0      0       192.168.157.166:22                  192.168.157.213:65011              
LISTEN     0      100               [::1]:25                             [::]:*                  
LISTEN     0      128                [::]:111                            [::]:*                  
LISTEN     0      128                [::]:22                             [::]:*                  
LISTEN     0      128               [::1]:631                            [::]:*        

   
6. 显示服务更多的信息
如显示-o时间信息、-m显示套接字使用的内容、-i显示内部tcp的信息

[root@localhost ~]# ss -imo | head -2
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
u_str  ESTAB      0      0       * 37084                 * 37928                 skmem:(r0,rb212992,t0,tb212992,f0,w0,o0,bl0,d0)


7. 显示套接字的使用信息
显示使用信息需要用到-s的参数

[root@localhost ~]# ss -s 
Total: 1711 (kernel 2055)
TCP:   11 (estab 1, closed 1, orphaned 0, synrecv 0, timewait 0/0), ports 0

Transport Total     IP        IPv6
*      2055      -         -        
RAW      1         0         1        
UDP      11        8         3        
TCP      10        6         4        
INET      22        14        8        
FRAG      0         0         0        


8. 显示IPv4的信息
需要使用-4的参数,在后面跟上要过滤的信息,这里只显示正在监听的服务。

[root@localhost ~]# ss -4 state listening
Netid  Recv-Q Send-Q    Local Address:Port                     Peer Address:Port                
tcp    0      100           127.0.0.1:smtp                                *:*                    
tcp    0      128                   *:sunrpc                              *:*                    
tcp    0      5         192.168.122.1:domain                              *:*                    
tcp    0      128                   *:ssh                                 *:*                    
tcp    0      128           127.0.0.1:ipp                                 *:*             


若要显示IPv6的可以使用-6的参数

[root@localhost ~]# ss -6 state listening
Netid  Recv-Q Send-Q    Local Address:Port                     Peer Address:Port                
tcp    0      100               [::1]:smtp                             [::]:*                    
tcp    0      128                [::]:sunrpc                           [::]:*                    
tcp    0      128                [::]:ssh                              [::]:*                    
tcp    0      128               [::1]:ipp                              [::]:*           


9. 匹配远程与本地地址和端口号
这里需要用到几种参数格式

分别是:
dst :过滤连接的目标地址
src: 过滤连接的本地地址
dport:目标端口
sport:来源端口

匹配远程地址和端口号

ss dst 192.168.157.33
ss dst 192.168.157.33:443
ss dst 192.168.157.33:http
1
2
3
匹配本地端口和端口号

ss src 192.168.157.166
ss src 192.168.157.166:22
ss src 192.168.157.166:ssh
1
2
3
9.1 当然还可以将本地与远程端口和一个数进行比较
使用方法:

ss dport OP num
ss sport OP num
1
2
以上文本中的OP可以使用以下这些内容

<=    le    小于等于
==    eq    等于
!=    ne    不等于
>    gt    大于
<    lt    小于
是不是突然觉得表格中的符号这么熟悉,如果有学习过shell脚本的,应该一眼就能看出来符号代表的是什么意思。
下面继续来演示下如何使用:
显示来源端口小于50的端口号的内容

[root@localhost ~]# ss -ntul sport lt 50
Netid  State      Recv-Q Send-Q Local Address:Port                Peer Address:Port              
tcp    LISTEN     0      100        127.0.0.1:25                             *:*                  
tcp    LISTEN     0      128                *:22                             *:*                  
tcp    LISTEN     0      100            [::1]:25                          [::]:*                  
tcp    LISTEN     0      128             [::]:22                          [::]:*              
1
2
3
4
5
6
如果要用符号,则需要在符号前面加上转义符。

[root@localhost ~]# ss -ntul sport \< 50
Netid  State      Recv-Q Send-Q Local Address:Port                Peer Address:Port              
tcp    LISTEN     0      100        127.0.0.1:25                             *:*                  
tcp    LISTEN     0      128                *:22                             *:*                  
tcp    LISTEN     0      100            [::1]:25                          [::]:*                  
tcp    LISTEN     0      128             [::]:22                          [::]:*            

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值