Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Windows 防火牆進階配置:善用 netsh 指令快速查詢與精準設定

編輯:XP 系統應用技巧

Windows 防火牆透過 GUI 設定雖直覺,但批次管理、遠端配置、腳本自動化時,netsh advfirewall 指令更具彈性與效率。本文整理常用查詢與設定指令,幫助管理員快速掌握防火牆狀態、規則、日誌等關鍵配置。

常用查詢指令(Show)

# 進入 netsh 防火牆語境
netsh advfirewall

# 顯示目前設定檔(Domain/Private/Public)狀態
show currentprofile

# 顯示所有防火牆規則(含啟用/停用)
show rule name=all

# 顯示特定規則詳細資訊
show rule name="允許的程式"

# 顯示允許的程式清單
show allowedprogram

# 顯示開放連接埠
show portopening

# 顯示 ICMP 設定
show icmpsetting

# 顯示日誌設定
show logging

# 顯示通知設定
show notifications

# 顯示作業模式(開啟/關閉/封鎖所有)
show opmode

# 顯示服務設定
show service

# 顯示多播/廣播回應
show multicastbroadcastresponse

# 顯示完整狀態摘要
show state

常用設定指令(Set)

# 啟用/停用防火牆(目前設定檔)
set currentprofile state on|off

# 設定預設輸入/輸出動作
set currentprofile firewallpolicy blockinbound,allowoutbound

# 新增/修改允許的程式規則
set allowedprogram program="C:\Path\App.exe" name="MyApp" mode=enable|disable profile=all

# 開放/關閉連接埠
set portopening protocol=TCP port=8080 name="WebPort" mode=enable profile=all

# 設定 ICMP 允許類型(如允許 Ping 回應)
set icmpsetting type=8 mode=enable   # Echo Request

# 設定日誌路徑、大小、記錄類型
set logging filename="%systemroot%\system32\LogFiles\Firewall\pfirewall.log" maxfilesize=4096 droppedconnections=enable connections=enable

# 設定通知(彈出提示)
set notifications enable|disable

# 設定服務(如遠端桌面)
set service remotedesktop mode=enable profile=domain

腳本化與遠端管理

  • 將指令序列存為 .cmd 或 .ps1 批次部署多台主機。
  • 遠端執行:netsh -r RemotePC -u Domain\Admin -p Password advfirewall show allprofiles
  • 匯出/匯入原則:netsh advfirewall export "fwpolicy.wfw" / import "fwpolicy.wfw"
  • 重置為預設:netsh advfirewall reset

PowerShell 替代方案(Win 8.1/Server 2012 R2 以上)

# 同等功能,物件導向更強大
Get-NetFirewallProfile
Get-NetFirewallRule
New-NetFirewallRule -DisplayName "Allow HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow
Set-NetFirewallProfile -DefaultInboundAction Block

善用 netsh 或 PowerShell 將防火牆配置納入版本控管與自動化部署流程,確保環境一致性並降低人為錯誤風險。

Copyright © Windows教程網 All Rights Reserved