Linux netstat 命令

1. netstat 介绍

Netstat 是一个监控 TCP/IP 网络的工具。
它可以显示路由表、实际的网络连接以及每一个网络接口设备的状态信息。
具体如:显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况。

2. 常见参数

-n (not) 输出中不显示主机,端口和用户名,以网络IP地址代替名称
-l (listen) 仅列出有在 Listen (监听) 的服務状态
-p (PID) 显示建立相关链接的程序名

-a (all)显示所有选项,默认不显示LISTEN相关
-t (tcp)仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-r (route) 显示路由信息,路由表
-e (extra) 显示扩展信息,例如uid等
-s (static) 按各个协议进行统计
-c 每隔一个固定时间,执行该netstat命令。

提示:LISTEN和LISTENING的状态只有用-a或者-l才能看到

3. netstat 常用命令

3.0 netstat //查看所有信息

[root@glusterfs ftp-user2]# netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:18080         localhost:33534         TIME_WAIT  
tcp        0      0 localhost:18080         localhost:33464         TIME_WAIT  
tcp        0      0 localhost:18080         localhost:33532         TIME_WAIT  
tcp        0      0 glusterfs.green.m:36878 glusterfs.green.ma:2379 ESTABLISHED
tcp        0      0 glusterfs.green.m:36870 glusterfs.green.ma:2379 ESTABLISHED
tcp        0      0 glusterfs.green.m:36818 glusterfs.green.ma:2379 ESTABLISHED
tcp        0      0 glusterfs.green.m:24007 glusterfs.green.m:49145 ESTABLISHED
tcp        0      0 glusterfs.green.m:36812 glusterfs.green.ma:2379 ESTABLISHED
tcp        0      0 glusterfs.green.m:49139 glusterfs.green.m:49152 ESTABLISHED
tcp        0      0 dlp:44330               10.255.0.49:vcom-tunnel TIME_WAIT  
tcp        0      0 glusterfs.green.m:36934 glusterfs.green.ma:2379 ESTABLISHED
tcp        0      0 dlp:44398               10.255.0.49:vcom-tunnel TIME_WAIT  
…… ……
…… ……
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path                                                                     

unix  3      [ ]         DGRAM                    17433    /run/systemd/notify
unix  2      [ ]         DGRAM                    17435    /run/systemd/cgroups-agent
unix  7      [ ]         DGRAM                    17456    /run/systemd/journal/socket
unix  41     [ ]         DGRAM                    17458    /dev/log
unix  2      [ ]         DGRAM                    564      /run/systemd/shutdownd
unix  3      [ ]         STREAM     CONNECTED     42771    @/tmp/dbus-YybD3o6jL9
unix  3      [ ]         STREAM     CONNECTED     43352    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     27022    @/tmp/dbus-ZEdat5lyZJ     
…… ……
…… ……

说明:
netstat 的输出结果可以分为两个部分:

  1. 一个是Active Internet connections
    称为有源TCP连接,其中"Recv-Q"和"Send-Q"指%0A的是接收队列和发送队列。这些数字一般都应该是0。

  2. 一个是Active UNIX domain sockets,称为有源Unix域套接口
    Proto显示连接使用的协议,RefCnt表示连接到本套接口上的进程号,Types显示套接口的类型,State显示套接口当前的状态,Path表示连接到套接口的其它进程使用的路径名。

3.1 netstat -a //列出所有端口 (包括监听和未监听的)

[root@glusterfs ftp-user2]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:18080           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:49152           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:cbt             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:24007           0.0.0.0:*               LISTEN     
tcp        0      0 localhost:10248         0.0.0.0:*               LISTEN     

3.2 netstat -at //列出所有 tcp 端口

[root@glusterfs ftp-user2]# 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:18080           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:49152           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:cbt             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:24007           0.0.0.0:*               LISTEN  

3.3 netstat -au //列出所有 udp 端口

root@glusterfs ftp-user2]# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
udp        0      0 dlp:domain              0.0.0.0:*                          
udp        0      0 0.0.0.0:bootps          0.0.0.0:*                          
udp        0      0 0.0.0.0:sunrpc          0.0.0.0:*                          
udp        0      0 dlp:ntp                 0.0.0.0:*                          
udp        0      0 dlp:ntp                 0.0.0.0:*    

3.4 netstat -l //只显示监听端口

[root@glusterfs ftp-user2]# netstat -l 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:18080           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:49152           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:cbt             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:24007           0.0.0.0:*               LISTEN     
tcp        0      0 localhost:10248         0.0.0.0:*               LIST

