Linux网络状态工具ss,代替netstat

Linuxss大约 7048 字

ss是Socket Statistics的缩写,是iproute工具集中的一员。

天下武功唯快不破。ss快的秘诀在于,它利用到了TCP协议栈中tcp_diag。tcp_diag是一个用于分析统计的模块,可以获得Linux 内核中第一手的信息,这就确保了ss的快捷高效。当然,如果你的系统中没有tcp_diag,ss也可以正常运行,只是效率会变得稍慢。(但仍然比 netstat要快。)

查看是否安装

rpm -qf /usr/sbin/ss

或:

rpm -q iproute

输出:

iproute-2.6.32-45.el6.x86_64

安装

如果无法使用ss命令,需安装iproute,一般现在Linux系统默认安装了iproute,故可以直接使用ss

yum install iproute iproute-doc

iproute与net-tool对比

用途net-tooliproute2
地址和链路配置ifconfigip addr, ip link
路由表routeip route
邻居arpip neigh
VLANvconfigip link
隧道iptunnelip tunnel
组播ipmaddrip maddr
统计netstatss

ss命令

帮助文档

ss -h

获取ss版本号

ss -V

查看当前服务器的网络连接统计

ss -s

输出:

[root@root ~]$ ss -s
Total: 953 (kernel 1080)
TCP:   754 (estab 722, closed 4, orphaned 0, synrecv 0, timewait 0/0), ports 775

Transport Total     IP        IPv6
*     1080      -         -        
RAW   0         0         0        
UDP   21        21        0        
TCP   750       750       0        
INET      771       771       0        
FRAG      0         0         0        

查看所有打开的网络端口

  • 使用-l参数。
ss -l

输出:

[root@root ~]$ ss -l
State      Recv-Q Send-Q   Local Address:Port       Peer Address:Port   
LISTEN     0      128                  *:rquotad               *:*       
LISTEN     0      100                  *:8109                  *:*       
LISTEN     0      128                  *:sunrpc                *:*       
LISTEN     0      50                   *:20880                 *:*       
LISTEN     0      50                   *:20881                 *:*       
LISTEN     0      50                   *:20882                 *:*       
LISTEN     0      50                   *:20883                 *:*       
LISTEN     0      1            127.0.0.1:18005                 *:*       
LISTEN     0      50                   *:20885                 *:*       
LISTEN     0      128                  *:44565                 *:*       
LISTEN     0      128                  *:ssh                   *:*       
LISTEN     0      128          127.0.0.1:ipp                   *:*       
LISTEN     0      50                   *:16377                 *:*       
LISTEN     0      500                  *:8090                  *:*       
LISTEN     0      64                   *:64156                 *:*       
LISTEN     0      128                  *:59231                 *:*       
LISTEN     0      50                   *:24384                 *:*       
LISTEN     0      64                   *:2049                  *:*       
LISTEN     0      128                  *:9090                  *:*       
LISTEN     0      128                  *:35107                 *:*       
LISTEN     0      1            127.0.0.1:8006                  *:*       
LISTEN     0      500                  *:7080                  *:*       
LISTEN     0      100                  *:8009                  *:*       
LISTEN     0      50                   *:2346                  *:*       
LISTEN     0      900                  *:3306                  *:*       
  • 使用-pl参数,显示具体进程名称,类似:("nginx",1673,6)
ss -pl

输出:

[root@root ~]$ ss -pl
State      Recv-Q Send-Q   Local Address:Port       Peer Address:Port   
LISTEN     0      128                  *:rquotad               *:*       
LISTEN     0      100                  *:8109                  *:*        users:(("java",60344,55))
LISTEN     0      128                  *:sunrpc                *:*       
LISTEN     0      50                   *:20880                 *:*        users:(("java",60344,239))
LISTEN     0      50                   *:20881                 *:*        users:(("java",15677,140))
LISTEN     0      50                   *:20882                 *:*        users:(("java",17575,139))
LISTEN     0      50                   *:20883                 *:*        users:(("java",28128,117))
LISTEN     0      1            127.0.0.1:18005                 *:*        users:(("java",60484,77))
LISTEN     0      50                   *:20885                 *:*        users:(("java",60484,242))
LISTEN     0      128                  *:44565                 *:*       
LISTEN     0      128                  *:ssh                   *:*       
LISTEN     0      128          127.0.0.1:ipp                   *:*       
LISTEN     0      50                   *:16377                 *:*        users:(("java",60484,19))
LISTEN     0      500                  *:8090                  *:*        users:(("java",60484,54))
LISTEN     0      64                   *:64156                 *:*       
LISTEN     0      128                  *:59231                 *:*       
LISTEN     0      50                   *:24384                 *:*        users:(("java",60484,21))
LISTEN     0      64                   *:2049                  *:*       
LISTEN     0      128                  *:9090                  *:*       
LISTEN     0      128                  *:35107                 *:*       
LISTEN     0      1            127.0.0.1:8006                  *:*        users:(("java",60344,74))
LISTEN     0      500                  *:7080                  *:*        users:(("java",60344,50))
LISTEN     0      100                  *:8009                  *:*        users:(("java",60484,58))
LISTEN     0      50                   *:2346                  *:*        users:(("java",60484,20))
LISTEN     0      900                  *:3306                  *:*       
LISTEN     0      128                  *:https                 *:*        users:(("nginx",1673,6),("nginx",958,6))
LISTEN     0      128                 :::http                 :::*        users:(("nginx",1673,8),("nginx",958,8))

查看服务器上所有的socket连接

  • 查看所有socket连接
ss -a

输出:

State      Recv-Q Send-Q      Local Address:Port          Peer Address:Port       
LISTEN     0      1               127.0.0.1:18005                    *:*       
LISTEN     0      50                      *:20885                    *:*       
LISTEN     0      128                     *:44565                    *:*            
LISTEN     0      1               127.0.0.1:8006                     *:*       
LISTEN     0      500                     *:7080                     *:*          
LISTEN     0      900                     *:3306                     *:*      
ESTAB      0      0           10.95.197.161:28892         10.95.216.9:7005    
ESTAB      0      0           10.95.197.161:45596       10.95.216.116:7070    
ESTAB      0      0           10.95.197.161:22437        10.95.216.10:7000    
ESTAB      0      0           10.95.197.161:57115         10.95.216.9:7000    
ESTAB      0      0           10.95.197.161:22185        10.95.216.10:7000    
ESTAB      0      0           10.95.197.161:60830        10.95.216.10:7001  
CLOSE-WAIT 61     0           10.95.197.161:12398       123.103.23.214:ftp     
CLOSE-WAIT 61     0           10.95.197.161:54971       123.103.23.214:ftp     
CLOSE-WAIT 61     0           10.95.197.161:56481       123.103.23.214:ftp     
  • 只查看TCP sockets,tcp all
ss -ta
  • 只查看UDP sockets,udp all
ss -ua
  • 只查看RAW sockets,raw all
ss -wa
  • 只查看UNIX sockets,unix all
ss -xa
阅读 383 · 发布于 2019-09-04

————        END        ————

扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看换一批