前端开发入门到精通的在线学习网站

网站首页 > 资源文章 正文

网络工程师必会之netsh 命令(Windows平台)

qiguaw 2025-07-06 15:53:50 资源文章 5 ℃ 0 评论

历经三个多小时悉心整理资料,原创实属不易!

netsh是 Windows 系统内置的一个命令行工具,用于配置和管理网络相关的设置。它提供了一系列命令,允许用户或管理员查看、修改网络接口、防火墙规则、无线网络配置、IP地址、DNS设置等,甚至可以进行网络诊断。

1、查看网络接口名称、状态(如 以太网、Wi-Fi)

netsh interface show interface

2、设置静态IP地址和DNS

(1)设置IP地址

netsh interface ip set address name="<接口名称>" static <IP地址> <子网掩码> <默认网关>

(2)设置首选DNS

netsh interface ip set dns name="<接口名称>" static <首选DNS>

(3)设置备用DNS(可选)

netsh interface ip add dns name="<接口名称>" <备用DNS> index=2

示例:

netsh interface ip set address name="以太网" static 192.168.1.100 255.255.255.0 192.168.1.1

netsh interface ip set dns name="以太网" static 114.114.114.114

netsh interface ip add dns name="以太网" 8.8.8.8 index=2

最后配置完后使用ipconfig /all验证一下

3、配置静态路由

netsh interface ipv4 add route 172.16.0.0/16 "以太网" 192.168.1.254

解释:10.20.0.0/16目标网段。

192.168.1.254下一跳网关。

4、设置自动获取IP和DNS(DHCP)

netsh interface ip set address name="<接口名称>" source=dhcp

netsh interface ip set dns name="<接口名称>" source=dhcp

5、设置端口代理

netsh interface portproxy add v4tov4 listenport=8080 listenaddress=0.0.0.0 connectport=80 connectaddress=192.168.1.100

解释:将本机 8080 端口的流量转发到192.168.1.100 的 80 端口

(1)查看端口代理规则

netsh interface portproxy show all

(2)删除已创建的端口代理规则

netsh interface portproxy delete v4tov4 listenport=8080 listenaddress=0.0.0.0

(3)如果需要清空全部代理规则,可执行

netsh interface portproxy reset

6、禁用/启用网络适配器

(1)禁用

netsh interface set interface "本地连接" admin=disable

(2)启用

netsh interface set interface "本地连接" admin=enable

7、查看网络接口的MTU值以及网口流量统计值

netsh interface ipv4 show subinterfaces

8、修改接口的MTU值

netsh interface ipv4 set subinterface "以太网" mtu=1400 store=persistent

解释:

store=persistent重启后配置仍生效。

解决某些网络因MTU不兼容导致的数据包分片问题。

9、导出所有WLAN配置文件(含密码)

netsh wlan export profile key=clear folder=C:\wlan

解释:

export profile 指无线网络配置文件(包括 SSID、加密类型、密码等信息)。

key=clear在导出的配置文件中以明文形式包含无线网络的密码(密钥)。若省略此参数,导出的密码会被加密(无法直接查看)。

folder=C:\wlan指定配置文件导出的目标文件夹(此处为 C:\wlan)。若文件夹不存在,命令会报错,需手动创建。

10、捕获网络跟踪

netsh trace start capture=yes tracefile=C:\trace.etl

# 在执行netsh trace stop前进行网络操作(重现问题)

netsh trace stop

这会生成 C:\trace.etl 文件。

将 .etl 转换为 .pcapng,使用微软官方工具 etl2pcapng 进行格式转换

下载:
https://github.com/microsoft/etl2pcapng/releases

转换方式:etl2pcapng.exe C:\trace.etl C:\trace.pcapng

输出文件 trace.pcapng 即为 Wireshark 兼容的格式。可以用wireshark打开分析

设置缓冲区大小

netsh trace start maxsize=500MB

解释:限制跟踪文件最大为 500MB。

netsh trace过滤器

(1)按协议过滤

netsh trace start capture=yes tracefile=C:\diag.etl protocol=TCP

仅捕获 TCP 协议流量(如 Web 请求、文件传输)。

(2)按 IP 地址过滤

netsh trace start capture=yes tracefile=C:\diag.etl IPv4.Address=192.168.1.100

仅捕获与 192.168.1.100 相关的流量(源或目标地址)。

(3)按端口过滤

netsh trace start capture=yes tracefile=C:\diag.etl IPv4.Port=80

仅捕获 80 端口(HTTP)的流量。

(4) 按网络接口过滤

netsh trace start capture=yes tracefile=C:\diag.etl Ethernet

仅捕获名为 Ethernet 的网卡流量(需替换为实际接口名)。

(5) 组合过滤

netsh trace start capture=yes tracefile=C:\diag.etl protocol=TCP IPv4.Address=192.168.1.100 IPv4.Port=443

捕获 TCP 协议 + 目标 IP 192.168.1.100 + 443 端口(HTTPS)的流量。


11、设置防火墙规则

(1)阻止所有入站流量到 445 端口(TCP协议)

netsh advfirewall firewall add rule name="BlockSMB-Inbound" dir=in action=block protocol=TCP localport=445

(2)阻止所有出站流量到 445 端口(TCP协议,可选)

netsh advfirewall firewall add rule name="BlockSMB-Outbound" dir=out action=block protocol=TCP localport=445

(3)如果需要阻止UDP协议(如SMB over UDP)

netsh advfirewall firewall add rule name="BlockSMB-UDP-In" dir=in action=block protocol=UDP localport=445

(4)查看已添加的规则

netsh advfirewall firewall show rule name=all | findstr "BlockSMB"

(5)备份防火墙配置

netsh advfirewall export "C:\firewall_backup.wfw"

(6)从文件恢复防火墙规则

netsh advfirewall import "C:\firewall_rules.wfw"

(7)关闭所有防火墙(慎用,可能降低安全性)

netsh advfirewall set allprofiles state off

(8)启用所有防火墙

netsh advfirewall set allprofiles state on


结束:通过 netsh,用户可高效管理网络设置,尤其适合批量操作或自动化脚本场景。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表