使用 netsh advfirewall 防火牆而非 netsh 防火牆來控制 Windows 防火牆行為

本文說明如何使用 netsh advfirewall 防火牆內容, netsh firewall 而非內容來控制 Windows 防火牆行為。

適用:Windows Server 2012 R2
原始 KB 編號: 947709

摘要

netsh advfirewall防火牆命令行內容可在 Windows Server 2012 R2 中使用。 此內容提供控制防火牆內容所提供之 Windows 防火牆行為的 netsh firewall 功能。

此內容也提供更精確地控制防火牆規則的功能。 這些規則包括下列個別設定檔設定:

  • 網域
  • 私人
  • 公用

在未來 netsh firewall 的 Windows 作業系統版本中,命令行內容可能已被取代。 建議您使用 netsh advfirewall 防火牆內容來控制防火牆行為。

重要事項

如果您是 Administrators 群組的成員,且已在電腦上啟用使用者帳戶控制,請從具有更高許可權的命令提示字元執行命令。 若要以提升的許可權啟動命令提示字元,請尋找您用來啟動命令提示字元會話的圖示或 [開始] 功能表項,以滑鼠右鍵按下它,然後按兩下 [ 以系統管理員身分執行]

下表提供一些常用命令的範例。 您可以使用這些範例來協助您從舊 netsh firewall 版內容移轉至新的 netsh advfirewall 防火牆內容。

此外, netsh advfirewall 也會提供可用來取得詳細內嵌說明的命令。

命令範例 1:啟用程式

舊命令 新增命令
netsh firewall add allowedprogram C:\MyApp\MyApp.exe "My Application" ENABLE netsh advfirewall firewall add rule name="My Application" dir=in action=allow program="C:\MyApp\MyApp.exe" enable=yes
netsh firewall add allowedprogram program=C:\MyApp\MyApp.exe name="My Application" mode=ENABLE scope=CUSTOM addresses=157.60.0.1,172.16.0.0/16,LocalSubnet profile=Domain netsh advfirewall firewall add rule name="My Application" dir=in action=allow program= "C:\MyApp\MyApp.exe" enable=yes remoteip=157.60.0.1,172.16.0.0/16,LocalSubnet profile=domain
netsh firewall add allowedprogram program=C:\MyApp\MyApp.exe name="My Application" mode=ENABLE scope=CUSTOM addresses=157.60.0.1,172.16.0.0/16,LocalSubnet profile=ALL 執行下列命令:
netsh advfirewall firewall add rule name="My Application" dir=in action=allow program= "C:\MyApp\MyApp.exe" enable=yes remoteip=157.60.0.1,172.16.0.0/16,LocalSubnet profile=domain
netsh advfirewall firewall add rule name="My Application" dir=in action=allow program="C:\MyApp\MyApp.exe" enable=yes remoteip=157.60.0.1,172.16.0.0/16,LocalSubnet profile=private

如需如何新增防火牆規則的詳細資訊,請執行下列命令:

netsh advfirewall firewall add rule ?

命令範例 2:啟用埠

舊命令 新增命令
netsh firewall add portopening TCP 80 "Open Port 80" netsh advfirewall firewall add rule name= "Open Port 80" dir=in action=allow protocol=TCP localport=80

如需如何新增防火牆規則的詳細資訊,請執行下列命令:

netsh advfirewall firewall add rule ?

命令範例 3:刪除已啟用的程式或埠

舊命令 新增命令
netsh firewall delete allowedprogram C:\MyApp\MyApp.exe netsh advfirewall firewall delete rule name= rule name program="C:\MyApp\MyApp.exe"
delete portopening protocol=UDP port=500 netsh advfirewall firewall delete rule name= rule name protocol=udp localport=500

如需如何刪除防火牆規則的詳細資訊,請執行下列命令:

netsh advfirewall firewall delete rule ?

命令範例 4:設定 ICMP 設定

舊命令 新增命令
netsh firewall set icmpsetting 8 netsh advfirewall firewall add rule name="ICMP Allow incoming V4 echo request" protocol=icmpv4:8,any dir=in action=allow
netsh firewall set icmpsetting type=ALL mode=enable netsh advfirewall firewall add rule name= "All ICMP V4" protocol=icmpv4:any,any dir=in action=allow
netsh firewall set icmpsetting 13 disable all netsh advfirewall firewall add rule name="Block Type 13 ICMP V4" protocol=icmpv4:13,any dir=in action=block

如需如何設定 ICMP 設定的詳細資訊,請執行下列命令:

netsh advfirewall firewall add rule ?

命令範例 5:設定記錄

舊命令 新增命令
netsh firewall set logging %systemroot%\system32\LogFiles\Firewall\pfirewall.log 4096 ENABLE ENABLE 執行下列命令:
netsh advfirewall set currentprofile logging filename %systemroot%\system32\LogFiles\Firewall\pfirewall.log
netsh advfirewall set currentprofile logging maxfilesize 4096
netsh advfirewall set currentprofile logging droppedconnections enable
netsh advfirewall set currentprofile logging allowedconnections enable

如需詳細資訊,請執行下列命令:

netsh advfirewall set currentprofile ?

如果您要設定特定設定檔的記錄,請使用下列其中一個選項, currentprofile 而不是 選項:

  • Domainprofile
  • Privateprofile
  • Publicprofile

命令範例 6:啟用 Windows 防火牆

舊命令 新增命令
netsh firewall set opmode ENABLE netsh advfirewall set currentprofile state on
netsh firewall set opmode mode=ENABLE exceptions=enable 執行下列命令:
Netsh advfirewall set currentprofile state on
netsh advfirewall set currentprofile firewallpolicy blockinboundalways,allowoutbound
netsh firewall set opmode mode=enable exceptions=disable profile=domain 執行下列命令:
Netsh advfirewall set domainprofile state on
netsh advfirewall set domainprofile firewallpolicy blockinbound,allowoutbound
netsh firewall set opmode mode=enable profile=ALL 執行下列命令:
netsh advfirewall set domainprofile state on
netsh advfirewall set privateprofile state on

如需詳細資訊,請執行下列命令:

netsh advfirewall set currentprofile ?

如果您要設定特定設定檔的防火牆狀態,請使用下列其中一個選項, currentprofile 而不是 選項:

  • Domainprofile
  • Privateprofile
  • Publicprofile

命令範例 7:還原原則預設值

舊命令 新增命令
netsh firewall reset netsh advfirewall reset

如需詳細資訊,請執行下列命令:

netsh advfirewall reset ?

命令範例 8:啟用特定服務

舊命令 新增命令
netsh firewall set service FileAndPrint netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes
netsh firewall set service RemoteDesktop enable netsh advfirewall firewall set rule group="remote desktop" new enable=Yes
netsh firewall set service RemoteDesktop enable profile=ALL 執行下列命令:

netsh advfirewall firewall set rule group="remote desktop" new enable=Yes profile=domain

netsh advfirewall firewall set rule group="remote desktop" new enable=Yes profile=private