คู่มือจัดการ Windows Firewall ด้วย netsh และ PowerShell [2026]
คู่มือจัดการ Windows Firewall ผ่าน command line ทั้ง netsh และ PowerShell ครอบคลุมการสร้างกฎ จัดการ port และ logging
จัดการ Windows Firewall ด้วย Command Line ทำได้ง่ายกว่าที่คิด
Windows Firewall เป็นระบบรักษาความปลอดภัยที่สำคัญที่สุดอย่างหนึ่งของ Windows Server การจัดการผ่าน GUI อาจสะดวกแต่ช้าเมื่อต้องตั้งค่าหลาย Rule พร้อมกัน การใช้ netsh และ PowerShell ช่วยให้ทำงานได้เร็วขึ้นและทำ Automation ได้ง่าย
บทความนี้จะอธิบายวิธีจัดการ Windows Firewall ผ่าน Command Line อย่างครบถ้วน ตั้งแต่พื้นฐานไปจนถึงขั้นสูง
ทำความเข้าใจ Firewall Profiles
Windows Firewall มี 3 Profiles ที่ทำงานแยกกัน ได้แก่ Domain Profile สำหรับเครื่องที่เชื่อมต่อ Active Directory, Private Profile สำหรับเครือข่ายส่วนตัว และ Public Profile สำหรับเครือข่ายสาธารณะ แต่ละ Profile มีกฎ Firewall แยกกัน ทำให้ปรับความปลอดภัยได้ตามสภาพแวดล้อม
ตรวจสอบสถานะ Firewall
ดูสถานะ Profile ปัจจุบันและทุก Profile ด้วย netsh
netsh advfirewall show currentprofile
netsh advfirewall show allprofiles
เปิดปิด Firewall อย่างถูกวิธี
netsh advfirewall set currentprofile state on
netsh advfirewall set allprofiles state on
สำหรับผู้ที่ดูแล VPS หรือ Dedicated Server ที่รัน Windows ไม่ควรปิด Firewall เด็ดขาด ให้ตั้งค่ากฎให้เหมาะสมแทน
สร้างกฎ Firewall ด้วย netsh
อนุญาตและบล็อก Port
netsh advfirewall firewall add rule name="Allow HTTP" dir=in action=allow protocol=TCP localport=80
netsh advfirewall firewall add rule name="Block Telnet" dir=in action=block protocol=TCP localport=23
จำกัดเฉพาะ IP และช่วง Port
netsh advfirewall firewall add rule name="Allow SSH from Office" dir=in action=allow protocol=TCP localport=22 remoteip=203.0.113.0/24
netsh advfirewall firewall add rule name="Allow App Ports" dir=in action=allow protocol=TCP localport=8000-9000
จัดการกฎที่มีอยู่
netsh advfirewall firewall show rule name=all
netsh advfirewall firewall delete rule name="Allow HTTP"
netsh advfirewall firewall set rule name="Allow HTTP" new enable=no
จัดการ Firewall ด้วย PowerShell
PowerShell มี Cmdlet ที่ทรงพลังกว่า netsh สำหรับจัดการ Firewall
Get-NetFirewallRule | Where-Object {$_.Enabled -eq 'True'} | Format-Table DisplayName, Direction, Action
New-NetFirewallRule -DisplayName "Allow HTTPS" -Direction Inbound -Protocol TCP -LocalPort 443 -Action Allow
New-NetFirewallRule -DisplayName "Allow RDP from Office" -Direction Inbound -Protocol TCP -LocalPort 3389 -RemoteAddress "203.0.113.0/24" -Action Allow
Remove-NetFirewallRule -DisplayName "Allow HTTPS"
Disable-NetFirewallRule -DisplayName "Allow HTTPS"
Enable-NetFirewallRule -DisplayName "Allow HTTPS"
ตั้งค่า Default Policy
Default Policy กำหนดว่าจะทำอย่างไรกับทราฟฟิกที่ไม่ตรงกับกฎใดๆ แนะนำให้บล็อกขาเข้าทั้งหมดเป็น Default แล้วเปิดเฉพาะที่ต้องการ
netsh advfirewall set currentprofile firewallpolicy blockinbound,allowoutbound
Export/Import และ Backup กฎ Firewall
การ Backup กฎ Firewall เป็นสิ่งสำคัญ โดยเฉพาะก่อนทำการเปลี่ยนแปลง
netsh advfirewall export "C:\firewall-backup.wfw"
netsh advfirewall import "C:\firewall-backup.wfw"
ถ้าตั้งค่าผิดพลาดจนแก้ไม่ได้ สามารถ Reset เป็นค่าเริ่มต้น
netsh advfirewall reset
เปิด Firewall Log ตรวจสอบทราฟฟิก
netsh advfirewall set currentprofile logging filename "C:\Windows\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
ดู Log ด้วย PowerShell
Get-Content "C:\Windows\System32\LogFiles\Firewall\pfirewall.log" -Tail 50
กฎ Firewall ที่แนะนำสำหรับ Windows Server
สำหรับ Hosting หรือเซิร์ฟเวอร์ที่ให้บริการเว็บ ควรเปิดเฉพาะ Port ที่จำเป็น ได้แก่ Port 80 และ 443 สำหรับ Web Server, Port 3389 สำหรับ RDP จำกัด IP, Port 53 สำหรับ DNS และ Port 25, 587 สำหรับ Mail Server
คำถามที่พบบ่อย (FAQ)
ตั้ง Firewall ผิดแล้วเข้าเซิร์ฟเวอร์ไม่ได้ ทำอย่างไร
ถ้าเข้าผ่าน RDP ไม่ได้ ให้ใช้ Console ของ Cloud Provider หรือ KVM over IP เข้าไป Reset Firewall ด้วย netsh advfirewall reset ก่อนตั้งค่าใหม่ควร Backup กฎเดิมเสมอ
ควรปิด Firewall เมื่อ Debug ปัญหาเครือข่ายไหม
ไม่ควรปิด Firewall ทั้งหมด ให้สร้าง Rule อนุญาตทราฟฟิกที่ต้องการแทน หรือเปิด Logging เพื่อดูว่าทราฟฟิกไหนถูกบล็อก จะได้แก้ปัญหาได้ตรงจุด
netsh กับ PowerShell ต่างกันอย่างไร
netsh เป็นเครื่องมือรุ่นเก่าที่ใช้ง่าย เหมาะกับงาน Quick Setup ส่วน PowerShell ยืดหยุ่นกว่า รองรับ Scripting ซับซ้อน และเป็นทิศทางที่ Microsoft สนับสนุน แนะนำ PowerShell สำหรับ Windows Server รุ่นใหม่
ควรเปิด Firewall Logging ตลอดเวลาไหม
แนะนำให้เปิด Logging สำหรับ Dropped Connections ตลอดเวลา เพราะช่วยตรวจจับการโจมตีและ Debug ปัญหาได้ แต่ตั้ง maxfilesize ให้เหมาะสมเพื่อไม่ให้กินพื้นที่ Disk มากเกินไป
สรุป
Windows Firewall เป็นเครื่องมือที่ทรงพลังในการปกป้องเซิร์ฟเวอร์ การจัดการผ่าน netsh และ PowerShell ช่วยให้ทำงานได้เร็วและทำ Automation ได้ง่าย เปิดเฉพาะ Port ที่จำเป็น จำกัด IP ที่เข้าถึงได้ และเปิด Logging ตรวจสอบความผิดปกติ
สำหรับผู้ที่ต้องการ VPS หรือ Dedicated Server ที่มีระบบรักษาความปลอดภัยครบถ้วน DriteStudio พร้อมให้บริการ
DriteStudio
タイで VPS、Webホスティング、コロケーションを提供するデジタルインフラ事業者
Craft Intertech (Thailand) Co., Ltd. が運営