Network shell(netsh)是一种通用且全面的命令行实用工具,用于在 Windows 和 Windows Server 系统上配置、管理和监视网络组件和服务器角色。 使用 netsh,可以查看和修改网络设置、自动执行任务,以及在本地或远程排查网络问题。
在许多情况下,netsh 命令提供与用于管理网络服务器角色和功能的 Microsoft 管理控制台(MMC)管理插件等效的功能。 这允许管理员通过图形 MMC 界面或使用命令行自动化来执行配置和管理任务。
例如,可以使用 NPS MMC 管理单元或 netsh 命令在 netsh nps 上下文中配置网络策略服务器(NPS)。 此外,还有一些用于网络技术的 netsh 命令,例如用于 IPv6、网桥和远程过程调用 (RPC) 的命令,这些命令在Windows Server 中不能作为 MMC 管理单元使用。
Important
建议使用 Windows PowerShell 管理 Windows 和 Windows Server 中的网络技术,而不是 netsh。
示例语法
netsh [-a <Aliasfile>] [-c <Context>] [-r <RemoteMachine>] [-u <DomainName>\<Username>] [-p <Password> | *] [Command> | -f <ScriptFile>]
以下参数是可选的。
| Parameter | Description |
|---|---|
| -a | 指定运行别名文件后要返回到 netsh shell。 别名文件是包含一个或多个 netsh 命令的文本文件。 |
| -c | 输入指定的 netsh 上下文。 上下文表示与特定功能或函数相关的特定可用命令集。 |
| -r | 指定希望命令在其计算机名称、完全限定域名(FQDN)或 IP 地址的远程计算机上运行。 远程注册表服务必须在远程计算机上运行。 如果未运行,Windows 会显示“找不到网络路径”错误消息。 如果指定 -r 后跟另一个命令,则 netsh 会在远程计算机上运行该命令,然后返回到命令提示符。 如果您在未指定其他命令的情况下指定 -r,netsh 将在远程模式下打开。 此过程类似于在 netsh 命令 shell 中使用 set machine 。 使用 -r时,仅为当前 netsh 实例设置目标计算机。 |
| -u | 指定要使用格式在特定用户帐户 DomainName\Username 下运行 netsh 命令。 |
| -p | 指定要为该用户帐户提供密码。 如果使用 * ,则指定要安全地输入密码。 |
| Command | 这是要运行的 netsh 命令。 |
| -f | 指定在执行提供的脚本文件中的命令后要退出 netsh。 |
如果字符串值在字符之间包含空格,你必须用引号将字符串值引起来。 例如,-r "contoso remote device"。 退出并重新输入 netsh后,目标计算机将重置为本地计算机。 可以通过指定存储在 WINS 中的计算机名称、UNC 名称、要由 DNS 服务器解析的 Internet 名称或 IP 地址,在远程计算机上运行 netsh 命令。
Note
若要使用某些 netsh 命令(例如 netsh firewall 使用 netsh -r 参数远程在另一台计算机上的命令),远程 注册表 服务必须在远程计算机上运行。 如果此服务未运行,可能会收到网络通信错误消息。
运行 netsh 命令
可以使用提升权限的命令提示符或 PowerShell 窗口来运行 netsh 命令。 可以根据已安装的网络组件,切换到包含您要使用的命令的上下文。
例如,如果键入 netsh,则直接进入网络 shell。 在此处键入并按 Enter 后,dhcp将转到命令的上下文菜单中dhcpclient。
命令上下文
Netsh 使用动态链接库 (DLL) 文件与其他操作系统组件进行交互。 每个 netsh 帮助程序 DLL 提供了一组称为 上下文的大量功能,这是特定于网络服务器角色或功能的一组命令。 这些上下文通过为一个或多个服务、实用工具或协议提供配置和监视支持来扩展 netsh 的功能。
例如,dhcpmon.dll 为 netsh 提供配置和管理 DHCP 服务器所需的上下文和命令集。
放置 > 让你知道你所处的上下文或子上下文。 可以通过运行 netsh ? 或 netsh help 按 Enter 获取任何可用命令的 netsh 上下文列表。
下面是示例输出:
The following commands are available:
Commands in this context:
.. - Goes up one context level.
? - Displays a list of commands.
abort - Discards changes made while in offline mode.
add - Adds a configuration entry to a list of entries.
advfirewall - Changes to the 'netsh advfirewall' context.
alias - Adds an alias.
branchcache - Changes to the 'netsh branchcache' context.
bridge - Changes to the 'netsh bridge' context.
bye - Exits the program.
commit - Commits changes made while in offline mode.
delete - Deletes a configuration entry from a list of entries.
dhcpclient - Changes to the 'netsh dhcpclient' context.
dnsclient - Changes to the 'netsh dnsclient' context.
dump - Displays a configuration script.
exec - Runs a script file.
exit - Exits the program.
firewall - Changes to the 'netsh firewall' context.
help - Displays a list of commands.
http - Changes to the 'netsh http' context.
interface - Changes to the 'netsh interface' context.
ipsec - Changes to the 'netsh ipsec' context.
ipsecdosprotection - Changes to the 'netsh ipsecdosprotection' context.
lan - Changes to the 'netsh lan' context.
namespace - Changes to the 'netsh namespace' context.
netio - Changes to the 'netsh netio' context.
offline - Sets the current mode to offline.
online - Sets the current mode to online.
popd - Pops a context from the stack.
pushd - Pushes current context on stack.
quit - Exits the program.
ras - Changes to the 'netsh ras' context.
rpc - Changes to the 'netsh rpc' context.
set - Updates configuration settings.
show - Displays information.
trace - Changes to the 'netsh trace' context.
unalias - Deletes an alias.
wfp - Changes to the 'netsh wfp' context.
winhttp - Changes to the 'netsh winhttp' context.
winsock - Changes to the 'netsh winsock' context.
The following sub-contexts are available:
advfirewall branchcache bridge dhcpclient dnsclient firewall http interface ipsec lan mbn namespace netio nlm ras rpc trace wcn wfp winhttp winsock wlan
To view help for a command, type the command, followed by a space, and then type ?.
命令子上下文
上下文还可能包括命令和嵌套上下文,称为 子上下文。 例如, 接口 上下文包含子上下文(如 ipv4 和 ipv6),可用于管理特定的网络接口设置。 例如:
netsh> interface help
The following commands are available:
Commands in this context:
6to4 - Changes to the 'netsh interface 6to4' context.
? - Displays a list of commands.
dump - Displays a configuration script.
fl48 - Changes to the 'netsh interface fl48' context.
fl68 - Changes to the 'netsh interface fl68' context.
help - Displays a list of commands.
httpstunnel - Changes to the 'netsh interface httpstunnel' context.
ipv4 - Changes to the 'netsh interface ipv4' context.
ipv6 - Changes to the 'netsh interface ipv6' context.
isatap - Changes to the 'netsh interface isatap' context.
portproxy - Changes to the 'netsh interface portproxy' context.
set - Sets configuration information.
show - Displays information.
tcp - Changes to the 'netsh interface tcp' context.
teredo - Changes to the 'netsh interface teredo' context.
udp - Changes to the 'netsh interface udp' context.
The following sub-contexts are available:
6to4 fl48 fl68 httpstunnel ipv4 ipv6 isatap portproxy tcp teredo udp
To view help for a command, type the command, followed by a space, and then
type ?.
要在不更改当前上下文的情况下在另一个上下文中执行任务,请在 netsh shell 处键入要使用的命令的上下文路径。 例如,要为“NetLAN1”添加静态 IPv4 地址并指定子网掩码和网关,请键入:
netsh interface ipv4 set address "NetLAN1" static <IP_Address> <Subnet_Mask> <Gateway>
格式图例
在批处理文件中或脚本中以交互方式运行命令时,可以使用以下格式图例来解释和使用正确的命令语法。
- 斜体中的文本是键入命令时必须提供的信息。 例如,如果命令具有名为 -UserName 的参数,则必须键入实际的用户名。
- 粗体文本是键入命令时必须完全按所示内容键入的信息。
- 后跟省略号的文本 ... 是一个参数,可以在命令行中重复多次。
- 括号 [ ] 之间的文本是可选项。
- 大括号 { } 之间的文本与管道分隔的选项提供了一组选项,必须从中选择一个选项,例如
{enable|disable}。 - 使用 Courier 字体设置格式的文本是代码或程序输出。
netsh 命令的字符串值
一些命令包含需要字符串值的参数。 如果字符串值包含字符之间的空格,则需要用双引号将字符串值括起来。
在为 IPv4 网络设置具有与 DHCP 的interface的字符串值的 参数时,请在字符串值两侧使用双引号。 例如:
netsh interface ipv4> set address name="Wireless Network Connection" dhcp
或者,对于网络接口,可以使用索引号代替与其关联的名称。 例如:
netsh interface ipv4> set address name=15 dhcp
若要在接口子上下文中查找连接的接口的名称或索引,请选择接口类型和类型 show interfaces 以查看详细信息。 例如:
netsh interface> ipv4 show interfaces
netsh interface> ipv6 show interfaces
如果你不在接口子上下文中,则可以为任一接口运行 完整 命令。 例如:
netsh interface ipv4 show interfaces
netsh interface ipv6 show interfaces
在批处理文件中使用 netsh
批处理文件是一个纯文本文件,其中包含由 Windows 命令提示符(CMD)执行的一系列命令。 批处理文件具有 .bat 或 .cmd 扩展名。 它们用于自动执行重复任务、管理系统配置和运行命令序列,而无需用户干预。 批处理文件对于涉及多个命令行指令的任务特别有用。
这些情况下,批处理脚本非常有用:
批量网络配置:如果需要为多台计算机配置网络设置,批处理文件可以通过自动执行设置来简化该过程。 批量配置适用于企业环境或管理多台计算机的 IT 专业人员。
网络重新配置:在不同网络环境之间切换的用户可以使用批处理文件快速更改 IP 地址和 DNS 服务器等设置,而无需手动干预。
备份和还原设置:可以在进行更改之前创建用于备份网络设置的批处理脚本,并根据需要创建另一个脚本来还原这些设置。
下面是一个示例批处理文件。 以“rem”开头的行是注释,说明命令执行的操作:
@echo off
echo Configuring network settings...
REM Set the IP address, subnet mask, and default gateway
netsh interface ip set address name="Ethernet" static 192.168.1.100 255.255.255.0 192.168.1.1
REM Set the DNS server address
netsh interface ip set dns name="Ethernet" static 8.8.8.8
REM Add a secondary DNS server
netsh interface ip add dns name="Ethernet" 8.8.4.4 index=2
REM Enable the firewall
netsh advfirewall set allprofiles state on
echo Network settings configured successfully.
pause
下面简要介绍了此脚本的作用:
- 它为名为“以太网”的网络接口设置静态 IP 地址、子网掩码和默认网关。
- 配置要使用的主 DNS 服务器。
- 添加辅助(回退)DNS 服务器。
- 为所有配置文件启用具有高级安全性的 Windows 防火墙。