Linux网络状态工具netstat和ss使用详解

2018-01-17  本文已影响0人  guo的学习笔记

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                                  *:*  
上一篇下一篇

猜你喜欢

热点阅读