linux(三十五)网络状态和端口监控 netstat

2023年 10月 11日 77.0k 0

Linux netstat 命令用于显示网络状态。

利用 netstat 指令可让你得知整个 Linux 系统的网络情况。

(1)安装

在centos7中此命令是未安装的,安装命令:

yum install -y net-tools

(2)基本语法

netstat-anp | grep进程号

(功能描述:查看该进程网络信息)

netstat-nlp | grep端口号

(功能描述:查看网络端口号占用情况)

 

(3)选项说明

-a:显示所有正在监听(listen)和未监听的套接字(socket).'

-n:拒绝显示别名,能显示数字的全部转化成数字

-p:表示显示哪个进程在调用

-t:–tcp:仅显示 TCP 相关。

-u:–udp:仅显示 UDP 相关。

–l:仅显示在监听(Listening)的 socket 服务。

 

实例:

1:显示所有 socket 连接:

root@iZijvdp1z0m5q4Z:~# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State     
tcp        0      0 localhost:domain        0.0.0.0:*               LISTEN    
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN    
tcp6       0      0 [::]:33060              [::]:*                  LISTEN    
tcp6       0      0 [::]:mysql              [::]:*                  LISTEN
udp        0      0 localhost:domain        0.0.0.0:*                         
udp        0      0 iZijvdp1z0m5q4Z:bootpc  0.0.0.0:* 
udp6       0      0 localhost:323           [::]:*                            
raw6       0      0 [::]:ipv6-icmp          [::]:*                  7         
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    3876299  /run/user/0/systemd/notify
unix  2      [ ACC ]     SEQPACKET  LISTENING     1515     /run/udev/control

 

这部分包括所有的使用端口,监听未监听的都有,一般很少使用这个命令来查看。

 

2:查看所有路由表信息

root@iZijvdp1z0m5q4Z:~# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         _gateway        0.0.0.0         UG        0 0          0 eth0
172.17.80.0     0.0.0.0         255.255.240.0   U         0 0          0 eth0
_gateway        0.0.0.0         255.255.255.255 UH        0 0          0 eth0

 

3:查看所有tcp相关端口

root@iZijvdp1z0m5q4Z:~# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State     
tcp        0      0 localhost:domain        0.0.0.0:*               LISTEN    
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN    
tcp6       0      0 localhost:6379          [::]:*                  LISTEN    
tcp6       0      0 [::]:http               [::]:*                  LISTEN

 

4:查看所有udp相关端口

root@iZijvdp1z0m5q4Z:~# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State     
udp        0      0 localhost:domain        0.0.0.0:*                          
udp        0      0 iZijvdp1z0m5q4Z:bootpc  0.0.0.0:*                         
udp        0      0 localhost:323           0.0.0.0:*                         
udp6       0      0 localhost:323           [::]:*

 

5:查看所有监听端口情况

root@iZijvdp1z0m5q4Z:~# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      488/systemd-resolve
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      701/sshd           
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      4296/sshd: root@pts
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      751/nginx: master p
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      519/php-fpm: master
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      694/redis-server 12
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      751/nginx: master p
tcp6       0      0 :::33060                :::*                    LISTEN      693/mysqld         
tcp6       0      0 :::3306                 :::*                    LISTEN      693/mysqld         
tcp6       0      0 ::1:6379                :::*                    LISTEN      694/redis-server 12
tcp6       0      0 :::80                   :::*                    LISTEN      751/nginx: master p

 

6:实时查看监听端口的情况,每两秒刷新一次:

watch netstat -ntpl
Every 2.0s: netstat -ntpl                                                                                                                                                                                                               Wed Dec 29 23:59:20 2021
 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name

 

以上大概就是netstat命令相关的使用。

 

有好的建议,请在下方输入你的评论。

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论