监视网络连接的ss命令

ss命令是一个用于在Linux系统上显示网络套接字相关信息的工具。这个工具显示比用于显示活动套接字连接的netstat命令更详细的信息。我们在这里专研并且看一下如何使用ss命令在Linux中显示变化的套接字连接信息。

1)列出所有连接

不带任何选项的基本ss命令只是列出所有连接,不管这些连接所在状态。

[blctrl@rockygu ~]$ ss
Netid   State   Recv-Q   Send-Q   Local Address:Port    Peer Address:Port   Process
...
tcp     ESTAB   0        48       172.28.133.248:ssh     61.87.239.94:54191
tcp     ESTAB   0        0        172.28.133.248:38194   100.100.30.26:http
tcp     ESTAB   0        0        172.28.133.248:ssh     61.87.239.94:54192

2) 列出监听和非监听端口

你可以使用-a选项获取一个监听和非监听端口的列表。

[root@rockygu blctrl]# ss -a
Netid State     Recv-Q Send-Q   Local Address:Port       Peer Address:Port     Process
...
tcp   LISTEN    0      128      127.0.0.1:x11-ssh-offset  0.0.0.0:*
tcp   LISTEN    0      128      0.0.0.0:sunrpc            0.0.0.0:*
tcp   LISTEN    0      128      0.0.0.0:ssh               0.0.0.0:*
tcp   ESTAB     0      64       172.28.133.248:ssh        61.87.239.94:54191
tcp   TIME-WAIT 0      0        172.28.133.248:55206      100.100.36.108:https
tcp   ESTAB     0      0        172.28.133.248:38194      100.100.30.26:http
tcp   ESTAB     0      0        172.28.133.248:ssh        61.87.239.94:54192
tcp   LISTEN    0      128      [::]:sunrpc               [::]:*
tcp   LISTEN    0      128      *:telnet                  *:*

3、列出监听套接字

要只显示监听套接字,使用-l标记。

[root@rockygu blctrl]# ss -l
Netid  State   Recv-Q  Send-Q    Local Address:Port     Peer Address:Port    Process
...
tcp    LISTEN  0       128       0.0.0.0:ssh            0.0.0.0:*
tcp    LISTEN  0       128       [::]:sunrpc            [::]:*
tcp    LISTEN  0       128       *:telnet                *:*

4、列出所有TCP连接

要显示所有TCP连接,使用-t选项。

[root@rockygu blctrl]# ss -t
State     Recv-Q     Send-Q    Local Address:Port    Peer Address:Port   Process
ESTAB     0          48        172.28.133.248:ssh    61.87.239.94:54191
ESTAB     0          0         172.28.133.248:38194  100.100.30.26:http
ESTAB     0          0         172.28.133.248:ssh    61.87.239.94:54192

5、列出所有监听的TCP连接

要查看所有监听的TCP套接字,使用-lt选项。

[root@localhost blctrl]# ss -lt
State      Recv-Q Send-Q     Local Address:Port       Peer Address:Port
LISTEN     0      100        127.0.0.1:smt             *:*
LISTEN     0      128        *:ssh                     *:*
LISTEN     0      100         [::1]:smtp               [::]:*
LISTEN     0      128         [::]:ssh                 [::]:*

6、列出所有UDP连接

要查看所有UDP套接字连接,使用-ua选项。

[root@localhost blctrl]# 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          [::]:*
、

7、列出所有监听的UDP连接

要查看监听的UDP连接,使用-lu选项。

[root@localhost blctrl]# ss -lu
State      Recv-Q Send-Q   Local Address:Port      Peer Address:Port
UNCONN     0      0            127.0.0.1:323             *:*
UNCONN     0      0                [::1]:323             [::]:*

8、显示套接字的PID(进程IDs)

要显示与套接字连接相关联的进程IDs,使用-p标记。

[root@localhost blctrl]# ss -p
Netid  State    Recv-Q Send-Q  Local Address:Port       Peer Address:Port
tcp    ESTAB      0      0       192.168.3.2:ssh          192.168.3.50:63561                                                                   users:(("sshd",pid=1580,fd=3),("sshd",pid=1556,fd=3))
tcp    ESTAB      0      0       192.168.3.2:ssh          192.168.3.50:60873                                                                   users:(("sshd",pid=1498,fd=3),("sshd",pid=1492,fd=3))

9、显示概要统计

要显示概要统计,使用-s选项。

[root@localhost blctrl]# ss -s
Total: 200 (kernel 240)
TCP:   8 (estab 4, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 0

Transport Total     IP        IPv6
*         240       -         -
RAW       1         0         1
UDP       2         1         1
TCP       8         6         2
INET      11        7         4
FRAG      0         0         0

10、显示IPv4和IPv6套接字连接

如果你只看IPv4套接字,使用-4选项。如果只看IPv6套接字,使用-6选项。

[root@localhost blctrl]# ss -4
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port
tcp    ESTAB      0      48      192.168.3.2:ssh                  192.168.3.50:63560
tcp    ESTAB      0      0       192.168.3.2:ssh                  192.168.3.50:60874
tcp    ESTAB      0      0       192.168.3.2:ssh                  192.168.3.50:63561
tcp    ESTAB      0      0       192.168.3.2:ssh                  192.168.3.50:60873
[root@localhost blctrl]# ss -6
Netid  State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port

11、通过端口号过滤连接

ss命令也让你过滤套接字端口号或地址号。例如,要显示一个ssh的目标或源端口的所有套接字连接,运行以下命令。

[root@localhost blctrl]# ss -at '( dport = :22 or sport = :22 )'
State      Recv-Q Send-Q   Local Address:Port                    Peer Address:Port
LISTEN     0      128                  *:ssh                                *:*
ESTAB      0      48         192.168.3.2:ssh                     192.168.3.50:63560
ESTAB      0      0          192.168.3.2:ssh                     192.168.3.50:60874
ESTAB      0      0          192.168.3.2:ssh                     192.168.3.50:63561
ESTAB      0      0          192.168.3.2:ssh                     192.168.3.50:60873
LISTEN     0      128               [::]:ssh                             [::]:*

作为替换,你可以运行以下命令:

[root@localhost blctrl]# ss -at '( dport = :ssh or sport = :ssh )'

12、检查ss命令的手册

要获取ss命令用法的更多详解,使用以下命令查看手册页。

[root@localhost blctrl]# man ss
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值