【Linux】一步一步学Linux——ss命令(170)
00. 目錄
文章目錄
- 00. 目錄
- 01. 命令概述
- 02. 命令格式
- 03. 常用選項
- 04. 參考示例
- 05. 附錄
01. 命令概述
ss命令用來顯示處于活動狀態的套接字信息。ss命令可以用來獲取socket統計信息,它可以顯示和netstat類似的內容。但ss的優勢在于它能夠顯示更多更詳細的有關TCP和連接狀態的信息,而且比netstat更快速更高效。
當服務器的socket連接數量變得非常大時,無論是使用netstat命令還是直接cat /proc/net/tcp,執行速度都會很慢。可能你不會有切身的感受,但請相信我,當服務器維持的連接達到上萬個的時候,使用netstat等于浪費 生命,而用ss才是節省時間。
天下武功唯快不破。ss快的秘訣在于,它利用到了TCP協議棧中tcp_diag。tcp_diag是一個用于分析統計的模塊,可以獲得Linux 內核中第一手的信息,這就確保了ss的快捷高效。當然,如果你的系統中沒有tcp_diag,ss也可以正常運行,只是效率會變得稍慢。
ss是Socket Statistics的縮寫。
02. 命令格式
ss [選項] ss [選項] [過濾]03. 常用選項
-h, --help 幫助信息 -V, --version 程序版本信息 -n, --numeric 不解析服務名稱 -r, --resolve 解析主機名 -a, --all 顯示所有套接字(sockets) -l, --listening 顯示監聽狀態的套接字(sockets) -o, --options 顯示計時器信息 -e, --extended 顯示詳細的套接字(sockets)信息 -m, --memory 顯示套接字(socket)的內存使用情況 -p, --processes 顯示使用套接字(socket)的進程 -i, --info 顯示 TCP內部信息 -s, --summary 顯示套接字(socket)使用概況 -4, --ipv4 僅顯示IPv4的套接字(sockets) -6, --ipv6 僅顯示IPv6的套接字(sockets) -0, --packet 顯示 PACKET 套接字(socket) -t, --tcp 僅顯示 TCP套接字(sockets) -u, --udp 僅顯示 UCP套接字(sockets) -d, --dccp 僅顯示 DCCP套接字(sockets) -w, --raw 僅顯示 RAW套接字(sockets) -x, --unix 僅顯示 Unix套接字(sockets) -f, --family=FAMILY 顯示 FAMILY類型的套接字(sockets),FAMILY可選,支持 unix, inet, inet6, link, netlink-A, --query=QUERY, --socket=QUERYQUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]-D, --diag=FILE 將原始TCP套接字(sockets)信息轉儲到文件 -F, --filter=FILE 從文件中都去過濾器信息FILTER := [ state TCP-STATE ] [ EXPRESSION ]04. 參考示例
4.1 顯示TCP連接
[root@localhost ~]# ss -t -a State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 80 *:mysql *:* LISTEN 0 128 *:sunrpc *:* LISTEN 0 5 192.168.122.1:domain *:* LISTEN 0 128 *:ssh *:* LISTEN 0 128 127.0.0.1:ipp *:* ESTAB 0 0 172.16.0.76:ssh 172.16.0.51:50412 LISTEN 0 128 :::sunrpc :::* LISTEN 0 128 :::ssh :::* LISTEN 0 128 ::1:ipp :::* [root@localhost ~]#4.2 顯示套接字使用概況
[root@localhost ~]# ss -s Total: 1644 (kernel 2523) TCP: 10 (estab 1, closed 1, orphaned 0, synrecv 0, timewait 0/0), ports 0Transport Total IP IPv6 * 2523 - - RAW 1 0 1 UDP 11 8 3 TCP 9 6 3 INET 21 14 7 FRAG 0 0 0 [root@localhost ~]#4.3 列出所有打開的網絡連接端口
[root@localhost ~]# ss -l Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port nl UNCONN 0 0 rtnl:evolution-addre/3016 * nl UNCONN 0 0 rtnl:packagekitd/1658 * nl UNCONN 0 0 rtnl:1334 * nl UNCONN 0 0 rtnl:avahi-daemon/630 *4.4 顯示LISTEN狀態的進程信息
[root@localhost ~]# ss -lp Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port nl UNCONN 0 0 rtnl:evolution-addre/3016 * nl UNCONN 0 0 rtnl:packagekitd/1658 * nl UNCONN 0 0 rtnl:1334 * nl UNCONN 0 0 rtnl:avahi-daemon/630 *4.5 查看指定端口的信息
[root@localhost ~]# ss -lp | grep 80 tcp LISTEN 0 80 *:mysql *:* users:(("mysqld",pid=1546,fd=24)) [root@localhost ~]#4.6 查看所有UDP套接字
[root@localhost ~]# ss -u -a State Recv-Q Send-Q Local Address:Port Peer Address:Port UNCONN 0 0 *:mdns *:* UNCONN 0 0 127.0.0.1:323 *:* UNCONN 0 0 *:17815 *:* UNCONN 0 0 *:783 *:* UNCONN 0 0 192.168.122.1:domain *:* UNCONN 0 0 *%virbr0:bootps *:* UNCONN 0 0 *:bootpc *:* UNCONN 0 0 *:sunrpc *:* UNCONN 0 0 ::1:323 :::* UNCONN 0 0 :::783 :::* UNCONN 0 0 :::sunrpc :::* [root@localhost ~]#4.7 顯示所有狀態為established的SMTP連接
[root@localhost ~]# ss -o state established '( dport = :smtp or sport = :smtp )' Netid Recv-Q Send-Q Local Address:Port Peer Address:Port [root@localhost ~]#4.8 顯示所有狀態為Established的HTTP連接
[root@localhost ~]# ss -o state established '( dport = :http or sport = :http )' Netid Recv-Q Send-Q Local Address:Port Peer Address:Port [root@localhost ~]#4.9 列舉出處于 FIN-WAIT-1狀態的源端口為 80或者 443,目標網絡為 193.233.7/24所有 tcp套接字
[root@localhost ~]# ss -o state fin-wait-1 '( sport = :http or sport = :https )' dst 193.233.7/24 Netid Recv-Q Send-Q Local Address:Port Peer Address:Port [root@localhost ~]#4.10 查詢關閉狀態的套接字
[root@localhost ~]# ss -4 state closing Netid Recv-Q Send-Q Local Address:Port Peer Address:Port [root@localhost ~]#ss -4 state 狀態
ss -6 state 狀態
狀態可以是如下:
established
syn-sent
syn-recv
fin-wait-1
fin-wait-2
time-wait
closed
close-wait
last-ack
listen
closing
4.11 匹配目的IP和端口
[deng@localhost ~]$ ss dst 192.168.12.94 Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port [deng@localhost ~]$ ss dst 192.168.12.94:http Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port [deng@localhost ~]$ ss dst 192.168.12.94:1521 Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port4.12 匹配本地IP和端口
[deng@localhost ~]$ ss src 192.168.12.94 Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port tcp ESTAB 0 0 192.168.12.94:ssh 192.168.12.87:52486 [deng@localhost ~]$ ss src 192.168.12.94:http Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port [deng@localhost ~]$ ss src 192.168.12.94:1521 Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port4.13 端口比較
[deng@localhost ~]# ss sport = :http [deng@localhost ~]# ss dport = :http [deng@localhost ~]# ss dport \> :1024 [deng@localhost ~]# ss sport \> :1024 [deng@localhost ~]# ss sport \< :32000 [deng@localhost ~]# ss sport eq :22 [deng@localhost ~]# ss dport != :22 [deng@localhost ~]# ss state connected sport = :http [deng@localhost ~]# ss \( sport = :http or sport = :https \) [deng@localhost ~]# ss -o state fin-wait-1 \( sport = :http or sport = :https \) dst 192.168.1/24說明:
ss dport OP PORT ss sport OP PORTss dport OP PORT 遠程端口和一個數比較;ss sport OP PORT 本地端口和一個數比較。
OP 可以代表以下任意一個:
<= or le : 小于或等于端口號
>= or ge : 大于或等于端口號
== or eq : 等于端口號
!= or ne : 不等于端口號
< or gt : 小于端口號
> or lt : 大于端口號
4.14 ss和netstat比較
[deng@localhost ~]$ time ss real 0m0.020s user 0m0.005s sys 0m0.005s[deng@localhost ~]$ time netstat -at real 0m10.266s user 0m0.015s sys 0m0.032s說明:
用time 命令分別獲取通過netstat和ss命令獲取程序和概要占用資源所使用的時間。在服務器連接數比較多的時候,netstat的效率完全沒法和ss比。
4.15 顯示連接X服務器的進程
[deng@localhost ~]$ ss -x src /tmp/.X11-unix/X0 Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port [deng@localhost ~]$05. 附錄
參考:【Linux】一步一步學Linux系列教程匯總
總結
以上是生活随笔為你收集整理的【Linux】一步一步学Linux——ss命令(170)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux】一步一步学Linux——r
- 下一篇: 【Linux】一步一步学Linux——n