侧边栏壁纸
博主头像
汪洋

即使慢,驰而不息,纵会落后,纵会失败,但一定可以达到他所向的目标。 - 鲁迅

  • 累计撰写 190 篇文章
  • 累计创建 74 个标签
  • 累计收到 108 条评论

Linux 网络状态工具 - ss

汪洋
2022-01-04 / 0 评论 / 0 点赞 / 391 阅读 / 5,123 字

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

当服务器的 socket 连接数量变得非常大时,无论是使用 netstat 命令还是直接 cat /proc/net/tcp,执行速度都会很慢。ss 快的秘诀在于,它利用到了 TCP 协议栈中 tcp_diag。tcp_diag 是一个用于分析统计的模块,可以获得 Linux 内核中第一手的信息,这就确保了ss 的快捷高效。当然,如果你的系统中没有 tcp_diag,ss 也可以正常运行,只是效率会变得稍慢。

语法

ss [参数]
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 ]

实例

基本使用

$ ss -t -a    # 显示 TCP连接
$ ss -s       # 显示 Sockets 摘要
$ ss -l       # 列出所有打开的网络连接端口
$ ss -pl      # 查看进程使用的 socket
$ ss -lp | grep 3306  # 找出打开套接字/端口应用程序
$ ss -u -a    # 显示所有 UDP Sockets
$ ss -o state established '( dport = :smtp or sport = :smtp )' # 显示所有状态为 established 的 SMTP 连接
$ ss -o state established '( dport = :http or sport = :http )' # 显示所有状态为 Established 的 HTTP 连接
$ ss -o state fin-wait-1 '( sport = :http or sport = :https )' dst 193.233.7/24  # 列举出处于 FIN-WAIT-1 状态的源端口为 80或者 443,目标网络为 193.233.7/24所有 tcp套接字

# ss 和 netstat 效率对比
$ time netstat -at
$ time ss

# 匹配远程地址和端口号
# 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

# 匹配本地地址和端口号
# 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

将本地或者远程端口和一个数比较

# ss dport OP PORT 远程端口和一个数比较;
# ss sport OP PORT 本地端口和一个数比较
# OP 可以代表以下任意一个:
# <= or le : 小于或等于端口号
# >= or ge : 大于或等于端口号
# == or eq : 等于端口号
# != or ne : 不等于端口号
# < or gt : 小于端口号
# > or lt : 大于端口号
$ ss  sport = :http
$ ss  dport = :http
$ ss  dport \> :1024
$ ss  sport \> :1024
$ ss sport \< :32000
$ ss  sport eq :22
$ ss  dport != :22
$ ss  state connected sport = :http
$ ss \( sport = :http or sport = :https \)
$ ss -o state fin-wait-1 \( sport = :http or sport = :https \) dst 192.168.1/24

用 TCP 状态过滤 Sockets

$ ss -4 state closing
# ss -4 state FILTER-NAME-HERE
# ss -6 state FILTER-NAME-HERE
# 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相反.

显示 TCP 连接

[root@localhost ~]# ss -t -a
State       Recv-Q Send-Q                            Local Address:Port                                Peer Address:Port
LISTEN      0      0                                             *:3306                                           *:*
LISTEN      0      0                                             *:http                                           *:*
LISTEN      0      0                                             *:ssh                                            *:*
LISTEN      0      0                                     127.0.0.1:smtp                                           *:*
ESTAB       0      0                                112.124.15.130:42071                              42.156.166.25:http
ESTAB       0      0                                112.124.15.130:ssh                              121.229.196.235:33398

显示 Sockets 摘要

[root@localhost ~]# ss -s
Total: 172 (kernel 189)
TCP:   10 (estab 2, closed 4, orphaned 0, synrecv 0, timewait 0/0), ports 5

Transport Total     ip        IPv6
*         189       -         -
RAW       0         0         0
UDP       5         5         0
TCP       6         6         0
INET      11        11        0
FRAG      0         0         0
# 列出当前的 established, closed, orphaned and waiting TCP sockets

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

