ss命令的参数及使用详解


参数


 Usage: ss [ OPTIONS ]ss [ OPTIONS ] [ FILTER ]-h, --help           this message      #显示帮助菜单-V, --version        output version information      #输出版本信息-n, --numeric        don't resolve service names    #不解析服务名-r, --resolve       resolve host names   #解析主机名-a, --all            display all sockets     #显示所有的套接字-l, --listening      display listening sockets   #显示监听状态的socket-o, --options       show timer information   #显示计时器信息-e, --extended      show detailed socket information #展示详细的socket信息-m, --memory        show socket memory usage #展示socket的内存使用-p, --processes      show process using socket   #展示使用socket的进程-i, --info           show internal TCP information   #展示tcp内部信息-s, --summary        show socket usage summary   #展示socket使用汇总-4, --ipv4          display only IP version 4 sockets    #只显示ipv4的sockets-6, --ipv6          display only IP version 6 sockets    #只显示ipv6的sockets-0, --packet display PACKET sockets  #显示包经过的网络接口-t, --tcp            display only TCP sockets    #显示tcp套接字-u, --udp            display only UDP sockets    #显示udp套接字-d, --dccp           display only DCCP sockets   #显示dccp套接字-w, --raw            display only RAW sockets    #显示raw套接字-x, --unix           display only Unix domain sockets    #显示unix套接字-f, --family=FAMILY display sockets of type FAMILY   #显示指定类型的套接字-A, --query=QUERY, --socket=QUERY    #查看某种类型QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]-D, --diag=FILE      Dump raw information about TCP sockets to FILE  #将关于TCP套接字的原始信息转储到文件中-F, --filter=FILE   read filter information from FILE #使用此参数指定的过滤规则文件,过滤某种状态的连接FILTER := [ state TCP-STATE ] [ EXPRESSION ]

用法


1 . 常用ss命令

ss -l 显示本地打开的所有端口
ss -pl 显示每个进程具体打开的socket
ss -t -a 显示所有tcp socket
ss -u -a 显示所有的UDP Socekt
ss -o state established '( dport = :smtp or sport = :smtp )' 显示所有已建立的SMTP连接
ss -o state established '( dport = :http or sport = :http )' 显示所有已建立的HTTP连接
ss -x src /tmp/.X11-unix/* 找出所有连接X服务器的进程
ss -s 列出当前socket详细信息

2 . 显示sockets简要信息

列出当前已经连接,关闭,等待的tcp连接
[root@jia1-LinuxPerformance ~]# ss -s
Total: 122 (kernel 139)
TCP:   4 (estab 2, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 3Transport Total     IP        IPv6
*         139       -         -        
RAW       0         0         0        
UDP       6         6         0        
TCP       4         4         0        
INET      10        10        0        
FRAG      0         0         0  

3 . 列出当前监听端口

[root@jia1-LinuxPerformance ~]# ss -l
State      Recv-Q Send-Q                                           Local Address:Port                                               Peer Address:Port   
LISTEN     0      128                                                          *:ssh                                                           *:*       
LISTEN     0      100                                                  127.0.0.1:smtp                                                          *:*  

4 . ss列出其监听的端口以及对应进程

[root@jia1-LinuxPerformance ~]# ss -pl
State      Recv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port   
LISTEN     0      128                                                    *:ssh                                                     *:*        users:(("sshd",1821,3))
LISTEN     0      100                                            127.0.0.1:smtp                                                    *:*        users:(("master",1923,12))

5 . ss列所有的tcp sockets

[root@jia1-LinuxPerformance ~]# ss -at
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      196                                         172.16.2.106:ssh                                        124.65.173.246:50809   
ESTAB      0      0                                           172.16.2.106:60382                                     140.205.140.205:http 

6 . ss列出所有udp sockets

[root@jia1-LinuxPerformance ~]# ss -au
State      Recv-Q Send-Q                                     Local Address:Port                                         Peer Address:Port   
UNCONN     0      0                                                      *:bootpc                                                  *:*       
UNCONN     0      0                                                      *:bootpc                                                  *:*       
UNCONN     0      0                                           172.16.2.107:ntp                                                     *:*       
UNCONN     0      0                                           172.16.2.106:ntp                                                     *:*       
UNCONN     0      0                                              127.0.0.1:ntp                                                     *:*       
UNCONN     0      0                                                      *:ntp                                                     *:*    

7 . ss列出所有http连接中的连接

# ss -o state established '( dport = :http or sport = :http )'

8 . ss列出本地哪个进程连接到x server

# ss -x src /tmp/.X11-unix/*

9 . ss列出处在FIN-WAIT-1状态的http、https连接

# ss -o state fin-wait-1 '( sport = :http or sport = :https )'

ss常用的state状态:

1. established
2. syn-sent
3. syn-recv
4. fin-wait-1
5. fin-wait-2
6. time-wait
7. closed
8. close-wait
9. last-ack
10. listen
11. closing
12. all : All of the above states
13. connected : All the states except for listen and closed
14. synchronized : All the connected states except for syn-sent
15. bucket : Show states, which are maintained as minisockets, i.e. time-wait and syn-recv.
16. 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

10 . ss使用IP地址筛选

ss src ADDRESS_PATTERN
src:表示来源
ADDRESS_PATTERN:表示地址规则 
如下:
ss src 120.33.31.1 # 列出来自20.33.31.1的连接 #列出来自120.33.31.1,80端口的连接
ss src 120.33.31.1:http
ss src 120.33.31.1:80

11 . ss使用端口筛选

ss dport OP PORT
OP:是运算符
PORT:表示端口
dport:表示过滤目标端口、相反的有sport

OP运算符如下:

1. <= or le : Less than or equal to port
2. >= or ge : Greater than or equal to port
3. == or eq : Equal to port
4. != or ne : Not equal to port
5. < or gt : Less than to port
6. > or lt : Greater than to port
7. Note: le, gt, eq, ne etc. are use in unix shell and are accepted as well.

OP实例

ss sport = :http 也可以是 ss sport = :80
ss dport = :http
ss dport \> :1024
ss sport \> :1024
ss sport \< :32000
ss sport eq :22
ss dport != :22
ss state connected sport = :http
ss \( sport = :http or sport = :https \)
ss -o state fin-wait-1 \( sport = :http or sport = :https \) dst 192.168.1/24

ss与netstat对比


ss执行的时候消耗资源以及消耗的时间都比netstat少很多。
ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信息,而且比netstat更快速更高效。原因如下:


本文链接:https://my.lmcjl.com/post/1122.html

展开阅读全文

4 评论

留下您的评论.