Azure NSG如何创建拒绝VNet间入站规则?门户及PowerShell疑问
解决Azure NSG拒绝VNet间入站流量的问题
Azure NSG中没有直接选择单个VNet作为源的下拉选项,要实现拒绝VNet间入站的需求,可通过以下两种方式操作:
一、拒绝所有VNet间入站流量
默认的AllowVnetInbound规则使用VirtualNetwork服务标签匹配所有VNet内流量,只需创建优先级更高的拒绝规则覆盖它:
1. Azure门户操作
- 进入目标NSG的「入站安全规则」页面,点击「添加」
- 设置规则优先级为小于65000(比如100,数字越小优先级越高)
- 源选择「服务标签」,然后选择「VirtualNetwork」
- 源端口范围设为
*,目标地址/端口按需配置(若要全拒绝则设为*) - 协议选「任何」,动作设为「拒绝」
- 命名规则(比如
DenyVnetInbound)后点击「添加」
2. PowerShell操作
执行以下命令创建并添加拒绝规则:
# 获取目标NSG $nsg = Get-AzNetworkSecurityGroup -Name "你的NSG名称" -ResourceGroupName "你的资源组名称" # 创建拒绝规则 $denyRule = New-AzNetworkSecurityRuleConfig -Name "DenyVnetInbound" ` -Priority 100 ` -Direction Inbound ` -Access Deny ` -Protocol * ` -SourceAddressPrefix "VirtualNetwork" ` -SourcePortRange * ` -DestinationAddressPrefix * ` -DestinationPortRange * # 保存规则到NSG $nsg.SecurityRules.Add($denyRule) Set-AzNetworkSecurityGroup -NetworkSecurityGroup $nsg
二、拒绝特定VNet的入站流量
如果只需拒绝某个特定VNet的流量,将源地址替换为该VNet的CIDR范围即可:
1. Azure门户操作
- 添加规则时,源选择「IP地址」,输入目标VNet的CIDR(比如
10.0.0.0/16) - 其余设置同上述拒绝所有VNet流量的操作,确保优先级低于65000
2. PowerShell操作
# 获取目标NSG $nsg = Get-AzNetworkSecurityGroup -Name "你的NSG名称" -ResourceGroupName "你的资源组名称" # 创建针对特定VNet的拒绝规则 $denyRule = New-AzNetworkSecurityRuleConfig -Name "DenySpecificVnetInbound" ` -Priority 150 ` -Direction Inbound ` -Access Deny ` -Protocol * ` -SourceAddressPrefix "10.0.0.0/16" # 替换为目标VNet的CIDR ` -SourcePortRange * ` -DestinationAddressPrefix * ` -DestinationPortRange * # 保存规则 $nsg.SecurityRules.Add($denyRule) Set-AzNetworkSecurityGroup -NetworkSecurityGroup $nsg
关键注意事项
- NSG规则按优先级数字从小到大执行,拒绝规则的优先级必须小于65000(默认
AllowVnetInbound的优先级),才能优先触发拒绝动作。 - 若需更精细的控制,可针对特定协议、端口配置规则,无需全量拒绝。
内容的提问来源于stack exchange,提问作者MathGeek
相关产品推荐
相关产品推荐

