0
点赞
收藏
分享

微信扫一扫

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


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 [选项] [过滤]

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 ]

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 ~]#

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 ~]#

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 *

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 *

4.5 查看指定端口的信息

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

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 ~]#

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 ~]#

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 ~]#

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 ~]#

4.10 查询关闭状态的套接字

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

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

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

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

说明:

ss dport OP PORT 
ss sport OP PORT

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

说明:

用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 ~]$

05. 附录

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



举报

相关推荐

0 条评论