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

善用 netsh 指令快速診斷與設定 Windows 防火牆

編輯:Windows XP 系統介紹

Windows 防火牆內建強大的命令列工具 netsh advfirewall(XP/Server 2003 為 netsh firewall),可腳本化完成所有圖形介面操作,適合遠端管理、批次部署與故障排查。以下整理最常用的查詢與設定指令。

一、查詢現況

指令說明
netsh advfirewall show allprofiles顯示所有設定檔(Domain/Private/Public)完整設定
netsh advfirewall show currentprofile僅顯示目前生效的設定檔
netsh advfirewall firewall show rule name=all列出所有防火牆規則
netsh advfirewall monitor show firewall顯示當前作用中規則與連線狀態
netsh advfirewall monitor show connectionsecurity顯示 IPsec 連線安全規則

二、常見設定操作

# 開放特定程式(需完整路徑)
netsh advfirewall firewall add rule name="Allow MyApp" dir=in action=allow program="C:\MyApp\app.exe" enable=yes

# 開放特定埠(TCP 80)
netsh advfirewall firewall add rule name="Allow HTTP" dir=in action=allow protocol=TCP localport=80

# 封鎖特定 IP
netsh advfirewall firewall add rule name="Block BadIP" dir=in action=block remoteip=203.0.113.10

# 啟用/停用防火牆(當前設定檔)
netsh advfirewall set currentprofile state on
netsh advfirewall set currentprofile state off

# 重設為預設值(慎用)
netsh advfirewall reset

三、匯出/匯入設定(備份與部署)

# 匯出目前設定檔
netsh advfirewall export "C:\Backup\fw_policy.wfw"

# 於另一台電腦匯入
netsh advfirewall import "C:\Backup\fw_policy.wfw"

四、XP/Server 2003 相容指令(netsh firewall)

netsh firewall show config           # 顯示設定
netsh firewall set opmode enable     # 啟用
netsh firewall add allowedprogram "C:\App.exe" "MyApp" ENABLE
netsh firewall add portopening TCP 80 "HTTP"

熟練 netsh 即可在無 GUI 環境(Server Core、遠端 SSH)下完整掌控防火牆,並能寫成批次檔實現跨機器一鍵部署。

Copyright © Windows教程網 All Rights Reserved