ss
(Socket Statistics)是Linux系统中用于分析网络连接信息的高效工具,作为netstat
的现代化替代品,它以更快的执行速度和更详细的连接状态展示著称。本文将从基础语法到实战技巧,全面解析这一网络诊断利器的使用方法。
特性 | ss命令 | netstat |
---|---|---|
执行速度 | 快10倍以上 | 较慢 |
内核数据获取 | 直接读取 | 遍历/proc目录 |
信息详细度 | 高 | 一般 |
ss [选项] [过滤条件]
参数 | 功能描述 |
---|---|
| 显示TCP连接 |
| 显示UDP连接 |
| 仅显示监听状态的端口 |
| 显示所有连接(含监听/非监听) |
| 禁用服务名称解析(显示端口号) |
| 显示关联进程信息 |
| 输出统计摘要 |
| 仅IPv4连接 |
| 仅IPv6连接 |
| 显示TCP定时器信息 |
ss -tunap
-t
:TCP协议-u
:UDP协议-n
:显示数字端口-a
:所有状态-p
:显示进程信息root@jeven01:~# ss -tunap
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
udp UNCONN 0 0 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=686,fd=13))
udp UNCONN 0 0 [fe80::be24:11ff:fe51:1ed4]%enp6s18:546 [::]:* users:(("systemd-network",pid=684,fd=20))
tcp LISTEN 0 4096 127.0.0.1:38059 0.0.0.0:* users:(("containerd",pid=728,fd=10))
tcp LISTEN 0 128 127.0.0.1:6010 0.0.0.0:* users:(("sshd",pid=5959,fd=7))
tcp LISTEN 0 4096 0.0.0.0:80 0.0.0.0:* users:(("haproxy",pid=940,fd=6))
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=875,fd=3))
tcp LISTEN 0 4096 0.0.0.0:8987 0.0.0.0:* users:(("docker-proxy",pid=6499,fd=4))
tcp LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=686,fd=14))
tcp LISTEN 0 4096 0.0.0.0:8080 0.0.0.0:* users:(("haproxy",pid=940,fd=7))
tcp LISTEN 0 4096 0.0.0.0:8000 0.0.0.0:* users:(("docker-proxy",pid=1721,fd=4))
tcp ESTAB 0 0 192.168.3.88:22 192.168.3.105:58051 users:(("sshd",pid=6070,fd=4))
tcp ESTAB 0 52 192.168.3.88:22 192.168.3.105:58050 users:(("sshd",pid=5959,fd=4))
tcp LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=875,fd=4))
tcp LISTEN 0 4096 [::]:8987 [::]:* users:(("docker-proxy",pid=6505,fd=4))
tcp LISTEN 0 128 [::1]:6010 [::]:* users:(("sshd",pid=5959,fd=5))
tcp LISTEN 0 4096 [::]:8000 [::]:* users:(("docker-proxy",pid=1728,fd=4))
查看SSH服务(22端口)连接情况:
root@jeven01:~# ss -tn src :22
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
ESTAB 0 0 192.168.3.88:22 192.168.3.105:58051
ESTAB 0 52 192.168.3.88:22 192.168.3.105:58050
ss -t -o state time-wait
root@jeven01:~# ss -s
Total: 208
TCP: 19 (estab 2, closed 5, orphaned 0, timewait 0)
Transport Total IP IPv6
RAW 1 0 1
UDP 2 1 1
TCP 14 10 4
INET 17 11 6
FRAG 0 0 0
ss state [state_name]
支持的状态类型:
示例:
root@jeven01:~# ss -t state established
Recv-Q Send-Q Local Address:Port Peer Address:Port Process
0 0 192.168.3.88:ssh 192.168.3.105:58051
0 52 192.168.3.88:ssh 192.168.3.105:58050 # 显示所有已建立的TCP连接
查看来自特定IP的连接:
root@jeven01:~# ss dst 192.168.3.105
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
tcp ESTAB 0 0 192.168.3.88:ssh 192.168.3.105:58051
tcp ESTAB 0 52 192.168.3.88:ssh 192.168.3.105:58050
ss -t sport gt :1024 # 源端口大于1024
root@jeven01:~# ss -antp | grep -v '127.0.0.1' | column -t
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 0.0.0.0:80 0.0.0.0:* users:(("haproxy",pid=940,fd=6))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=875,fd=3))
LISTEN 0 4096 0.0.0.0:8987 0.0.0.0:* users:(("docker-proxy",pid=6499,fd=4))
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=686,fd=14))
LISTEN 0 4096 0.0.0.0:8080 0.0.0.0:* users:(("haproxy",pid=940,fd=7))
LISTEN 0 4096 0.0.0.0:8000 0.0.0.0:* users:(("docker-proxy",pid=1721,fd=4))
ESTAB 0 0 192.168.3.88:22 192.168.3.105:58051 users:(("sshd",pid=6070,fd=4))
ESTAB 0 52 192.168.3.88:22 192.168.3.105:58050 users:(("sshd",pid=5959,fd=4))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=875,fd=4))
LISTEN 0 4096 [::]:8987 [::]:* users:(("docker-proxy",pid=6505,fd=4))
LISTEN 0 128 [::1]:6010 [::]:* users:(("sshd",pid=5959,fd=5))
LISTEN 0 4096 [::]:8000 [::]:* users:(("docker-proxy",pid=1728,fd=4))
ss -ltn | grep 'Send-Q'
ss -ap | grep 'python'
ss
,否则可能无法完整显示。-l
选项仅显示监听中的端口,而默认情况下会显示所有活动连接。-n
禁用解析以加快输出速度。-t
和 -u
分别用于查看TCP和UDP连接,切勿混淆两者。ss
输出可能非常庞大,建议结合 grep
或其他工具进行筛选。/proc
文件系统:-p
选项显示进程信息时,依赖于 /proc
文件系统,确保其已挂载。ss
版本可能存在功能差异,建议查阅本地手册页确认支持的选项。ss
可能同时显示IPv4和IPv6连接,使用 -4
或 -6
明确指定协议版本。-o
(计时器信息)或自定义列格式,以优化输出可读性。原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。