ss命令详解
ss命令
ss是Socket Statistics的缩写。ss命令用来显示处于活动状态的套接字信息。它可以显示和netstat类似的内容。但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信息,而且比netstat更快速更高效。
语法格式
ss [参数]
常用参数
| 参数 | 含义 |
|---|---|
| -n | 不解析服务名称,以数字方式显示 |
| -a | 显示所有套接字 |
| -l | 显示处于监听状态的套接字 |
| -o | 显示计时器信息 |
| -e | 显示详细的套接字信息 |
| -m | 显示套接字的内存使用情况 |
| -p | 显示使用套接字的进程 |
| -i | 显示内部的TCP信息 |
| -s | 显示套接字使用概况 |
| -4 | 仅显示ipv4的套接字 |
| -6 | 仅显示ipv6的套接字 |
| -0 | 显示PACKET套接字 |
| -t | 只显示TCP套接字 |
| -u | 只显示UDP套接字 |
| -d | 只显示DCCP套接字 |
| -w | 只显示RAW套接字 |
| -x | 只显示 Unix套接字 |
| -D | 将原始TCP套接字信息转储到文件 |
使用示例
- 显示所有TCP套接字:
ss -ta
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:ssh *:*
LISTEN 0 100 127.0.0.1:smtp *:*
ESTAB 0 96 192.168.172.101:ssh 192.168.172.1:52859
LISTEN 0 128 :::ssh :::*
LISTEN 0 100 ::1:smtp :::*
- 显示所有UDP套接字:
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 :::*
- 显示套接字使用概况:
ss -s
Total: 562 (kernel 819)
TCP: 5 (estab 1, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 0
Transport Total IP IPv6
* 819 - -
RAW 0 0 0
UDP 2 1 1
TCP 5 3 2
INET 7 4 3
FRAG 0 0 0
- 查看主机监听的端口
ss -tln
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
可以看到,本机开启了22和25两个端口,也就是smtp服务25端口,ssh服务22端口。
- 通过-r选项解析IP和端口号
ss -tlr
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:ssh *:*
LISTEN 0 100 localhost:smtp *:*
LISTEN 0 128 :::ssh :::*
LISTEN 0 100 localhost:smtp :::*
- 通过-p选项查看监听端口的程序名称
ss -tlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:ssh *:* users:(("sshd",pid=958,fd=3))
LISTEN 0 100 127.0.0.1:smtp *:* users:(("master",pid=1083,fd=13))
LISTEN 0 128 :::ssh :::* users:(("sshd",pid=958,fd=4))
LISTEN 0 100 ::1:smtp :::* users:(("master",pid=1083,fd=14))
- 可以通过grep对监听端口进行进一步过滤
ss -tlp|grep ssh
LISTEN 0 128 *:ssh *:* users:(("sshd",pid=958,fd=3))
LISTEN 0 128 :::ssh :::* users:(("sshd",pid=958,fd=4))
- 查看建立的TCP连接
ss -tna
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
ESTAB 0 96 192.168.172.101:22 192.168.172.1:52859
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
ESTAB这一行表示,192.168.172.1:52859这个机器通过ssh连接到了192.168.172.101,这个机器使用的是默认的ssh端口。
- 显示所有已建立的SMTP连接
ss -o state established '( dport = :smtp or sport = :smtp )'
Netid Recv-Q Send-Q
- 显示所有已建立的HTTP连接
ss -o state established '( dport = :http or sport = :http )'
Netid Recv-Q Send-Q Local Address:Port Peer Address:Port
- 找出所有连接X服务器的进程
ss -x src /tmp/.X11-unix/*
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
- ss列出处在FIN-WAIT-1状态的http、https连接
ss -o state fin-wait-1 '( sport = :http or sport = :https )'
ss常用的state状态:
established
syn-sent
syn-recv
fin-wait-1
fin-wait-2
time-wait
closed
close-wait
last-ack
listen
closing
all : All of the above states
connected : All the states except for listen and closed
synchronized : All the connected states except for syn-sent
bucket : Show states, which are maintained as minisockets, i.e. time-wait and syn-recv.
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