ss

发布时间: 更新时间: 总字数:1620 阅读时间:4m 作者: IP上海 分享 网址

在无数次使用过 netstat 命令后,终于有一天发现了ss命令。好用、强大呀。

ss 简介

ssSocket Statistics的缩写。ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容。但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信息,而且比netstat更快速更高效。

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

ss -help

[root@xiexianbin_cn ~]# ss -h
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
   -o, --options       show timer information
   -e, --extended      show detailed socket information
   -m, --memory        show socket memory usage
   -p, --processes	show process using socket
   -i, --info		show internal TCP information
   -s, --summary	show socket usage summary

   -4, --ipv4          display only IP version 4 sockets
   -6, --ipv6          display only IP version 6 sockets
   -0, --packet	display PACKET sockets
   -t, --tcp		display only TCP sockets
   -u, --udp		display only UDP sockets
   -d, --dccp		display only DCCP sockets
   -w, --raw		display only RAW sockets
   -x, --unix		display only Unix domain sockets
   -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
   -F, --filter=FILE   read filter information from FILE
       FILTER := [ state TCP-STATE ] [ EXPRESSION ]

常用命令:

ss -lpn
ss -ltn

netstat 与 ss

netstat命令是net-tools工具集中的一员:

[root@xiexianbin_cn ~]# rpm -qa | grep net-tools
net-tools-1.60-110.el6_2.x86_64

而ss命令是iproute工具集中的一员:

[root@xiexianbin_cn ~]# rpm -qf /usr/sbin/ss
iproute-2.6.32-45.el6.x86_64
[root@xiexianbin_cn ~]# rpm -qa iproute
iproute-2.6.32-45.el6.x86_64

如果你无法使用ss命令,那么可能是缺少了iproute,需要安装一下:

yum install iproute iproute-doc

从某种意义上说,iproute工具集几乎可以替代掉net-tools工具集,具体的替代方案是这样的:

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

使用

查看网络连接统计

[root@xiexianbin_cn ~]# ss -s
Total: 95 (kernel 102)
TCP:   9 (estab 2, closed 1, orphaned 0, synrecv 0, timewait 1/0), ports 7

Transport Total     IP        IPv6
*	  102       -         -
RAW	  0         0         0
UDP	  10        10        0
TCP	  8         8         0
INET	  18        18        0
FRAG	  0         0         0

在服务器产生大量sockets连接时,我们会使用这个命令在做宏观统计。

查看所有打开的网络端口

[root@xiexianbin_cn ~]# ss -l
State       Recv-Q Send-Q                                           Local Address:Port                                               Peer Address:Port
LISTEN      0      128                                                          *:50414                                                         *:*
LISTEN      0      128                                                          *:sunrpc                                                        *:*
LISTEN      0      5                                                121.42.140.63:http                                                          *:*
LISTEN      0      5                                                121.42.140.63:webcache                                                       *:*
LISTEN      0      5                                                192.168.122.1:domain                                                        *:*
LISTEN      0      128                                                          *:ssh                                                           *:*

使用-pl参数的话,则会列出具体的程序名称

[root@xiexianbin_cn ~]# ss -pl
State       Recv-Q Send-Q                                           Local Address:Port                                               Peer Address:Port
LISTEN      0      128                                                          *:50414                                                         *:*        users:(("rpc.statd",1273,8))
LISTEN      0      128                                                          *:sunrpc                                                        *:*        users:(("rpcbind",1249,8))
LISTEN      0      5                                                121.42.140.63:http                                                          *:*        users:(("jekyll",27567,5))
LISTEN      0      5                                                121.42.140.63:webcache                                                       *:*        users:(("jekyll",27508,5))
LISTEN      0      5                                                192.168.122.1:domain                                                        *:*        users:(("dnsmasq",1571,6))
LISTEN      0      128                                                          *:ssh                                                           *:*        users:(("sshd",1390,3))

查看所有的socket连接

[root@xiexianbin_cn ~]# ss -a
State       Recv-Q Send-Q                                                             Local Address:Port                                                                 Peer Address:Port
LISTEN      0      128                                                                            *:50414                                                                           *:*
LISTEN      0      128                                                                            *:sunrpc                                                                          *:*
LISTEN      0      5                                                                  121.42.140.63:http                                                                            *:*
LISTEN      0      5                                                                  121.42.140.63:webcache                                                                         *:*
LISTEN      0      5                                                                  192.168.122.1:domain                                                                          *:*
LISTEN      0      128                                                                            *:ssh                                                                             *:*
ESTAB       0      0                                                                  121.42.140.63:60264                                                             140.205.140.205:http
ESTAB       0      332                                                                121.42.140.63:ssh                                                                116.227.15.201:53478

查看TCP sockets,使用-ta选