[root@localhost ~]# ss -l
Recv-Q Send-Q                                 Local Address:Port                                     Peer Address:Port
0      0                                                  *:3306                                                *:*
0      0                                                  *:http                                                *:*
0      0                                                  *:ssh                                                 *:*
0      0                                          127.0.0.1:smtp                                                *:*

查看进程使用的 socket

[root@localhost ~]# ss -pl
Recv-Q Send-Q                                          Local Address:Port                                              Peer Address:Port
0      0                                                           *:3306                                                         *:*        users:(("mysqld",1718,10))
0      0                                                           *:http                                                         *:*        users:(("nginx",13312,5),("nginx",13333,5))
0      0                                                           *:ssh                                                          *:*        users:(("sshd",1379,3))
0      0                                                   127.0.0.1:smtp                                                         *:*        us

找出打开套接字/端口应用程序

[root@localhost ~]# ss -pl | grep 3306
0      0                            *:3306                          *:*        users:(("mysqld",1718,10))

显示所有 UDP Sockets

[root@localhost ~]# ss -u -a
State       Recv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port
UNCONN      0      0                                                      *:syslog                                                  *:*
UNCONN      0      0                                         112.124.15.130:ntp                                                     *:*
UNCONN      0      0                                            10.160.7.81:ntp                                                     *:*
UNCONN      0      0                                              127.0.0.1:ntp                                                     *:*
UNCONN      0      0                                                      *:ntp                                                     *:*

出所有端口为 22(ssh)的连接

[root@localhost ~]# ss state all sport = :ssh
Netid State      Recv-Q Send-Q     Local Address:Port                      Peer Address:Port
tcp   LISTEN     0      128                    *:ssh                                  *:*
tcp   ESTAB      0      0          192.168.0.136:ssh                      192.168.0.102:46540
tcp   LISTEN     0      128                   :::ssh                                 :::*

查看 TCP 的连接状态

[root@localhost ~]# ss  -tan|awk 'NR>1{++S[$1]}END{for (a in S) print a,S[a]}'
LISTEN 7
ESTAB 31
TIME-WAIT 28
ss 常用的 state 状态
1. established
2. syn-sent
3. syn-recv
4. fin-wait-1
5. fin-wait-2
6. time-wait
7. closed
8. close-wait
9. last-ack
10. listen
11. closing
12. all : All of the above states
13. connected : All the states except for listen and closed
14. synchronized : All the connected states except for syn-sent
15. bucket : Show states, which are maintained as minisockets, i.e. time-wait and syn-recv.
16. big : Opposite to bucket state.

  • 主动连接端可能的状态有: CLOSED SYN_SEND ESTABLISHED
  • 主动关闭端可能的状态有: FIN_WAIT_1 FIN_WAIT_2 TIME_WAIT
  • 被动连接端可能的状态有: LISTEN SYN_RECV ESTABLISHED
  • 被动关闭端可能的状态有: CLOSE_WAIT LAST_ACK CLOSED

ss 使用端口筛选

ss dport OP PORT
OP:是运算符
PORT:表示端口
dport:表示过滤目标端口、相反的有sport
OP 运算符如下
1. <= or le : Less than or equal to port
2. >= or ge : Greater than or equal to port
3. == or eq : Equal to port
4. != or ne : Not equal to port
5. < or gt : Less than to port
6. > or lt : Greater than to port
7. Note: le, gt, eq, ne etc. are use in unix shell and are accepted as well.
OP 实例
$ ss sport = :http 也可以是 ss sport = :80
$ ss dport = :http
$ ss dport \> :1024
$ ss sport \> :1024
$ ss sport \< :32000
$ ss sport eq :22
$ ss dport != :22
$ ss state connected sport = :http
$ ss \( sport = :http or sport = :https \)
$ ss -o state fin-wait-1 \( sport = :http or sport = :https \) dst 192.168.1/24
0

评论区