说明

  ss命令是类似并将取代netstat的工具,它能用来查看网络状态信息,包括TCP、UDP连接、端口等信息。它的优点是能够显示更多详细的有关网络连接信息,而且比netstat更快速高效。
  如果系统中没有ss命令,就需要进行安装:yum install iproute

格式

  ss [option]

参数说明
  • -h:显示帮助信息;
  • -V:显示指令版本信息;
  • -n:不解析服务名称,以数字方式显示;
  • -a:显示所有的套接字;
  • -l:显示处于监听状态的套接字;
  • -o:显示计时器信息;
  • -m:显示套接字的内存使用情况;
  • -p:显示使用套接字的进程信息;
  • -i:显示内部的TCP信息;
  • -4:只显示ipv4的套接字;
  • -6:只显示ipv6的套接字;
  • -t:只显示tcp套接字;
  • -u:只显示udp套接字;
  • -d:只显示DCCP套接字;
  • -w:仅显示RAW套接字;
  • -x:仅显示UNIX域套接字。
示例

1、显示所有socket连接

[root@localhost ~]# ss -an
Netid State      Recv-Q Send-Q                                      Local Address:Port                                        Peer Address:Port 
....
tcp   LISTEN     0      128                                             127.0.0.1:9000                                                   *:*     
tcp   LISTEN     0      128                                                     *:27017                                                  *:*     
tcp   LISTEN     0      128                                                     *:80                                                     *:*     
tcp   LISTEN     0      128                                                     *:22                                                     *:*     
tcp   ESTAB      0      13948                                      192.168.71.108:22                                          192.168.71.1:57791 
tcp   LISTEN     0      80                                                     :::3306                                                  :::*     
tcp   LISTEN     0      128                                                    :::22                                                    :::*     

#上面的显示比较杂乱,下面通过column格式化一下
[root@localhost ~]# ss -an|column -t
Netid  State   Recv-Q  Send-Q  Local                                 Address:Port        Peer  Address:Port
nl     UNCONN  0       0       0:0                                   *
nl     UNCONN  0       0       0:860                                 *
nl     UNCONN  0       0       0:863                                 *
nl     UNCONN  0       0       0:4195164                             *
nl     UNCONN  4352    0       4:5047                                *
nl     UNCONN  768     0       4:0                                   *
...
raw    UNCONN  0       0       :::58                                 :::*
tcp    UNCONN  0       0       *:35416                               *:*
tcp    UNCONN  0       0       *:5353                                *:*
tcp    LISTEN  0       128     127.0.0.1:9000                        *:*
tcp    LISTEN  0       128     *:27017                               *:*
tcp    LISTEN  0       128     *:80                                  *:*
tcp    LISTEN  0       128     *:22                                  *:*
tcp    ESTAB   0       52      192.168.71.108:22                     192.168.71.1:57791
tcp    LISTEN  0       80      :::3306                               :::*
tcp    LISTEN  0       128     :::22                                 :::*

2、显示所有正在监听的TCP和UDP连接

[root@localhost ~]# ss -lntup|column -t
Netid  State   Recv-Q  Send-Q  Local           Address:Port  Peer                                                              Address:Port
tcp    UNCONN  0       0       *:35416         *:*           users:(("avahi-daemon",863,13))
tcp    UNCONN  0       0       *:5353          *:*           users:(("avahi-daemon",863,12))
tcp    LISTEN  0       128     127.0.0.1:9000  *:*           users:(("php-fpm",1460,0),("php-fpm",1459,0),("php-fpm",1449,8))
tcp    LISTEN  0       128     *:27017         *:*           users:(("mongod",1736,11))
tcp    LISTEN  0       128     *:80            *:*           users:(("nginx",1202,6),("nginx",1199,6))
tcp    LISTEN  0       128     *:22            *:*           users:(("sshd",1114,3))
tcp    LISTEN  0       80      :::3306         :::*          users:(("mysqld",3598,10))
tcp    LISTEN  0       128     :::22           :::*          users:(("sshd",1114,4))

3、显示socket统计

[root@localhost ~]# ss -s
Total: 369 (kernel 408)
TCP:   7 (estab 1, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 0

Transport Total     IP        IPv6
*      408       -         -        
RAW      1         0         1        
UDP      2         2         0        
TCP      7         5         2        
INET      10        7         3        
FRAG      0         0         0