本文内容

  1. 摘要
  2. 命令示例 1:启用程序
  3. 命令示例 2:启用端口
  4. 命令示例 3:删除已启用的程序或端口
  5. 命令示例 4:配置 ICMP 设置
  6. 命令示例 5:设置日志记录
  7. 命令示例 6:启用 Windows 防火墙
  8. 命令示例 7:还原策略默认值
  9. 命令示例 8:启用特定服务

本文介绍如何使用netsh advfirewall防火墙上下文而不是netsh firewall上下文来控制 Windows 防火墙行为。

适用于:Windows Server 2012 R2
原始 KB 编号:947709

摘要

netsh advfirewall防火墙命令行上下文在 Windows Server 2012 R2 中可用。 此上下文提供用于控制防火墙上下文提供的 Windows 防火墙行为的netsh firewall功能。

此上下文还提供更精确地控制防火墙规则的功能。 这些规则包括以下按配置文件设置:

  • Domain
  • 私人
  • 公共

将来netsh firewall版本的 Windows 操作系统中可能会弃用命令行上下文。 建议使用netsh advfirewall防火墙上下文来控制防火墙行为。

重要

如果你是管理员组的成员,并且在计算机上启用了用户帐户控制,请从具有提升权限的命令提示符运行命令。 若要使用提升的权限启动命令提示符,请找到用于启动命令提示符会话的图标或“开始”菜单条目,右键单击它,然后单击“以管理员身份运行”。

下表提供了一些常用命令示例。 可以使用这些示例来帮助你从旧netsh firewall上下文迁移到新的netsh advfirewall防火墙上下文。

此外,netsh advfirewall还提供了可用于获取详细内联帮助的命令。

命令示例 1:启用程序

旧命令新建命令
netsh firewall add allowedprogram C:\MyApp\MyApp.exe "My Application" ENABLEnetsh advfirewall firewall add rule name="My Application" dir=in action=allow program="C:\MyApp\MyApp.exe" enable=yes
netsh firewall add allowedprogram program=C:\MyApp\MyApp.exe name="My Application" mode=ENABLE scope=CUSTOM addresses=157.60.0.1,172.16.0.0/16,LocalSubnet profile=Domainnetsh advfirewall firewall add rule name="My Application" dir=in action=allow program= "C:\MyApp\MyApp.exe" enable=yes remoteip=157.60.0.1,172.16.0.0/16,LocalSubnet profile=domain
netsh firewall add allowedprogram program=C:\MyApp\MyApp.exe name="My Application" mode=ENABLE scope=CUSTOM addresses=157.60.0.1,172.16.0.0/16,LocalSubnet profile=ALL运行以下命令:
netsh advfirewall firewall add rule name="My Application" dir=in action=allow program= "C:\MyApp\MyApp.exe" enable=yes remoteip=157.60.0.1,172.16.0.0/16,LocalSubnet profile=domain
netsh advfirewall firewall add rule name="My Application" dir=in action=allow program="C:\MyApp\MyApp.exe" enable=yes remoteip=157.60.0.1,172.16.0.0/16,LocalSubnet profile=private

有关如何添加防火墙规则的详细信息,请运行以下命令:

控制台复制

netsh advfirewall firewall add rule ?

命令示例 2:启用端口

旧命令新建命令
netsh firewall add portopening TCP 80 "Open Port 80"netsh advfirewall firewall add rule name= "Open Port 80" dir=in action=allow protocol=TCP localport=80

有关如何添加防火墙规则的详细信息,请运行以下命令:

控制台复制

netsh advfirewall firewall add rule ?

命令示例 3:删除已启用的程序或端口

旧命令新建命令
netsh firewall delete allowedprogram C:\MyApp\MyApp.exenetsh advfirewall firewall delete rule name= rule name program="C:\MyApp\MyApp.exe"
delete portopening protocol=UDP port=500netsh advfirewall firewall delete rule name= rule name protocol=udp localport=500

有关如何删除防火墙规则的详细信息,请运行以下命令:

控制台复制

netsh advfirewall firewall delete rule ?

命令示例 4:配置 ICMP 设置

旧命令新建命令
netsh firewall set icmpsetting 8netsh advfirewall firewall add rule name="ICMP Allow incoming V4 echo request" protocol=icmpv4:8,any dir=in action=allow
netsh firewall set icmpsetting type=ALL mode=enablenetsh advfirewall firewall add rule name= "All ICMP V4" protocol=icmpv4:any,any dir=in action=allow
netsh firewall set icmpsetting 13 disable allnetsh advfirewall firewall add rule name="Block Type 13 ICMP V4" protocol=icmpv4:13,any dir=in action=block

有关如何配置 ICMP 设置的详细信息,请运行以下命令:

控制台复制

netsh advfirewall firewall add rule ?

命令示例 5:设置日志记录

旧命令新建命令
netsh firewall set logging %systemroot%\system32\LogFiles\Firewall\pfirewall.log 4096 ENABLE ENABLE运行以下命令:
netsh advfirewall set currentprofile logging filename %systemroot%\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

有关详细信息,请运行以下命令:

控制台复制

netsh advfirewall set currentprofile ?

如果要为特定配置文件设置日志记录,请使用以下选项之一,currentprofile而不是选项之一:

  • Domainprofile
  • Privateprofile
  • Publicprofile

命令示例 6:启用 Windows 防火墙

旧命令新建命令
netsh firewall set opmode ENABLEnetsh advfirewall set currentprofile state on
netsh firewall set opmode mode=ENABLE exceptions=enable运行以下命令:
Netsh advfirewall set currentprofile state on
netsh advfirewall set currentprofile firewallpolicy blockinboundalways,allowoutbound
netsh firewall set opmode mode=enable exceptions=disable profile=domain运行以下命令:
Netsh advfirewall set domainprofile state on
netsh advfirewall set domainprofile firewallpolicy blockinbound,allowoutbound
netsh firewall set opmode mode=enable profile=ALL运行以下命令:
netsh advfirewall set domainprofile state on
netsh advfirewall set privateprofile state on

有关详细信息,请运行以下命令:

控制台复制

netsh advfirewall set currentprofile ?

如果要为特定配置文件设置防火墙状态,请使用以下选项之一,currentprofile而不是选项之一:

  • Domainprofile
  • Privateprofile
  • Publicprofile

命令示例 7:还原策略默认值

旧命令新建命令
netsh firewall resetnetsh advfirewall reset

有关详细信息,请运行以下命令:

控制台复制

netsh advfirewall reset ?

命令示例 8:启用特定服务

旧命令新建命令
netsh firewall set service FileAndPrintnetsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes
netsh firewall set service RemoteDesktop enablenetsh advfirewall firewall set rule group="remote desktop" new enable=Yes
netsh firewall set service RemoteDesktop enable profile=ALL运行以下命令:

netsh advfirewall firewall set rule group="remote desktop" new enable=Yes profile=domain

netsh advfirewall firewall set rule group="remote desktop" new enable=Yes profile=private

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注