ss命令可以用來(lái)獲取socket統(tǒng)計(jì)信息,它可以顯示和netstat類似的內(nèi)容。但ss
的優(yōu)勢(shì)在于它能夠顯示更多更詳細(xì)的有關(guān)TCP和連接狀態(tài)的信息,而且比netstat
更快速更高效。ss
是Socket Statistics的縮寫。
當(dāng)服務(wù)器的socket連接數(shù)量變得非常大時(shí),無(wú)論是使用netstat命令還是直接cat /proc/net/tcp
,執(zhí)行速度都會(huì)很慢。可能你不會(huì)有切身的感受,但請(qǐng)相信我,當(dāng)服務(wù)器維持的連接達(dá)到上萬(wàn)個(gè)的時(shí)候,使用netstat
等于浪費(fèi) 生命,而用ss才是節(jié)省時(shí)間。
天下武功唯快不破。ss
快的秘訣在于,它利用到了TCP協(xié)議棧中tcp_diag。tcp_diag是一個(gè)用于分析統(tǒng)計(jì)的模塊,可以獲得Linux 內(nèi)核中第一手的信息,這就確保了ss的快捷高效。當(dāng)然,如果你的系統(tǒng)中沒(méi)有tcp_diag,ss
也可以正常運(yùn)行,只是效率會(huì)變得稍慢。(但仍然比 netstat
要快。)
ss [參數(shù)]
ss [參數(shù)] [過(guò)濾]
ss(Socket Statistics的縮寫)命令可以用來(lái)獲取 socket統(tǒng)計(jì)信息,此命令輸出的結(jié)果類似于 netstat輸出的內(nèi)容,但它能顯示更多更詳細(xì)的 TCP連接狀態(tài)的信息,且比 netstat 更快速高效。它使用了 TCP協(xié)議棧中 tcp_diag
(是一個(gè)用于分析統(tǒng)計(jì)的模塊),能直接從獲得第一手內(nèi)核信息,這就使得 ss命令快捷高效。在沒(méi)有 tcp_diag
,ss
也可以正常運(yùn)行。
-h
, --help
幫助信息QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]
FILTER := [ state TCP-STATE ] [ EXPRESSION ]
命令:
ss -t -a
輸出:
[yiibai@localhost ~]$ ss -t -a
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:ssh *:*
LISTEN 0 100 127.0.0.1:smtp *:*
ESTAB 0 52 192.168.0.197:ssh 192.168.0.5:54835
LISTEN 0 128 :::ssh :::*
LISTEN 0 100 ::1:smtp :::*
[yiibai@localhost ~]$
命令:
ss -s
輸出:
[yiibai@localhost ~]$ ss -s
Total: 567 (kernel 0)
TCP: 5 (estab 1, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 0
Transport Total IP IPv6
* 0 - -
RAW 1 0 1
UDP 5 3 2
TCP 5 3 2
INET 11 6 5
FRAG 0 0 0
[yiibai@localhost ~]$
說(shuō)明:列出當(dāng)前的established, closed, orphaned and waiting TCP sockets
命令:
ss -l
輸出:
[yiibai@localhost ~]$ ss -l
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
nl UNCONN 0 0 rtnl:-1254096198 *
nl UNCONN 0 0 rtnl:kernel *
nl UNCONN 0 0 rtnl:-1254096198 *
nl UNCONN 4352 0 tcpdiag:ss/4750 *
nl UNCONN 768 0 tcpdiag:kernel *
nl UNCONN 0 0 xfrm:kernel *
nl UNCONN 0 0 selinux:su/4717 *
nl UNCONN 0 0 selinux:kernel *
nl UNCONN 0 0 selinux:systemd/1 *
nl UNCONN 0 0 selinux:dbus-daemon/648 *
nl UNCONN 0 0 selinux:su/4717 *
nl UNCONN 0 0 selinux:dbus-daemon/648 *
nl UNCONN 0 0 selinux:systemd/1 *
nl UNCONN 0 0 audit:auditd/625 *
nl UNCONN 0 0 audit:systemd/1 *
nl UNCONN 0 0 audit:kernel *
nl UNCONN 0 0 fiblookup:kernel *
nl UNCONN 0 0 connector:kernel *
nl UNCONN 0 0 nft:kernel *
nl UNCONN 0 0 uevent:-4117 *
nl UNCONN 0 0 uevent:systemd/1 *
nl UNCONN 0 0 uevent:-4120 *
nl UNCONN 0 0 uevent:-4107 *
nl UNCONN 0 0 uevent:systemd-udevd/498 *
nl UNCONN 0 0 uevent:-4119 *
nl UNCONN 0 0 uevent:systemd-logind/665 *
nl UNCONN 0 0 uevent:kernel *
nl UNCONN 0 0 uevent:NetworkManager/698 *
nl UNCONN 0 0 uevent:tuned/1002 *
nl UNCONN 0 0 uevent:-4118 *
nl UNCONN 0 0 uevent:tuned/1002 *
nl UNCONN 0 0 uevent:-4120 *
nl UNCONN 0 0 uevent:NetworkManager/698 *
nl UNCONN 0 0 uevent:-4119 *
nl UNCONN 0 0 uevent:-4118 *
nl UNCONN 0 0 uevent:-4117 *
nl UNCONN 0 0 uevent:systemd-logind/665 *
nl UNCONN 0 0 uevent:-4107 *
nl UNCONN 0 0 uevent:systemd/1 *
nl UNCONN 0 0 genl:kernel *
nl UNCONN 0 0 scsi-trans:kernel *
p_raw UNCONN 0 0 *:ens33 *
u_str LISTEN 0 128 /run/lvm/lvmpolld.socket 12805 * 0
u_seq LISTEN 0 128 /run/udev/control 12811 * 0
u_str LISTEN 0 128 /run/lvm/lvmetad.socket 12827 * 0
u_dgr UNCONN 0 0 /run/systemd/shutdownd 13100 * 0
u_str LISTEN 0 100 public/showq 19872 * 0
u_str LISTEN 0 100 private/rewrite 19842 * 0
u_str LISTEN 0 128 /var/run/dbus/system_bus_socket 15231 * 0
u_str LISTEN 0 100 private/bounce 19845 * 0
u_str LISTEN 0 100 private/defer 19848 * 0
u_str LISTEN 0 100 private/trace 19851 * 0
u_str LISTEN 0 100 private/verify 19854 * 0
u_str LISTEN 0 100 private/proxymap 19860 * 0
u_str LISTEN 0 100 private/proxywrite 19863 * 0
u_str LISTEN 0 100 private/smtp 19866 * 0
u_str LISTEN 0 100 private/relay 19869 * 0
u_str LISTEN 0 100 private/error 19875 * 0
u_str LISTEN 0 100 private/retry 19878 * 0
u_str LISTEN 0 100 private/discard 19881 * 0
u_str LISTEN 0 100 private/local 19884 * 0
u_str LISTEN 0 100 private/virtual 19887 * 0
u_str LISTEN 0 100 private/lmtp 19890 * 0
u_str LISTEN 0 100 private/anvil 19893 * 0
u_str LISTEN 0 100 private/scache 19896 * 0
u_str LISTEN 0 100 public/pickup 19822 * 0
u_str LISTEN 0 100 public/cleanup 19832 * 0
u_str LISTEN 0 100 public/qmgr 19835 * 0
u_str LISTEN 0 100 public/flush 19857 * 0
u_str LISTEN 0 10 /var/run/NetworkManager/private-dhcp 17836 * 0
u_dgr UNCONN 0 0 /run/systemd/notify 8385 * 0
u_dgr UNCONN 0 0 /run/systemd/cgroups-agent 8387 * 0
u_str LISTEN 0 100 private/tlsmgr 19839 * 0
u_str LISTEN 0 128 /run/systemd/journal/stdout 8395 * 0
u_dgr UNCONN 0 0 /run/systemd/journal/socket 8398 * 0
u_dgr UNCONN 0 0 /dev/log 8400 * 0
u_str LISTEN 0 128 /run/systemd/private 12794 * 0
u_dgr UNCONN 0 0 * 16501 * 8400
u_dgr UNCONN 0 0 * 80277 * 8400
u_dgr UNCONN 0 0 * 85014 * 8400
u_dgr UNCONN 0 0 * 16412 * 8400
u_dgr UNCONN 0 0 * 80112 * 8400
u_dgr UNCONN 0 0 * 85465 * 8400
u_dgr UNCONN 0 0 * 15509 * 8400
u_dgr UNCONN 0 0 * 82676 * 8400
u_dgr UNCONN 0 0 * 13285 * 8398
u_dgr UNCONN 0 0 * 19784 * 8400
u_dgr UNCONN 0 0 * 20011 * 8400
u_dgr UNCONN 0 0 * 16231 * 8400
u_dgr UNCONN 8 0 * 13816 * 13817
u_dgr UNCONN 0 0 * 16204 * 8400
u_dgr UNCONN 0 0 * 79889 * 8400
u_dgr UNCONN 0 0 * 16133 * 8398
u_dgr UNCONN 0 768 * 13817 * 13816
u_dgr UNCONN 0 0 * 13797 * 8398
u_dgr UNCONN 0 0 * 15143 * 8400
udp UNCONN 0 0 :::ipv6-icmp :::*
udp UNCONN 0 0 *:trnsprntproxy *:*
udp UNCONN 0 0 127.0.0.1:323 *:*
udp UNCONN 0 0 *:bootpc *:*
udp UNCONN 0 0 :::rnm :::*
udp UNCONN 0 0 ::1:323 :::*
tcp LISTEN 0 128 *:ssh *:*
tcp LISTEN 0 100 127.0.0.1:smtp *:*
tcp LISTEN 0 128 :::ssh :::*
tcp LISTEN 0 100 ::1:smtp :::*
[yiibai@localhost ~]$
命令:
ss -pl
輸出:
[yiibai@localhost ~]$ ss -pl
Netid State Recv-Q Send-Q Local Address:Port
nl UNCONN 0 0 rtnl:-1254096198
nl UNCONN 0 0 rtnl:kernel
nl UNCONN 0 0 rtnl:-1254096198
nl UNCONN 4352 0 tcpdiag:ss/4871
nl UNCONN 768 0 tcpdiag:kernel
nl UNCONN 0 0 xfrm:kernel
nl UNCONN 0 0 selinux:su/4717
nl UNCONN 0 0 selinux:kernel
nl UNCONN 0 0 selinux:systemd/1
nl UNCONN 0 0 selinux:dbus-daemon/648
nl UNCONN 0 0 selinux:su/4717
nl UNCONN 0 0 selinux:dbus-daemon/648
nl UNCONN 0 0 selinux:systemd/1
nl UNCONN 0 0 audit:auditd/625
nl UNCONN 0 0 audit:systemd/1
nl UNCONN 0 0 audit:kernel
nl UNCONN 0 0 fiblookup:kernel
nl UNCONN 0 0 connector:kernel
nl UNCONN 0 0 nft:kernel
nl UNCONN 0 0 uevent:-4117
nl UNCONN 0 0 uevent:systemd/1
nl UNCONN 0 0 uevent:-4120
nl UNCONN 0 0 uevent:-4107
nl UNCONN 0 0 uevent:-4119
nl UNCONN 0 0 uevent:systemd-logind/665
nl UNCONN 0 0 uevent:kernel
nl UNCONN 0 0 uevent:NetworkManager/698
nl UNCONN 0 0 uevent:tuned/1002
nl UNCONN 0 0 uevent:-4118
nl UNCONN 0 0 uevent:tuned/1002
nl UNCONN 0 0 uevent:-4120
nl UNCONN 0 0 uevent:NetworkManager/698
nl UNCONN 0 0 uevent:-4119
nl UNCONN 0 0 uevent:-4118
nl UNCONN 0 0 uevent:-4117
nl UNCONN 0 0 uevent:systemd-logind/665
nl UNCONN 0 0 uevent:-4107
nl UNCONN 0 0 uevent:systemd/1
nl UNCONN 0 0 genl:kernel
nl UNCONN 0 0 scsi-trans:kernel
p_raw UNCONN 0 0 *:ens33
u_str LISTEN 0 128 /run/lvm/lvmpolld.socket 12805
u_seq LISTEN 0 128 /run/udev/control 12811
u_str LISTEN 0 128 /run/lvm/lvmetad.socket 12827
u_dgr UNCONN 0 0 /run/systemd/shutdownd 13100
u_str LISTEN 0 100 public/showq 19872
u_str LISTEN 0 100 private/rewrite 19842
u_str LISTEN 0 128 /var/run/dbus/system_bus_socket 15231
u_str LISTEN 0 100 private/bounce 19845
u_str LISTEN 0 100 private/defer 19848
u_str LISTEN 0 100 private/trace 19851
u_str LISTEN 0 100 private/verify 19854
u_str LISTEN 0 100 private/proxymap 19860
u_str LISTEN 0 100 private/proxywrite 19863
u_str LISTEN 0 100 private/smtp 19866
u_str LISTEN 0 100 private/relay 19869
u_str LISTEN 0 100 private/error 19875
u_str LISTEN 0 100 private/retry 19878
u_str LISTEN 0 100 private/discard 19881
u_str LISTEN 0 100 private/local 19884
u_str LISTEN 0 100 private/virtual 19887
u_str LISTEN 0 100 private/lmtp 19890
u_str LISTEN 0 100 private/anvil 19893
u_str LISTEN 0 100 private/scache 19896
u_str LISTEN 0 100 public/pickup 19822
u_str LISTEN 0 100 public/cleanup 19832
u_str LISTEN 0 100 public/qmgr 19835
u_str LISTEN 0 100 public/flush 19857
u_str LISTEN 0 10 /var/run/NetworkManager/private-dhcp 17836
u_dgr UNCONN 0 0 /run/systemd/notify 8385
u_dgr UNCONN 0 0 /run/systemd/cgroups-agent 8387
u_str LISTEN 0 100 private/tlsmgr 19839
u_str LISTEN 0 128 /run/systemd/journal/stdout 8395
u_dgr UNCONN 0 0 /run/systemd/journal/socket 8398
u_dgr UNCONN 0 0 /dev/log 8400
u_str LISTEN 0 128 /run/systemd/private 12794
u_dgr UNCONN 0 0 * 16501
u_dgr UNCONN 0 0 * 80277
u_dgr UNCONN 0 0 * 85014
u_dgr UNCONN 0 0 * 16412
u_dgr UNCONN 0 0 * 80112
u_dgr UNCONN 0 0 * 85465
u_dgr UNCONN 0 0 * 15509
u_dgr UNCONN 0 0 * 82676
u_dgr UNCONN 0 0 * 13285
u_dgr UNCONN 0 0 * 19784
u_dgr UNCONN 0 0 * 20011
u_dgr UNCONN 0 0 * 16231
u_dgr UNCONN 0 0 * 13816
u_dgr UNCONN 0 0 * 16204
u_dgr UNCONN 0 0 * 79889
u_dgr UNCONN 0 0 * 16133
u_dgr UNCONN 0 0 * 13817
u_dgr UNCONN 0 0 * 13797
u_dgr UNCONN 0 0 * 15143
udp UNCONN 0 0 :::ipv6-icmp
udp UNCONN 0 0 *:trnsprntproxy
udp UNCONN 0 0 127.0.0.1:323
udp UNCONN 0 0 *:bootpc
udp UNCONN 0 0 :::rnm
udp UNCONN 0 0 ::1:323
tcp LISTEN 0 128 *:ssh
tcp LISTEN 0 100 127.0.0.1:smtp
tcp LISTEN 0 128 :::ssh
tcp LISTEN 0 100 ::1:smtp
[yiibai@localhost ~]$
命令:
ss -lp | grep 23
輸出:
[yiibai@localhost ~]$ ss -lp | grep 23
u_str LISTEN 0 128 /var/run/dbus/system_bus_socket 15231 * 0
u_dgr UNCONN 0 0 * 16231 * 8400
udp UNCONN 0 0 127.0.0.1:323 *:*
udp UNCONN 0 0 ::1:323 :::*
[yiibai@localhost ~]$
命令:
ss -u -a
輸出:
[yiibai@localhost ~]$ ss -u -a
State Recv-Q Send-Q Local Address:Port Peer Address:Port
UNCONN 0 0 *:trnsprntproxy *:*
UNCONN 0 0 127.0.0.1:323 *:*
UNCONN 0 0 *:bootpc *:*
UNCONN 0 0 :::rnm :::*
UNCONN 0 0 ::1:323 :::*
[yiibai@localhost ~]$
命令:
ss -o state established '( dport = :ssh or sport = :ssh )'
輸出:
[yiibai@localhost ~]$ ss -o state established '( dport = :ssh or sport = :ssh )'
Netid Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp 0 52 192.168.0.197:ssh 192.168.0.5:54835 timer:(on,241ms,0)
[yiibai@localhost ~]$
命令:
ss -o state established '( dport = :ssh or sport = :ssh )'
輸出:
[yiibai@localhost ~]$ ss -o state established '( dport = :ssh or sport = :ssh )'
Netid Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp 0 52 192.168.0.197:ssh 192.168.0.5:54835 timer:(on,248ms,0)
[yiibai@localhost ~]$
命令:
ss -o state fin-wait-1 ‘( sport = :http or sport = :http )’ dst 193.233.7/24
輸出:自已動(dòng)手測(cè)試一下吧、
命令:
ss -4 state FILTER-NAME-HERE
ss -6 state FILTER-NAME-HERE
輸出:
[yiibai@localhost ~]$ ss -4 state closing
Netid Recv-Q Send-Q Local Address:Port Peer Address:Port
[yiibai@localhost ~]$
說(shuō)明:FILTER-NAME-HERE
可以代表以下任何一個(gè):
established
syn-sent
syn-recv
fin-wait-1
fin-wait-2
time-wait
closed
close-wait
last-ack
listen
closing
all
: 所有以上狀態(tài)connected
: 除了listen and closed的所有狀態(tài)synchronized
:所有已連接的狀態(tài)除了syn-sentbucket
: 顯示狀態(tài)為maintained as minisockets,如:time-wait和syn-recv.big
: 和bucket
相反.命令:
ss dst 192.168.0.5
ss dst 192.168.0.113:http
ss dst 192.168.0.113:smtp
ss dst 192.168.0.113:443
輸出:
[yiibai@localhost ~]$ ss dst 192.168.0.5
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp ESTAB 0 52 192.168.0.197:ssh 192.168.0.5:54835
[yiibai@localhost ~]$
命令:
ss src 192.168.0.197
ss src 192.168.0.197:http
ss src 192.168.0.197:80
ss src 192.168.0.197:smtp
ss src 192.168.0.197:25
輸出:
命令:
ss dport OP PORT
ss sport OP PORT
輸出:
[root@localhost ~]# ss sport = :http
[root@localhost ~]# ss dport = :http
[root@localhost ~]# ss dport \> :1024
[root@localhost ~]# ss sport \> :1024
[root@localhost ~]# ss sport \< :32000
[root@localhost ~]# ss sport eq :22
[root@localhost ~]# ss dport != :22
[root@localhost ~]# ss state connected sport = :http
[root@localhost ~]# ss \( sport = :http or sport = :http \)
[root@localhost ~]# ss -o state fin-wait-1 \( sport = :http or sport = :http \) dst 192.168.0/24
ss dport OP PORT
遠(yuǎn)程端口和一個(gè)數(shù)比較;ss sport OP PORT
本地端口和一個(gè)數(shù)比較。
OP 可以代表以下任意一個(gè):
<=
或 le
: 小于或等于端口號(hào)>=
或 ge
: 大于或等于端口號(hào)==
或 eq
: 等于端口號(hào)!=
或 ne
: 不等于端口號(hào)<
或 gt
: 小于端口號(hào)>
或 lt
: 大于端口號(hào)命令:
time netstat -at
time ss
輸出:
[yiibai@localhost ~]$ time netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN
tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN
tcp 0 52 localhost.localdoma:ssh 192.168.0.5:54835 ESTABLISHED
tcp6 0 0 [::]:http [::]:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 localhost:smtp [::]:* LISTEN
real 0m0.119s
user 0m0.006s
sys 0m0.021s
[yiibai@localhost ~]$ time ss
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
u_str ESTAB 0 0 * 19870 * 19871
u_str ESTAB