3.5 netstat -lt //只列出所有监听 tcp 端口

[root@glusterfs ftp-user2]# netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:18080           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:49152           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:cbt             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:24007           0.0.0.0:*               LISTEN    

3.6 netstat -lu //只列出所有监听 udp 端口 netstat -lu

[root@glusterfs ftp-user2]# netstat -lu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
udp        0      0 dlp:domain              0.0.0.0:*                          
udp        0      0 0.0.0.0:bootps          0.0.0.0:*                          
udp        0      0 0.0.0.0:sunrpc          0.0.0.0:*                          
udp        0      0 dlp:ntp                 0.0.0.0:*     

3.7 netstat -lx //只列出所有监听 UNIX 端口

root@glusterfs ftp-user2]# netstat -lx
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     17941    @/tmp/.ICE-unix/3335
unix  2      [ ACC ]     STREAM     LISTENING     142424   @/containerd-shim/moby/727ba6c04f943685fd10e447b780ec8eed5c5843566cfcc06k
unix  2      [ ACC ]     STREAM     LISTENING     293125   /tmp/prometheus-nginx.socket
unix  2      [ ACC ]     STREAM     LISTENING     18305    @/containerd-shim/moby/4c32bc51bd8ef580c6833aac8bef650875fbb21b56566d031k
unix  2      [ ACC ]     STREAM     LISTENING     148807   @/containerd-shim/moby/79c45484bc5ae668c2cf73f4af2113f6fb5321c89ec948f6ek
unix  2      [ ACC ]     STREAM     LISTENING     52500    /var/run/libvirt/virtlockd-sock

3.8 netstat -s //显示所有端口的统计信息 netstat -s

root@glusterfs ftp-user2]# netstat -s
Ip:
	19205078 total packets received
	14540 forwarded
	0 incoming packets discarded
	19190538 incoming packets delivered
	19341801 requests sent out
	15 outgoing packets dropped
Icmp:
	40082 ICMP messages received
	102 input ICMP message failed.
	ICMP input histogram:
		destination unreachable: 40076
		echo requests: 4
		echo replies: 2
	48126 ICMP messages sent
	0 ICMP messages failed
	ICMP output histogram:
		destination unreachable: 48120
		echo request: 2
		echo replies: 4
…… ……
…… ……

3.9 netstat -r //显示核心路由信息 netstat -r

[root@glusterfs ftp-user2]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         gateway         0.0.0.0         UG        0 0          0 ens7f3
10.0.0.0        55.55.55.100    255.0.0.0       UG        0 0          0 ens7f3
10.255.0.0      0.0.0.0         255.255.255.0   U         0 0          0 cni0
55.55.55.0      0.0.0.0         255.255.255.0   U         0 0          0 ens7f3
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 docker0
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0
[root@glusterfs ftp-user2]#

3.10 netstat -pln | grep ssh //找出程序运行的端口

[root@glusterfs ftp-user2]# netstat -pln | grep ssh
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2009/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      2009/sshd           
unix  2      [ ACC ]     STREAM     LISTENING     1786987  33440/sshd: root@pt  /tmp/ssh-WKYGi5j7Qg/agent.33440
unix  2      [ ACC ]     STREAM     LISTENING     1950635  130291/sshd: root@p  /tmp/ssh-tosXL6cTfV/agent.130291
[root@glusterfs ftp-user2]#            

3.11 netstat -pln | grep 80 //找出端口的程序运行

[root@glusterfs ftp-user2]# netstat -pln | grep 80                                                 
tcp        0      0 0.0.0.0:18080           0.0.0.0:*               LISTEN      28592/nginx: master
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      28592/nginx: master
tcp6       0      0 :::30080                :::*                    LISTEN      4439/kube-proxy    
tcp6       0      0 :::6443                 :::*                    LISTEN      3807/kube-apiserver

tcp6       0      0 :::2380                 :::*                    LISTEN      3109/etcd          

4. windows statat 命令

查看被占用端口对应的PID 输入命令: netstat -aon|findstr “25” //smtp 是 25 然后
C:\Users\xxx>netstat -aon|findstr “25” //smtp 是 25 然后
TCP 0.0.0.0:25 0.0.0.0:0 LISTENING 2096

参考:

https://www.cnblogs.com/ggjucheng/p/2316661.html
https://blog.csdn.net/dongl890426/article/details/86981901

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值