[root@xiexianbin_cn ~]# ss -ta
State       Recv-Q Send-Q                                           Local Address:Port                                               Peer Address:Port
LISTEN      0      128                                                          *:50414                                                         *:*
LISTEN      0      128                                                          *:sunrpc                                                        *:*
LISTEN      0      5                                                121.42.140.63:http                                                          *:*
LISTEN      0      5                                                121.42.140.63:webcache                                                       *:*
LISTEN      0      5                                                192.168.122.1:domain                                                        *:*
LISTEN      0      128                                                          *:ssh                                                           *:*
ESTAB       0      0                                                121.42.140.63:60264                                           140.205.140.205:http
ESTAB       0      300                                              121.42.140.63:ssh                                              116.227.15.201:53478
TIME-WAIT   0      0                                                121.42.140.63:http                                              123.125.71.77:17852

查看UDP sockets,使用-ua选项

[root@xiexianbin_cn ~]# ss -ua
State       Recv-Q Send-Q                                           Local Address:Port                                               Peer Address:Port
UNCONN      0      0                                                            *:56733                                                         *:*
UNCONN      0      0                                                            *:vsinet                                                        *:*
UNCONN      0      0                                                192.168.122.1:domain                                                        *:*
UNCONN      0      0                                                            *:bootps                                                        *:*
UNCONN      0      0                                                            *:sunrpc                                                        *:*
UNCONN      0      0                                                192.168.122.1:ntp                                                           *:*
UNCONN      0      0                                                121.42.140.63:ntp                                                           *:*
UNCONN      0      0                                                    127.0.0.1:ntp                                                           *:*
UNCONN      0      0                                                            *:ntp                                                           *:*
UNCONN      0      0                                                    127.0.0.1:659                                                           *:*

查看RAW sockets,使用-wa选项

[root@xiexianbin_cn ~]# ss -wa
State       Recv-Q Send-Q                                           Local Address:Port                                               Peer Address:Port

查看UNIX sockets,使用-xa选项

[root@xiexianbin_cn ~]# ss -xa
Netid State      Recv-Q Send-Q                                         Local Address:Port                                             Peer Address:Port
u_str LISTEN     0      0                                       @/com/ubuntu/upstart 7885                                                        * 0
u_str LISTEN     0      0                               /var/run/openvswitch/db.sock 9977                                                        * 0
u_str LISTEN     0      0                  /var/run/openvswitch/ovsdb-server.931.ctl 9978                                                        * 0
u_str LISTEN     0      0                  /var/run/openvswitch/ovs-vswitchd.942.ctl 10020                                                       * 0
u_str ESTAB      0      0                                                          * 10021                                                       * 0
u_str ESTAB      0      0                               /var/run/openvswitch/db.sock 10023                                                       * 0
u_str LISTEN     0      0                                      /var/run/rpcbind.sock 10523                                                       * 0
u_str LISTEN     0      0                                       /var/run/nscd/socket 10779                                                       * 0
u_str LISTEN     0      0                               /tmp/qtsingleapp-aegiss-a5d2 12439                                                       * 0
u_str LISTEN     0      0            /var/tmp/alibabasecurityaegisdetectservice.root 12446                                                       * 0
u_str LISTEN     0      0      /tmp/Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)> 12449                                                       * 0
u_str LISTEN     0      0                               /tmp/qtsingleapp-aegisG-46d2 1663657                                                     * 0
u_str ESTAB      0      0                                                          * 1663663                                                     * 0
u_str ESTAB      0      0      /tmp/Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)> 1663664                                                     * 0
u_dgr ESTAB      0      0                                    @/org/kernel/udev/udevd 8400                                                        * 0
u_dgr ESTAB      0      0                                                          * 8413                                                        * 0
u_dgr ESTAB      0      0                                                          * 8414                                                        * 0
u_dgr ESTAB      0      0                                                   /dev/log 10426                                                       * 0
u_dgr ESTAB      0      0                                                          * 10530                                                       * 0
u_dgr ESTAB      0      0                                                          * 10604                                                       * 0
u_dgr ESTAB      0      0                                                          * 11037                                                       * 0
u_dgr ESTAB      0      0                                                          * 11196                                                       * 0
u_dgr ESTAB      0      0                                                          * 11258                                                       * 0
u_dgr ESTAB      0      0                                                          * 11523                                                       * 0
u_dgr ESTAB      0      0                                                          * 2640389                                                     * 0

查看某端口的进程,使用-选项

[root@xiexianbin_cn ~]# ss -pn | grep 35559
tcp    ESTAB      0      0      127.0.0.1:35559              10.3.13.61:5672                users:(("aodh-notifier: ",pid=4380,fd=10))

其他demo

ss  -uan
ss -ta    # 显示TCP连接
ss -s       # 显示 Sockets 摘要
ss -l       # 列出所有打开的网络连接端口
ss -pl      # 查看进程使用的socket
ss -lp | grep 3306  # 找出打开套接字/端口应用程序
ss -ua    # 显示所有UDP Sockets
ss -o state established '( dport = :smtp or sport = :smtp )' # 显示所有状态为established的SMTP连接
ss -o state established '( dport = :http or sport = :http )' # 显示所有状态为Established的HTTP连接
ss -o state fin-wait-1 '( sport = :http or sport = :https )' dst 193.233.7/24  # 列举出处于 FIN-WAIT-1状态的源端口为 80或者 443,目标网络为 193.233.7/24所有 tcp套接字
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数