首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

【Linux】一步一步学Linux——ss命令(170)

  • 23-11-18 13:20
  • 4227
  • 13426
blog.csdn.net

00. 目录

文章目录

    • 00. 目录
    • 01. 命令概述
    • 02. 命令格式
    • 03. 常用选项
    • 04. 参考示例
    • 05. 附录

01. 命令概述

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是Socket Statistics的缩写。

02. 命令格式

ss [选项]
ss [选项] [过滤]
  • 1
  • 2

03. 常用选项

-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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

04. 参考示例

4.1 显示TCP连接

[root@localhost ~]# ss -t -a
State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
LISTEN     0      80        *:mysql                   *:*                    
LISTEN     0      128       *:sunrpc                  *:*                    
LISTEN     0      5      192.168.122.1:domain                  *:*                    
LISTEN     0      128       *:ssh                     *:*                    
LISTEN     0      128    127.0.0.1:ipp                     *:*                    
ESTAB      0      0      172.16.0.76:ssh                  172.16.0.51:50412                
LISTEN     0      128      :::sunrpc                 :::*                    
LISTEN     0      128      :::ssh                    :::*                    
LISTEN     0      128     ::1:ipp                    :::*                    
[root@localhost ~]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

4.2 显示套接字使用概况

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

Transport Total     IP        IPv6
*         2523      -         -        
RAW       1         0         1        
UDP       11        8         3        
TCP       9         6         3        
INET      21        14        7        
FRAG      0         0         0        

[root@localhost ~]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

4.3 列出所有打开的网络连接端口

[root@localhost ~]# ss -l
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
nl     UNCONN     0      0      rtnl:evolution-addre/3016   *                     
nl     UNCONN     0      0      rtnl:packagekitd/1658       *                     
nl     UNCONN     0      0      rtnl:1334                   *                     
nl     UNCONN     0      0      rtnl:avahi-daemon/630       *  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4.4 显示LISTEN状态的进程信息

[root@localhost ~]# ss -lp
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
nl     UNCONN     0      0      rtnl:evolution-addre/3016   *                     
nl     UNCONN     0      0      rtnl:packagekitd/1658       *                     
nl     UNCONN     0      0      rtnl:1334                   *                     
nl     UNCONN     0      0      rtnl:avahi-daemon/630       *   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4.5 查看指定端口的信息

[root@localhost ~]# ss -lp | grep 80
tcp    LISTEN     0      80      *:mysql                 *:*                     users:(("mysqld",pid=1546,fd=24))
[root@localhost ~]# 
  • 1
  • 2
  • 3

4.6 查看所有UDP套接字

[root@localhost ~]# ss -u -a
State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
UNCONN     0      0         *:mdns                    *:*                    
UNCONN     0      0      127.0.0.1:323                     *:*                    
UNCONN     0      0         *:17815                   *:*                    
UNCONN     0      0         *:783                     *:*                    
UNCONN     0      0      192.168.122.1:domain                  *:*                    
UNCONN     0      0      *%virbr0:bootps                  *:*                    
UNCONN     0      0         *:bootpc                  *:*                    
UNCONN     0      0         *:sunrpc                  *:*                    
UNCONN     0      0       ::1:323                    :::*                    
UNCONN     0      0        :::783                    :::*                    
UNCONN     0      0        :::sunrpc                 :::*                    
[root@localhost ~]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4.7 显示所有状态为established的SMTP连接

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

4.8 显示所有状态为Established的HTTP连接

[root@localhost ~]# ss -o state established '( dport = :http or sport = :http )' 
Netid  Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
[root@localhost ~]# 
  • 1
  • 2
  • 3

4.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/24
Netid  Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
[root@localhost ~]# 
  • 1
  • 2
  • 3

4.10 查询关闭状态的套接字

[root@localhost ~]# ss -4 state closing
Netid  Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
[root@localhost ~]# 
  • 1
  • 2
  • 3

ss -4 state 状态

ss -6 state 状态

状态可以是如下:

established

syn-sent

syn-recv

fin-wait-1

fin-wait-2

time-wait

closed

close-wait

last-ack

listen

closing

4.11 匹配目的IP和端口

[deng@localhost ~]$ ss dst 192.168.12.94
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
[deng@localhost ~]$ ss dst 192.168.12.94:http
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
[deng@localhost ~]$ ss dst 192.168.12.94:1521
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4.12 匹配本地IP和端口

[deng@localhost ~]$ ss src 192.168.12.94
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
tcp    ESTAB      0      0      192.168.12.94:ssh                  192.168.12.87:52486                
[deng@localhost ~]$ ss src 192.168.12.94:http
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
[deng@localhost ~]$ ss src 192.168.12.94:1521
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4.13 端口比较

[deng@localhost ~]# ss  sport = :http 
[deng@localhost ~]# ss  dport = :http 
[deng@localhost ~]# ss  dport \> :1024 
[deng@localhost ~]# ss  sport \> :1024 
[deng@localhost ~]# ss sport \< :32000 
[deng@localhost ~]# ss  sport eq :22 
[deng@localhost ~]# ss  dport != :22 
[deng@localhost ~]# ss  state connected sport = :http 
[deng@localhost ~]# ss \( sport = :http or sport = :https \) 
[deng@localhost ~]# ss -o state fin-wait-1 \( sport = :http or sport = :https \) dst 192.168.1/24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

说明:

ss dport OP PORT 
ss sport OP PORT
  • 1
  • 2

ss dport OP PORT 远程端口和一个数比较;ss sport OP PORT 本地端口和一个数比较。

OP 可以代表以下任意一个:

<= or le : 小于或等于端口号

>= or ge : 大于或等于端口号

== or eq : 等于端口号

!= or ne : 不等于端口号

< or gt : 小于端口号

> or lt : 大于端口号

4.14 ss和netstat比较

[deng@localhost ~]$ time ss
real    0m0.020s
user    0m0.005s
sys     0m0.005s

[deng@localhost ~]$ time netstat -at
real    0m10.266s
user    0m0.015s
sys     0m0.032s
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

说明:

用time 命令分别获取通过netstat和ss命令获取程序和概要占用资源所使用的时间。在服务器连接数比较多的时候,netstat的效率完全没法和ss比。

4.15 显示连接X服务器的进程

[deng@localhost ~]$ ss -x src /tmp/.X11-unix/X0
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
[deng@localhost ~]$ 
  • 1
  • 2
  • 3

05. 附录

参考:【Linux】一步一步学Linux系列教程汇总

注:本文转载自blog.csdn.net的沧海一笑-dj的文章"https://blog.csdn.net/dengjin20104042056/article/details/100022996"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top