Linux系统中ss命令是 Socket Statistics 的缩写。

ss 命令可以用来获取socket 统计信息,它显示的内容和 netstat 类似。但 ss 的优势在于它能够显示更多更详细的有关 TCP 和连接状态的信息,而且比 netstat 更快。当服务器的 socket 连接数量变得非常大时,无论是使用 netstat 命令还是直接 cat /proc/net/tcp,执行速度都会很慢。ss 命令利用到了 TCP 协议栈中tcp_diag。tcp_diag 是一个用于分析统计的模块,可以获得 Linux 内核中第一手的信息,因此 ss 命令的性能会好很多。也就是说 ss命令表示高效,准确。

ss命令支持命令组合,和netstat类似的,比如:

1.查看主机监听的端口

ss -tnl

[root@centos7 mnt]# ss -tlnr
State      Recv-Q Send-Q                              Local Address:Port                                             Peer Address:Port              
LISTEN     0      128                                             *:111                                                         *:*                  
LISTEN     0      5                                         centos7:53                                                          *:*                  
LISTEN     0      128                                             *:22                                                          *:*                  
LISTEN     0      128                                     localhost:631                                                         *:*                  
LISTEN     0      100                                     localhost:25                                                          *:*                  
LISTEN     0      128                                     localhost:6010                                                        *:*                  
LISTEN     0      128                                            :::111                                                        :::*                  
LISTEN     0      128                                            :::80                                                         :::*                  
LISTEN     0      128                                            :::22                                                         :::*                  
LISTEN     0      128                                     localhost:631                                                        :::*                  
LISTEN     0      100                                     localhost:25                                                         :::*                  
LISTEN     0      128                                     localhost:6010                                                       :::* 
我们可以看到,本地本机开启了 111,53,22,631,25,6010,80,25这几个tcp端口,也就是smtp服务-25,ssh-22,dns-53,http-80,xshell-631,cupsd-6010,docker的桥接网卡-111

2.通过 -r 选项解析 IP 和端口号

ss -tlr

[root@centos7 mnt]# ss -tlr
State      Recv-Q Send-Q                            Local Address:Port                                             Peer Address:Port                
LISTEN     0      128                                           *:rpc.portmapper                                              *:*                    
LISTEN     0      5                                       centos7:domain                                                      *:*                    
LISTEN     0      128                                           *:ssh                                                         *:*                    
LISTEN     0      128                                   localhost:ipp                                                         *:*                    
LISTEN     0      100                                   localhost:smtp                                                        *:*                    
LISTEN     0      128                                   localhost:x11-ssh-offset                                              *:*                    
LISTEN     0      128                                          :::rpc.portmapper                                             :::*                    
LISTEN     0      128                                          :::http                                                       :::*                    
LISTEN     0      128                                          :::ssh                                                        :::*                    
LISTEN     0      128                                   localhost:ipp                                                        :::*                    
LISTEN     0      100                                   localhost:smtp                                                       :::*                    
LISTEN     0      128                                   localhost:x11-ssh-offset                                             :::*

      
3.使用 -p 选项查看监听端口的程序名称

ss -tlp

4.还可以通过 grep 对监听端口进行进一步过滤

ss -tlp | grep ssh

[root@centos7 mnt]# ss -tlp | grep ssh
LISTEN     0      128        *:ssh                      *:*                     users:(("sshd",pid=1015,fd=3))
LISTEN     0      128    127.0.0.1:x11-ssh-offset           *:*                     users:(("sshd",pid=1839,fd=9))
LISTEN     0      128       :::ssh                     :::*                     users:(("sshd",pid=1015,fd=4))
LISTEN     0      128      ::1:x11-ssh-offset          :::*                     users:(("sshd",pid=1839,fd=8))


5.查看建立的 TCP 连接

ss -tna

estab 这一行表示,192.168.0.2:49899 这个机器通过ssh连接到了192.168.0.17,17这个机器是使用的默认ssh端口。

[root@centos7 mnt]# ss -tan
State      Recv-Q Send-Q                              Local Address:Port                                             Peer Address:Port              
LISTEN     0      128                                             *:111                                                         *:*                  
LISTEN     0      5                                   192.168.122.1:53                                                          *:*                  
LISTEN     0      128                                             *:22                                                          *:*                  
LISTEN     0      128                                     127.0.0.1:631                                                         *:*                  
LISTEN     0      100                                     127.0.0.1:25                                                          *:*                  
LISTEN     0      128                                     127.0.0.1:6010                                                        *:*                  
ESTAB      0      52                                   192.168.0.17:22                                                192.168.0.2:49899              
LISTEN     0      128                                            :::111                                                        :::*                  
LISTEN     0      128                                            :::80                                                         :::*                  
LISTEN     0      128                                            :::22                                                         :::*                  
LISTEN     0      128                                           ::1:631                                                        :::*                  
LISTEN     0      100                                           ::1:25                                                         :::*                  
LISTEN     0      128                                           ::1:6010       
 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