linux-command/command/ss.md

116 lines
5.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

ss
===
获取socket统计信息
## 补充说明
**ss命令** 用来显示处于活动状态的套接字信息。ss命令可以用来获取socket统计信息它可以显示和netstat类似的内容。但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信息而且比netstat更快速更高效。
当服务器的socket连接数量变得非常大时无论是使用netstat命令还是直接`cat /proc/net/tcp`执行速度都会很慢。可能你不会有切身的感受但请相信我当服务器维持的连接达到上万个的时候使用netstat等于浪费 生命而用ss才是节省时间。
天下武功唯快不破。ss快的秘诀在于它利用到了TCP协议栈中tcp_diag。tcp_diag是一个用于分析统计的模块可以获得Linux 内核中第一手的信息这就确保了ss的快捷高效。当然如果你的系统中没有tcp_diagss也可以正常运行只是效率会变得稍慢。
### 语法
```
ss(选项)
```
### 选项
```
-h显示帮助信息
-V显示指令版本信息
-n不解析服务名称以数字方式显示
-a显示所有的套接字
-l显示处于监听状态的套接字
-o显示计时器信息
-m显示套接字的内存使用情况
-p显示使用套接字的进程信息
-i显示内部的TCP信息
-4只显示ipv4的套接字
-6只显示ipv6的套接字
-t只显示tcp套接字
-u只显示udp套接字
-d只显示DCCP套接字
-w仅显示RAW套接字
-x仅显示UNIX域套接字。
```
### 实例
**显示ICP连接**
```
[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 *:*
```
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->