共用方式為


網路命令介面(netsh)

網路殼層 (netsh) 是一個多功能且全面的命令行公用程式,可用來設定、管理及監視 Windows 和 Windows Server 系統上的網路元件和伺服器角色。 透過 netsh,您可以檢視和修改網路設定、自動化工作,以及針對本機或遠端網路問題進行疑難解答。

在許多情況下,netsh 命令會提供與管理網路伺服器角色和功能的 Microsoft Management Console (MMC) 嵌入式管理單元同等的功能。 這可讓系統管理員透過圖形化 MMC 介面或使用命令行自動化來執行設定和管理工作。

例如,您可以使用 NPS MMC 嵌入式管理單元或內容中的 netsh nps netsh 命令來設定網路原則伺服器 (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 命令提示字元中使用 set machine 。 當您使用 -r時,您只會為目前的 netsh 實例設定目標電腦。
-u 指定您要使用 format 在 DomainName\Username 特定使用者帳戶下執行 netsh 命令。
-p 指定您想要提供使用者帳戶的密碼。 如果使用,則 * 指定您要安全地輸入密碼。
Command 這是您要執行的 netsh 命令。
-f 指定您想要在從您提供的腳本檔案執行命令之後結束 netsh。

如果字串值在字元之間包含空格,則必須以引號括住字串值。 例如: -r "contoso remote device" 。 退出並重新進入 netsh後,目標電腦會重設為本地電腦。 您可以指定儲存在 WINS 中的電腦名稱、UNC 名稱、DNS 伺服器要解析的因特網名稱或 IP 位址,在遠端電腦上執行 netsh 命令。

Note

若要使用某些 netsh 命令,例如 netsh firewall 使用 netsh -r 參數在另一部電腦上遠端執行的命令, 遠端登錄 服務必須在遠端電腦上執行。 如果此服務未執行,您可能會收到網路通訊錯誤訊息。

執行 netsh 命令

您可以在提升權限的命令提示字元或 PowerShell 視窗中執行 netsh 命令。 您可以根據所安裝的網路元件,切換到包含您想使用命令的情境。

例如,如果您輸入 netsh,則會直接進入命令列介面。 到達此處後,如果您鍵入 dhcp 並按 Enter,您將進入命令的 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 ?.

命令子環境定義

內容也可能同時包含命令和巢狀內容,稱為 子內容。 例如, 介面 內容包含 ipv4ipv6 等子內容,可讓您管理特定的網路介面設定。 例如:

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 並使用字串值「無線網路連線」時,請在字串值周圍加上雙引號。 例如:

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

以下簡短說明此文稿的用途:

  • 它會為名為 「Ethernet」 的網路介面設定靜態 IP 位址、子網掩碼和預設網關。
  • 設定要使用的主要 DNS 伺服器。
  • 新增次要 (後援) DNS 伺服器。
  • 為所有配置檔啟用具有進階安全性的 Windows 防火牆。

另請參閱