赞
踩
ss
(Socket Statistics)Socket统计;ss命令可以获取socket统计信息。它的功能和netstat类似。
[root@localhost ~]# time netstat -at
real 0m0.038s
user 0m0.008s
sys 0m0.013s
[root@localhost ~]# time ss -at
real 0m0.006s
user 0m0.003s
sys 0m0.003s
从上面的执行效率可以清晰地看到,ss命令的执行速度是netstat的6倍(不同环境可能执行效率不一样),可以看到ss 命令的执行之快的优势,体现的尤为突出。也为我们的工作解决执行节省时间(即使当服务器效率较低时)。
ss
命令默认统计所有建立的连接(不包含监听的端口),包括 tcp, udp, and unix socket 三种类型的连接;
[root@example opt]# ss --help 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 # 解析主机名,把 IP 解释为域名,把端口号解释为协议名称 -a, --all display all sockets #显示所有Socket -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使用总数 -b, --bpf show bpf filter socket information -E, --events continually display sockets as they are destroyed -Z, --context display process SELinux security contexts #显示进程SELinux 安全山下文 -z, --contexts display process and socket SELinux security contexts #显示进程和Socket 的SELinux 安全山下文 -N, --net switch to the specified network namespace name -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 #显示packet 的Sockets -t, --tcp display only TCP sockets #显示TCP 协议 的Sockets -S, --sctp display only SCTP sockets #显示STCP 的Sockets -u, --udp display only UDP sockets #显示UDP 的Sockets -d, --dccp display only DCCP sockets -w, --raw display only RAW sockets -x, --unix display only Unix domain sockets --vsock display only vsock sockets -f, --family=FAMILY display sockets of type FAMILY FAMILY := {inet|inet6|link|unix|netlink|vsock|help} -K, --kill forcibly close sockets, display what was closed -H, --no-header Suppress header line -A, --query=QUERY, --socket=QUERY QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink|vsock_stream|vsock_dgram}[,QUERY] -D, --diag=FILE Dump raw information about TCP sockets to FILE -F, --filter=FILE read filter information from FILE FILTER := [ state STATE-FILTER ] [ EXPRESSION ] STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES} TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listen|closing} connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing} synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing} bucket := {syn-recv|time-wait} big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listen|closing}
[root@localhost ~]# ss -atn # -a:所有连接 -t:TCP 连接 -n: 不解析名称
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6010 *:*
LISTEN 0 128 127.0.0.1:6011 *:*
[root@localhost ~]# ss -s
Total: 653 (kernel 1354)
TCP: 16 (estab 2, closed 1, orphaned 0, synrecv 0, timewait 0/0), ports 0
Transport Total IP IPv6
* 1354 - -
RAW 0 0 0
UDP 8 6 2
TCP 15 9 6
INET 23 15 8
FRAG 0 0 0
[root@localhost ~]# ss -alt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:sunrpc *:*
LISTEN 0 5 192.168.122.1:domain *:*
LISTEN 0 128 *:ssh *:*
LISTEN 0 128 127.0.0.1:ipp *:*
LISTEN 0 100 127.0.0.1:smtp *:*
LISTEN 0 128 127.0.0.1:x11-ssh-offset *:*
LISTEN 0 128 :::sunrpc :::*
LISTEN 0 128 :::ssh :::*
LISTEN 0 128 ::1:ipp :::*
LISTEN 0 100 ::1:smtp :::*
LISTEN 0 128 ::1:x11-ssh-offset :::*
[root@localhost ~]# ss -altp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:sunrpc *:* users:(("rpcbind",pid=6174,fd=4),("systemd",pid=1,fd=47))
LISTEN 0 5 192.168.122.1:domain *:* users:(("dnsmasq",pid=7202,fd=6))
LISTEN 0 128 *:ssh *:* users:(("sshd",pid=6798,fd=3))
LISTEN 0 128 127.0.0.1:ipp *:* users:(("cupsd",pid=6796,fd=12))
LISTEN 0 100 127.0.0.1:smtp *:* users:(("master",pid=7145,fd=13))
LISTEN 0 128 127.0.0.1:x11-ssh-offset *:* users:(("sshd",pid=13154,fd=9))
[root@localhost ~]# ss -o state established '( dport = :http or sport = :http )'
Netid Recv-Q Send-Q Local Address:Port Peer Address:Port
注意:命令格式,字符之间需要有空格;可以是服务名称,或端口号,(如:http;80)
FILTER-NAME: | Description |
---|---|
established | 活跃状态 |
syn-sent | 发送 |
syn-recv | 接收 |
fin-wait-1 | FIN-WAIT-1状态 |
fin-wait-2 | FIN-WAIT-2状态 |
time-wait | 等待关闭 |
closed | 已关闭 |
close-wait | 关闭 |
last-ack | |
listen | 正在监听 |
closing | 正在关闭 |
all | 所有以上状态 |
connected | 除了listen and closed的所有状态 |
synchronized | 所有已连接的状态除了syn-sent |
bucket | 显示状态为maintained as minisockets,如:time-wait和syn-recv. |
big | 和bucket相反. |
[root@localhost ~]# ss -o state fin-wait-1 '( sport = :http or sport = :https )' dst 192.168.2/24
Netid Recv-Q Send-Q Local Address:Port Peer Address:Port
# -o, --options 显示时间信息
# -m, --memory 显示 socket 使用的内存
# -i, --info 显示更多 TCP 内部的信息
[root@localhost ~]# ss -iom state established '( sport = :ssh )'
Netid Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp 0 48 192.168.2.128:ssh 192.168.2.1:56434 timer:(on,238ms,0) # -o
skmem:(r0,rb369280,t0,tb87040,f1792,w2304,o0,bl0,d0) sack cubic wscale:8,7 rto:240 rtt:39.96/3.157 ato:52 mss:1460 rcvmss:1168 advmss:1460 cwnd:10 bytes_acked:76797 bytes_received:50548 segs_out:1111 segs_in:1874 send 2.9Mbps lastsnd:2 lastrcv:4 lastack:4 pacing_rate 5.8Mbps unacked:1 rcv_rtt:122063 rcv_space:29364 # -m
[root@localhost ~]# ss dst 192.168.5/24 # 查询源地址为192.168.5/24网段的socket 数据
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp ESTAB 0 48 192.168.5.128:ssh 192.168.5.1:56434
192.168.5.1:56434
$ ss dst 192.168.5.120:http # 查询远程地址为192.168.5.120,端口为80的Socket
$ ss dst 192.168.5.120:443
[root@localhost ~]# ss src 192.168.5/24 # 查询源地址为192.168.5/24网段的socket 数据
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp ESTAB 0 48 192.168.5.128:ssh
格式: ss [dport|sport] Option port
Option | 说明 |
---|---|
le(<=) | 小于等于 |
ge(>=) | 大于等于 |
lt(<) | 小于 |
gt(>) | 大于 |
eq(==) | 等于 |
ne(!=) | 不等于 |
# 查询远端端口号大于50的TCP的Socket
[root@localhost ~]# ss -t dport ge 50
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 48 192.168.5.128:ssh 192.168.5.1:56434
总结:
由于ss命令的性能出色且功能丰富,可以使用ss
命令替代 netsate
命令成为我们日常查看 socket 相关信息的利器。常见的ss命令使用方式,基本如上,更多的需我们结合自己的能力不断地在工作中总结。欢迎在留言区与我讨论。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。