简介

netsh advfirewall firewall 命令行在Windows Vista 和 Windows Server 2008 中可用。
它提供了用于控制 Windows 防火墙行为的功能。
在早期版本的 Windows <包含Win7> 操作系统中应该使用 netsh firewall 命令。
新版本的命令行提供了更精确地控制的防火墙规则的功能,这些规则包括以下的每个配置文件设置︰

  • 域(Domain)
  • 私有(Private)
  • 公共(Public)

    基本使用

    进入防火墙

    1. C:\Users\0310011772>netsh
    2. netsh>

    使用help

    ```java C:\Users\0310011772>netsh netsh>help

下列指令有效:

此上下文中的命令: .. - 移到上一层上下文级。 ? - 显示命令列表。 abort - 丢弃在脱机模式下所做的更改。 add - 在项目列表上添加一个配置项目。 advfirewall - 更改到 netsh advfirewall' 上下文。 alias - 添加一个别名 branchcache - 更改到netsh branchcache’ 上下文。 bridge - 更改到 netsh bridge' 上下文。 bye - 退出程序。 commit - 提交在脱机模式中所做的更改。 delete - 在项目列表上删除一个配置项目。 dhcpclient - 更改到netsh dhcpclient’ 上下文。 dnsclient - 更改到 netsh dnsclient' 上下文。 dump - 显示一个配置脚本。 exec - 运行一个脚本文件。 exit - 退出程序。 firewall - 更改到netsh firewall’ 上下文。 help - 显示命令列表。 http - 更改到 netsh http' 上下文。 interface - 更改到netsh interface’ 上下文。 ipsec - 更改到 netsh ipsec' 上下文。 lan - 更改到netsh lan’ 上下文。 mbn - 更改到 netsh mbn' 上下文。 namespace - 更改到netsh namespace’ 上下文。 netio - 更改到 netsh netio' 上下文。 offline - 将当前模式设置成脱机。 online - 将当前模式设置成联机。 p2p - 更改到netsh p2p’ 上下文。 popd - 从堆栈上打开一个上下文。 pushd - 将当前上下文放入堆栈。 quit - 退出程序。 ras - 更改到 netsh ras' 上下文。 rpc - 更改到netsh rpc’ 上下文。 set - 更新配置设置。 show - 显示信息。 trace - 更改到 netsh trace' 上下文。 unalias - 删除一个别名。 wcn - 更改到netsh wcn’ 上下文。 wfp - 更改到 netsh wfp' 上下文。 winhttp - 更改到netsh winhttp’ 上下文。 winsock - 更改到 netsh winsock' 上下文。 wlan - 更改到netsh wlan’ 上下文。

下列的子上下文可用: advfirewall branchcache bridge dhcpclient dnsclient firewall http interface ipsec lan mbn namespace netio p2p ras rpc trace wcn wfp winhttp winsock wlan

若需要命令的更多帮助信息,请键入命令,接着是空格, 后面跟 ?。

  1. <a name="txYAa"></a>
  2. # netsh firewall>
  3. <a name="LKVMZ"></a>
  4. ## help
  5. ```java
  6. netsh firewall>help
  7. 下列指令有效:
  8. 命令从 netsh 上下文继承:
  9. .. - 移到上一层上下文级。
  10. abort - 丢弃在脱机模式下所做的更改。
  11. add - 在项目列表上添加一个配置项目。
  12. advfirewall - 更改到 `netsh advfirewall' 上下文。
  13. alias - 添加一个别名
  14. branchcache - 更改到 `netsh branchcache' 上下文。
  15. bridge - 更改到 `netsh bridge' 上下文。
  16. bye - 退出程序。
  17. commit - 提交在脱机模式中所做的更改。
  18. delete - 在项目列表上删除一个配置项目。
  19. dhcpclient - 更改到 `netsh dhcpclient' 上下文。
  20. dnsclient - 更改到 `netsh dnsclient' 上下文。
  21. exit - 退出程序。
  22. firewall - 更改到 `netsh firewall' 上下文。
  23. http - 更改到 `netsh http' 上下文。
  24. interface - 更改到 `netsh interface' 上下文。
  25. ipsec - 更改到 `netsh ipsec' 上下文。
  26. lan - 更改到 `netsh lan' 上下文。
  27. mbn - 更改到 `netsh mbn' 上下文。
  28. namespace - 更改到 `netsh namespace' 上下文。
  29. netio - 更改到 `netsh netio' 上下文。
  30. offline - 将当前模式设置成脱机。
  31. online - 将当前模式设置成联机。
  32. p2p - 更改到 `netsh p2p' 上下文。
  33. popd - 从堆栈上打开一个上下文。
  34. pushd - 将当前上下文放入堆栈。
  35. quit - 退出程序。
  36. ras - 更改到 `netsh ras' 上下文。
  37. rpc - 更改到 `netsh rpc' 上下文。
  38. set - 更新配置设置。
  39. show - 显示信息。
  40. trace - 更改到 `netsh trace' 上下文。
  41. unalias - 删除一个别名。
  42. wcn - 更改到 `netsh wcn' 上下文。
  43. wfp - 更改到 `netsh wfp' 上下文。
  44. winhttp - 更改到 `netsh winhttp' 上下文。
  45. winsock - 更改到 `netsh winsock' 上下文。
  46. wlan - 更改到 `netsh wlan' 上下文。
  47. 此上下文中的命令:
  48. ? - 显示命令列表。
  49. add - 添加防火墙配置。
  50. delete - 删除防火墙配置。
  51. dump - 显示一个配置脚本。
  52. help - 显示命令列表。
  53. set - 设置防火墙配置。
  54. show - 显示防火墙配置。
  55. 若需要命令的更多帮助信息,请键入命令,接着是空格,
  56. 后面跟 ?。
  57. netsh firewall>

