Linux查看网络连接情况ss,Linux网络状态工具netstat和ss使用详解
Netstat簡介
Netstat可用于列出系統所有的網絡套接字連接信息,包括處于監聽狀態的套接字信息。
語法
netstat [--tcp|-t] [--udp|-u] [--raw|-w] [--
listening|-l] [--all|-a] [--numeric|-n] [--extend|-e[--
extend|-e]] [--program|-p]
常用選項
-t: tcp協議相關;
-u: udp協議相關;
-w: raw socket ,原始套接字是一個特殊的套接字類型,它的創建方式跟TCP/UDP創建方法幾乎是一摸一樣;
-l: 處于監聽狀態;
-a: 所有狀態;
-n: 以數字顯示IP和端口,禁止DNS反向查詢;
-e:擴展格式;
-p: 顯示相關進程及PID;
-s: 列出所有網絡協議的統計信息;
-r:列出路由信息;
-i:列出網絡接口信息;
-e:顯示附加信息,配合其他選項使用;
-c:持續顯示信息,每秒刷新一次,配合其他選項使用;
-g:顯示IPv4和IPv6的多播組成員關系信息。
常用組合
顯示所有的tcp連接,并以數字的方式顯示。
[root@CentOS7.3 ~]#netstat -tan
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.1xx.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 52 192.168.1xx.130:22 192.168.1xx.1:56747 ESTABLISHED
tcp 0 0 192.168.1xx.130:22 192.168.1xx.1:56909 ESTABLISHED
tcp 0 0 192.168.1xx.130:22 192.168.1xx.1:52053 ESTABLISHED
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
顯示所有的udp連接信息。
[root@CentOS7.3 ~]#netstat -uan
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 0.0.0.0:5353 0.0.0.0:*
udp 0 0 0.0.0.0:25506 0.0.0.0:*
udp 0 0 192.168.122.1:53 0.0.0.0:*
udp 0 0 0.0.0.0:67 0.0.0.0:*
udp 0 0 0.0.0.0:68 0.0.0.0:*
udp 0 0 0.0.0.0:53379 0.0.0.0:*
udp6 0 0 :::23086 :::*
只顯示處于監聽狀態的tcp連接。
[root@CentOS7.3 ~]#netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
只顯示處于監聽狀態的udp連接。
[root@CentOS7.3 ~]#netstat -unl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 0.0.0.0:5353 0.0.0.0:*
udp 0 0 0.0.0.0:25506 0.0.0.0:*
udp 0 0 192.168.122.1:53 0.0.0.0:*
udp 0 0 0.0.0.0:67 0.0.0.0:*
udp 0 0 0.0.0.0:68 0.0.0.0:*
udp 0 0 0.0.0.0:53379 0.0.0.0:*
udp6 0 0 :::23086 :::*
列出所有tcp連接的信息,以擴展格式顯示。
[root@CentOS7.3 ~]#netstat -tnle
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 0 15472
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 0 22226
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 21045
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 0 21400
tcp6 0 0 :::111 :::* LISTEN 0 15471
tcp6 0 0 :::22 :::* LISTEN 0 21054
tcp6 0 0 ::1:631 :::* LISTEN 0 21399
列出所有tcp連接的信息,并顯示其相關的進行和PID(進程號)。
[root@CentOS7.3 ~]#netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 2136/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1200/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 946/cupsd
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 1200/sshd
tcp6 0 0 ::1:631 :::* LISTEN 946/cupsd
持續顯示所有的tcp連接。
[root@CentOS7.3 ~]#netstat -tanc
顯示路由表和使用route -n 列出的信息相同.
[root@CentOS7.3 ~]#netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.166.1 0.0.0.0 UG 0 0 0 eth0
0.0.0.0 172.17.0.1 0.0.0.0 UG 0 0 0 eth1
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
192.168.166.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
ss簡介
ss是Socker Statistics的縮寫,是一款非常適用、快速、跟蹤顯示的網絡套接字的新工具。它和netstat顯示的內容類似,但它比netstat更加強大。當服務器的socket連接數量變得非常大時,無論是使用netstat命令還是直接cat /proc/net/tcp,執行速度都會很慢。而用ss可以快速、有效的執行并得到結果。ss利用到了TCP協議棧中tcp_diag。tcp_diag是一個用于分析統計的模塊,可以獲得Linux 內核中第一手的信息,這就確保了ss的快捷高效。當然,如果你的系統中沒有tcp_diag,ss也可以正常運行,只是效率會變得稍慢。
語法格式
ss [OPTION]... [FILTER]
常用選項
-t: tcp協議相關;
-u: udp協議相關;
-w: 裸套接字相關;
-x: unix sock相關;
-l: listen狀態的連接;
-a: 顯示所有sockets信息;
-n: 數字格式;
-p: 相關的程序及PID;
-e: 擴展的信息;
-m:內存用量;
-o:計時器信息;
-s:顯示當前sockets的統計信息的摘要;
-i:顯示系統內部tcp連接;
-r:解析主機名;
-4:僅顯示IPv4的sockets連接;
-6:僅顯示IPv6的sockets連接;
常用選項示例
[root@CentOS7.3 ~]#ss -an #列出所有的sockets連接。
[root@CentOS7.3 ~]#ss -tnl #列出和tcp相關的sockets連接。
[root@CentOS7.3 ~]#ss -unl #列出和udp相關的sockets連接。
匹配過濾本機ip地址和端口
[root@centos7.3 ~]#ss src :22 #匹配本機端口為22的連接
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp ESTAB 0 0 192.168.xxx.xxx:ssh 192.168.166.1:63892
tcp ESTAB 0 52 192.168.xxx.xxx:ssh 192.168.166.1:63076
[root@centos7.3 ~]#ss src :ssh #匹配所有ssh協議的連接
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp ESTAB 0 0 192.168.166.137:ssh 192.168.166.1:63892
tcp ESTAB 0 52 192.168.166.137:ssh 192.168.166.1:63076
[root@centos7.4-1 ~]#ss src 192.168.1.2:ssh #匹配單個IP地址的ssh協議連接
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp ESTAB 0 0 192.168.166.137:ssh 192.168.166.1:63892
tcp ESTAB 0 52 192.168.166.137:ssh 192.168.166.1:63076
匹配過濾遠程ip地址和端口
[root@centos7.4-1 ~]#ss dst 119.75.213.61 #匹配單個遠程IP的所有連接
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp ESTAB 0 0 192.168.0.25:59484 119.75.213.61:http
[root@centos7.4-1 ~]#ss dst 119.75.213.61:80 #只匹配單個IP地址的80端口
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp ESTAB 0 0 192.168.0.25:59484 119.75.213.61:http
將本地或者遠程端口和一個數比較
[root@centos7.3 ~]# ss sport = :http
[root@centos7.3 ~]# ss dport = :http
[root@centos7.3 ~]# ss dport \> :1024
[root@centos7.3 ~]# ss sport \> :1024
[root@centos7.3 ~]# ss sport \< :32000
[root@centos7.3 ~]# ss sport eq :22
[root@centos7.3 ~]# ss dport != :22
使用state 過濾sockets信息
顯示所有狀態為established的http連接
[root@CentOS7.3 ~]#ss -o state established '( dport = :smtp or sport = :http )'
Netid Recv-Q Send-Q Local Address:Port Peer Address:Port
顯示處于 FIN-WAIT-1狀態的源端口為 80或者 443,目標網絡為 192.168.1/24所有 tcp套接字
ss -o state fin-wait-1 '( sport = :http or sport = :https )' dst 192.168.1/24
使用tcp連接的狀態進行過濾
ss -4 state FILTER-NAME-HERE
ss -6 state FILTER-NAME-HERE
FILTER-NAME-HERE 可用狀態:
established
syn-sent
syn-recv
fin-wait-1
fin-wait-2
time-wait
closed
close-wait
last-ack
closing
all #所有以上狀態。
connected #除了listen and closed的所有狀態。
synchronized #所有已連接的狀態除了syn-sent。
bucket #顯示狀態為maintained as minisockets,如:time-wait和syn-recv。
big #和bucket相反。
[root@CentOS7.3 ~]#ss -4 state closed
Netid Recv-Q Send-Q Local Address:Port Peer Address:Port
udp 0 0 *:mdns *:*
udp 0 0 *:25506 *:*
udp 0 0 192.168.xxx.1:domain *:*
udp 0 0 *%virbr0:bootps *:*
udp 0 0 *:bootpc *:*
udp 0 0 *:53379 *:*
總結
以上是生活随笔為你收集整理的Linux查看网络连接情况ss,Linux网络状态工具netstat和ss使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt Quick 和qml介绍
- 下一篇: Linux shell 的ss