iproute家族命令之ss

一、简介

ss命令是用来显示处于活动状态的套接字连接信息,比起netstat命令,ss命令具有显示信息更多更详细并且更快速高效的特点。当服务器的socket连接数量非常庞大的时候,netstat命令执行的时候会非常缓慢,此时ss命令就是更好地选择。

二、ss命令的使用

1、命令格式

ss [options] [ FILTER ]

2、命令选项

-t:显示tcp协议相关的连接;
-n:以数字格式显示信息;
-u:显示udp协议相关的连接;
-l:显示处于监听状态的连接;
-a:显示所有连接;
-e:显示扩展信息;
-m:显示使用信息;
-o:显示计时器信息;
-4:仅显示ipv4的套接字信息;
-p:显示使用套接字的进程信息;

FILTER=[state STATE-FILTER] [EXPRESSION]
常见的STATE_FILTER包括:connected、time-wait、listening、closed、find_wait_{1|2}等等;

三、使用案例

  • 查看套接字对应的进程信息
root@localhost ~]# ss -pl
Netid  State      Recv-Q Send-Q        Local Address:Port                         Peer Address:Port                
u_str  LISTEN     0      128    /run/lvm/lvmetad.socket 13756                                   * 0                     users:(("lvmetad",pid=734,fd=3),("systemd",pid=1,fd=42))
u_str  LISTEN     0      128    /run/systemd/private 31434                                   * 0                     users:(("systemd",pid=1,fd=14))
u_dgr  UNCONN     0      0                         * 17810                                   * 8231                  users:(("NetworkManager",pid=976,fd=5))
u_dgr  UNCONN     0      0                         * 16890                                   * 8229                  users:(("systemd-logind",pid=884,fd=3))
u_dgr  UNCONN     0      0                         * 14561                                   * 8229                  users:(("systemd-udevd",pid=740,fd=5))
u_dgr  UNCONN     0      0                         * 31529                                   * 8231                  users:(("crond",pid=11530,fd=4))
......
  • 查看所有的TCP套接字连接信息
[root@localhost ~]# ss -t -a
State      Recv-Q Send-Q           Local Address:Port                            Peer Address:Port                
LISTEN     0      128                          *:ssh                                        *:*                    
LISTEN     0      100                  127.0.0.1:smtp                                       *:*                    
ESTAB      0      52               192.168.0.188:ssh                             192.168.0.38:53451                
ESTAB      0      0                192.168.0.188:ssh                             192.168.0.38:52967                
LISTEN     0      128                         :::http                                      :::*                    
LISTEN     0      128                         :::ssh                                       :::*                    
LISTEN     0      100                        ::1:smtp                                      :::*          ```
  • 查看状态为Established的ssh连接:
[root@localhost ~]# ss -o state established '( dport = :ssh or sport = :ssh )'
Netid  Recv-Q Send-Q             Local Address:Port                              Peer Address:Port                
tcp    0      52                 192.168.0.188:ssh                               192.168.0.38:53451                 timer:(on,384ms,0)
tcp    0      0                  192.168.0.188:ssh                               192.168.0.38:52967          
  • 显示状态为LISTEN的所有套接字连接:
[root@localhost ~]# ss -4 state listening
Netid  Recv-Q Send-Q             Local Address:Port                              Peer Address:Port                
tcp    0      128                            *:ssh                                          *:*                    
tcp    0      100                    127.0.0.1:smtp                                         *:*      
  • 显示指定源/目地址的套接字信息:
[root@localhost ~]# ss dst  192.168.0.38:*
Netid  State      Recv-Q Send-Q        Local Address:Port                         Peer Address:Port                
tcp    ESTAB      0      52            192.168.0.188:ssh                          192.168.0.38:53451                
tcp    ESTAB      0      0             192.168.0.188:ssh                          192.168.0.38:52967         
  • 显示指定源目端口的套接字信息:
[root@localhost ~]# ss -tan '( dport = :22 or sport = :22 )'
State      Recv-Q Send-Q             Local Address:Port                            Peer Address:Port              
LISTEN     0      128                            *:22                                         *:*                  
ESTAB      0      52                 192.168.0.188:22                              192.168.0.38:53451              
ESTAB      0      0                  192.168.0.188:22                              192.168.0.38:52967              
LISTEN     0      128                           :::22                                        :::*         
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。