实例

  1. // 查看是否启动windows防火墙
  2. netsh advfirewall show allprofiles state
  3. // 查看默认防火墙规则
  4. netsh advfirewall show allprofiles firewallpolicy
  5. //查看windows防火墙所有入站规则
  6. netsh advfirewall firewall show rule name=all dir=in type=static verbose
  7. //查看windows防火墙指定端口规则
  8. netsh advfirewall firewall show rule name=all localport=8089
  1. //查看防火墙放行的程序
  2. netsh firewall>show allowedprogram // (当已经进入到netsh firewall,则不用再输入相关内容)
  3. netsh firewall show allowedprogram
  4. //打开445端口
  5. netsh firewall set portopening TCP 445 ENABLE
  6. //关闭3389端口
  7. netsh firewall set portopening TCP 3389 disable
  8. netsh firewall set portopening TCP 8088 disable
  9. netsh firewall set portopening TCP 3389 disable
  10. //删除放行程序A.exe
  11. netsh firewall delete allowedprogram C:\A.exe
  12. //添加程序C盘下的A.exe并放行
  13. netsh firewall set allowedprogram C:\A.exe A ENABLE
  14. netsh firewall add allowedprogram C:\A.exe A ENABLE
  15. //开启ICMP协议
  16. netsh firewall set icmpsettting type=ALL mode=enable
  17. netsh firewall set icmpsettting type=ALL mode=disable
  18. //允许出站数据包太大
  19. netsh firewall set icmpsettting type=2 mode=enable
  20. //删除针对本地21端口的所有入站规则:
  21. netsh advfirewall firewall delete name rule name=all protocol=tcp localport=8089
  22. // 重新载入
  23. firewall --reload
  24. // 开启&关闭防火墙
  25. netsh advfirewall set allprofiles state on
  26. netsh advfirewall set allprofiles state off
  27. // 关闭端口
  28. // 测试ok
  29. netsh advfirewall firewall add rule name=test1 dir=in action=block protocol=TCP localport=8089
  30. netsh firewall add portopening protocol = TCP port = 8088 name = "关闭8088" mode = DISABLE
  31. netsh firewall set portopening protocol=tcp port=8088 mode=disable name=deny8088
  32. firewall --reload
  33. netsh firewall set icmpsettting type=ALL mode=enable //开启ICMP协议
  34. 开启端口
  35. netsh firewall add portopening protocol = TCP port = 139 name = "开启139" mode = ENABLE
  36. netsh firewall add portopening protocol = TCP port = 445 name = "开启445" mode = ENABLE
  37. netsh firewall add portopening protocol = UDP port = 137 name = "开启137" mode = ENABLE
  38. netsh firewall add portopening protocol = UDP port = 138 name = "开启138" mode = ENABLE
  39. 检测端口是否关闭成功
  40. netstat -na -p tcp | findstr "8088"
  41. 没有任何记录返回,则说明该端口关闭成功了。
  1. C:\WINDOWS\system32>netsh advfirewall firewall delete rule
  2. 提供的许多参数无效。请查看帮助获取正确语法。
  3. 用法: delete rule name=<string>
  4. [dir=in|out]
  5. [profile=public|private|domain|any[,...]]
  6. [program=<program path>]
  7. [service=<service short name>|any]
  8. [localip=any|<IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
  9. [remoteip=any|localsubnet|dns|dhcp|wins|defaultgateway|
  10. <IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
  11. [localport=0-65535|<port range>[,...]|RPC|RPC-EPMap|any]
  12. [remoteport=0-65535|<port range>[,...]|any]
  13. [protocol=0-255|icmpv4|icmpv6|icmpv4:type,code|icmpv6:type,code|
  14. tcp|udp|any]
  15. 注释:
  16. - 删除按名称识别的规则,也可按终结点、端口、
  17. 协议和类型识别规则
  18. - 如果找到多个匹配项,则删除所有匹配规则。
  19. - 如果指定 name=all,则从指定的
  20. 类型和配置文件中删除所有规则。
  21. 示例:
  22. 删除本地端口 80 的所有入则:
  23. netsh advfirewall firewall delete rule name=all protocol=tcp localport=80
  24. 删除名为 "allow80" 的规则:
  25. netsh advfirewall firewall delete rule name="allow80"
  1. 1)恢复初始防火墙设置
  2. netsh advfirewall reset
  3. 2)关闭防火墙
  4. netsh advfirewall set allprofiles state off
  5. 3)启用桌面防火墙
  6. netsh advfirewall set allprofiles state on
  7. 4)设置默认输入和输出策略
  8. netsh advfirewall set allprofiles firewallpolicy allowinbound,allowoutbound
  9. 以上是设置为允许,如果设置为拒绝使用blockinbound,blockoutbound
  10. 5)关闭tcp协议的139端口
  11. netsh advfirewall firewall add rule name=”deny tcp 139 dir=in protocol=tcp localport=139 action=block
  12. 6)关闭udp协议的139端口
  13. netsh advfirewall firewall add rule name=”deny udp 139 dir=in protocol=udp localport=139 action=block
  14. 7)关闭tcp协议的445端口
  15. netsh advfirewall firewall add rule name=”deny tcp 445 dir=in protocol=tcp localport=445 action=block
  16. 8)关闭udp协议的445端口
  17. netsh advfirewall firewall add rule name=”deny udp 445 dir=in protocol=udp localport=445 action=block
  18. 9)使用相同的方法,依次关闭TCP协议的212223137138338958005900端口。
  19. netsh advfirewall firewall add rule name= deny tcp 21 dir=in protocol=tcp localport=21 action=block
  20. netsh advfirewall firewall add rule name= deny tcp 22 dir=in protocol=tcp localport=22 action=block
  21. netsh advfirewall firewall add rule name= deny tcp 23 dir=in protocol=tcp localport=23 action=block
  22. netsh advfirewall firewall add rule name= deny tcp 3389 dir=in protocol=tcp localport=3389 action=block 学习电脑
  23. netsh advfirewall firewall add rule name= deny tcp 5800 dir=in protocol=tcp localport=5800 action=block
  24. netsh advfirewall firewall add rule name= deny tcp 5900 dir=in protocol=tcp localport=5900 action=block
  25. netsh advfirewall firewall add rule name= deny tcp 137 dir=in protocol=tcp localport=137 action=block
  26. netsh advfirewall firewall add rule name= deny tcp 138 dir=in protocol=tcp localport=138 action=block
  27. 以上内容搬运自百度知道https://zhidao.baidu.com/question/2075121309871751268.html,稍作整理

Set命令