iT邦幫忙

DAY 21
8

使用指令管理 Microsoft Windows Server系列 第 21

使用 netsh 設定防火牆

啟用防火牆:

netsh advfirewall set currentprofile state on

關閉防火牆:

netsh advfirewall set currentprofile state off

在例外清單中,加入連接埠,例如下面指令分別允許 TCP 1234 連入,禁止 UDP 5678 連出:

netsh advfirewall firewall add rule name="允許 TCP 1234 連入" protocol=TCP dir=in localport=1234 action=allow
netsh advfirewall firewall add rule name="禁止 UDP 5678 連入" protocol=UDP dir=out localport=5678 action=block

請注意啊!規則的名稱(也就是 name= 後面的文字)不能叫做 all(不管大小寫,都視為相同)。為什麼呢?請看最後的範例就知道了。

如果要允許的連接埠是連續的呢?
例如下面允許 TCP 20、21 連入

netsh advfirewall firewall add rule name="允許 TCP 20~21 連入" protocol=TCP dir=in localport=20-21 action=allow

如果是應用程式的話呢?

netsh firewall firewall add rule name="允許 hello.exe 連入" dir=in program="c:\alexc\hello.exe" action=allow 

那要怎麼知道防火牆所有的設定呢?

netsh advfirewall firewall show rule name="all"

上一篇
使用 netsh 清除 ARP 快取
下一篇
使用 netsh 連線無線網路
系列文
使用指令管理 Microsoft Windows Server25
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
黃彥儒
iT邦高手 1 級 ‧ 2017-11-12 15:12:11

netsh advfirewall firewall add rule name="禁止 UDP 5678 連入" protocol=UDP dir=out localport=5678 action=block

這個Name似乎有點錯誤,應該是禁止 UDP 5678 連出

1
蹦蹦哥
iT邦新手 5 級 ‧ 2017-12-04 11:12:40

netsh advfirewall firewall add rule name="允許 hello.exe 連入" dir=in program="c:\alexc\hello.exe" action=allow

應該漏了adv,若有誤請再告知,謝謝。

我要留言

立即登入留言