netstat命令来自于英文词组”network statistics“的缩写,其功能是用于显示各种网络相关信息,例如网络连接状态、路由表信息、接口状态、NAT、多播成员等等。
netstat命令不仅应用于Linux系统,而且在Windows XP、Windows 7、Windows 10及Windows 11中均已默认支持,并且可用参数也相同,有经验的运维人员可以直接上手。
语法格式:netstat [参数]
常用参数:
-a | 显示所有连线中的Socket |
-p | 显示正在使用Socket的程序识别码和程序名称 |
-l | 仅列出在监听的服务状态 |
-t | 显示TCP传输协议的连线状况 |
-u | 显示UDP传输协议的连线状况 |
-i | 显示网络界面信息表单 |
-r | 显示路由表信息 |
-n | 直接使用IP地址,不通过域名服务器 |
参考实例
显示系统网络状态中的所有连接信息:
1 2 3 4 5 6 | [root@linuxcool ~] # 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:http 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:https 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:ms-wbt-server 0.0.0.0:* LISTEN |
显示系统网络状态中的UDP连接信息:
1 2 3 4 | [root@linuxcool ~] # netstat -nu Active Internet connections (w /o servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 172.19.226.238:68 172.19.239.253:67 ESTABLISHED |
显示系统网络状态中的UDP连接端口号使用信息:
1 2 3 4 5 6 | [root@linuxcool ~] # netstat -apu Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID /Program name udp 0 0 linuxcool:bootpc _gateway:bootps ESTABLISHED 1024 /NetworkManager udp 0 0 localhost:323 0.0.0.0:* 875 /chronyd udp6 0 0 localhost:323 [::]:* 875 /chronyd |
显示网卡当前状态信息:
1 2 3 4 5 | [root@linuxcool~] # netstat -i Kernel Interface table Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 31945 0 0 0 39499 0 0 0 BMRU lo 65536 0 0 0 0 0 0 0 0 LRU |
显示网络路由表状态信息:
1 2 3 4 5 | [root@linuxcool ~] # 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.19.224.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0 |
找到某个服务所对应的连接信息:
1 2 3 4 5 6 7 8 9 | [root@linuxcool ~] # netstat -ap | grep ssh unix 2 [ ] STREAM CONNECTED 89121805 203890 /sshd : root [ unix 3 [ ] STREAM CONNECTED 27396 1754 /sshd unix 3 [ ] STREAM CONNECTED 89120965 203890 /sshd : root [ unix 2 [ ] STREAM CONNECTED 89116510 203903 /sshd : root@p unix 2 [ ] STREAM CONNECTED 89121803 203890 /sshd : root [ unix 2 [ ] STREAM CONNECTED 29959 1754 /sshd unix 2 [ ] DGRAM 89111175 203890 /sshd : root [ unix 3 [ ] STREAM CONNECTED 89120964 203903 /sshd : root@p |