ss命令是一个用于在Linux系统上显示网络套接字相关信息的工具。这个工具显示比用于显示活动套接字连接的netstat命令更详细的信息。我们在这里专研并且看一下如何使用ss命令在Linux中显示变化的套接字连接信息。
1)列出所有连接
不带任何选项的基本ss命令只是列出所有连接,不管这些连接所在状态。
[blctrl@rockygu ~]$ ss
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
...
tcp ESTAB 0 48 172.28.133.248:ssh 61.87.239.94:54191
tcp ESTAB 0 0 172.28.133.248:38194 100.100.30.26:http
tcp ESTAB 0 0 172.28.133.248:ssh 61.87.239.94:54192
2) 列出监听和非监听端口
你可以使用-a选项获取一个监听和非监听端口的列表。
[root@rockygu blctrl]# ss -a
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
...
tcp LISTEN 0 128 127.0.0.1:x11-ssh-offset 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:sunrpc 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:ssh 0.0.0.0:*
tcp ESTAB 0 64 172.28.133.248:ssh 61.87.239.94:54191
tcp TIME-WAIT 0 0 172.28.133.248:55206 100.100.36.108:https
tcp ESTAB 0 0 172.28.133.248:38194 100.100.30.26:http
tcp ESTAB 0 0 172.28.133.248:ssh 61.87.239.94:54192
tcp LISTEN 0 128 [::]:sunrpc [::]:*
tcp LISTEN 0 128 *:telnet *:*
3、列出监听套接字
要只显示监听套接字,使用-l标记。
[root@rockygu blctrl]# ss -l
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
...
tcp LISTEN 0 128 0.0.0.0:ssh 0.0.0.0:*
tcp LISTEN 0 128 [::]:sunrpc [::]:*
tcp LISTEN 0 128 *:telnet *:*
4、列出所有TCP连接
要显示所有TCP连接,使用-t选项。
[root@rockygu blctrl]# ss -t
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
ESTAB 0 48 172.28.133.248:ssh 61.87.239.94:54191
ESTAB 0 0 172.28.133.248:38194 100.100.30.26:http
ESTAB 0 0 172.28.133.248:ssh 61.87.239.94:54192
5、列出所有监听的TCP连接
要查看所有监听的TCP套接字,使用-lt选项。
[root@localhost blctrl]# ss -lt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:smt *:*
LISTEN 0 128 *:ssh *:*
LISTEN 0 100 [::1]:smtp [::]:*
LISTEN 0 128 [::]:ssh [::]:*
6、列出所有UDP连接
要查看所有UDP套接字连接,使用-ua选项。
[root@localhost blctrl]# ss -ua
State Recv-Q Send-Q Local Address:Port Peer Address:Port
UNCONN 0 0 127.0.0.1:323 *:*
UNCONN 0 0 [::1]:323 [::]:*
、
7、列出所有监听的UDP连接
要查看监听的UDP连接,使用-lu选项。
[root@localhost blctrl]# ss -lu
State Recv-Q Send-Q Local Address:Port Peer Address:Port
UNCONN 0 0 127.0.0.1:323 *:*
UNCONN 0 0 [::1]:323 [::]:*
8、显示套接字的PID(进程IDs)
要显示与套接字连接相关联的进程IDs,使用-p标记。
[root@localhost blctrl]# ss -p
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp ESTAB 0 0 192.168.3.2:ssh 192.168.3.50:63561 users:(("sshd",pid=1580,fd=3),("sshd",pid=1556,fd=3))
tcp ESTAB 0 0 192.168.3.2:ssh 192.168.3.50:60873 users:(("sshd",pid=1498,fd=3),("sshd",pid=1492,fd=3))
9、显示概要统计
要显示概要统计,使用-s选项。
[root@localhost blctrl]# ss -s
Total: 200 (kernel 240)
TCP: 8 (estab 4, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 0
Transport Total IP IPv6
* 240 - -
RAW 1 0 1
UDP 2 1 1
TCP 8 6 2
INET 11 7 4
FRAG 0 0 0
10、显示IPv4和IPv6套接字连接
如果你只看IPv4套接字,使用-4选项。如果只看IPv6套接字,使用-6选项。
[root@localhost blctrl]# ss -4
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp ESTAB 0 48 192.168.3.2:ssh 192.168.3.50:63560
tcp ESTAB 0 0 192.168.3.2:ssh 192.168.3.50:60874
tcp ESTAB 0 0 192.168.3.2:ssh 192.168.3.50:63561
tcp ESTAB 0 0 192.168.3.2:ssh 192.168.3.50:60873
[root@localhost blctrl]# ss -6
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
11、通过端口号过滤连接
ss命令也让你过滤套接字端口号或地址号。例如,要显示一个ssh的目标或源端口的所有套接字连接,运行以下命令。
[root@localhost blctrl]# ss -at '( dport = :22 or sport = :22 )'
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:ssh *:*
ESTAB 0 48 192.168.3.2:ssh 192.168.3.50:63560
ESTAB 0 0 192.168.3.2:ssh 192.168.3.50:60874
ESTAB 0 0 192.168.3.2:ssh 192.168.3.50:63561
ESTAB 0 0 192.168.3.2:ssh 192.168.3.50:60873
LISTEN 0 128 [::]:ssh [::]:*
作为替换,你可以运行以下命令:
[root@localhost blctrl]# ss -at '( dport = :ssh or sport = :ssh )'
12、检查ss命令的手册
要获取ss命令用法的更多详解,使用以下命令查看手册页。
[root@localhost blctrl]# man ss