创建、更改或删除网络安全组

使用网络安全组 (NSG) 中的安全规则时,可以筛选流入和流出虚拟网络子网和网络接口的网络流量的类型。 若要详细了解 NSG,请参阅网络安全组概述。 接下来请完成筛选网络流量教程,以获得有关 NSG 的一些经验。

先决条件

如果你没有具有有效订阅的 Azure 帐户,请创建试用版帐户。 在开始学习本文的余下内容之前,请完成以下任务之一:

  • 门户用户:使用 Azure 帐户登录到 Azure 门户

  • PowerShell 用户:在计算机本地运行 PowerShell。

    如果在本地运行 PowerShell,请使用 Azure PowerShell 模块 1.0.0 或更高版本。 运行 Get-Module -ListAvailable Az.Network 查找已安装的版本。 如果需要进行安装或升级,请参阅安装 Azure PowerShell 模块。 运行 Connect-AzAccount -Environment AzureChinaCloud 以登录到 Azure。

  • Azure CLI 用户:在计算机本地运行 Azure CLI。

    如果在本地运行 Azure CLI,请使用 Azure CLI 2.0.28 或更高版本。 运行 az --version 查找已安装的版本。 如需进行安装或升级,请参阅安装 Azure CLI。 运行 az login 以登录到 Azure。

分配具有相应权限网络参与者角色自定义角色

使用网络安全组

可对 NSG 执行创建、全部查看查看详细信息更改删除操作。 也可从网络接口子网关联或取消关联 NSG。

创建网络安全组

可以为每个 Azure 区域和订阅创建的 NSG 的数目有限。 有关详细信息,请参阅 Azure 订阅和服务限制、配额和约束

使用 New-AzNetworkSecurityGroup 在“中国北部 3”myNSG区域创建名为 的 NSG。 名为 myNSG 的 NSG 在现有的 myResourceGroup 资源组中创建。

# Define parameters for the new network security group
$NSGParams = @{
    Name              = "myNSG"
    ResourceGroupName = "myResourceGroup"
    Location          = "eastus"
}

# Create the network security group
New-AzNetworkSecurityGroup @NSGParams

查看所有网络安全组

使用 Get-AzNetworkSecurityGroup 列出订阅中的所有 NSG。

Get-AzNetworkSecurityGroup | format-table Name, Location, ResourceGroupName, ProvisioningState, ResourceGuid

查看网络安全组的详细信息

使用 Get-AzNetworkSecurityGroup 查看 NSG 的详细信息。

# Define parameters for the network security group
$NSGParams = @{
    Name              = "myNSG"
    ResourceGroupName = "myResourceGroup"
}

# Retrieve the network security group
Get-AzNetworkSecurityGroup @NSGParams

若要详细了解列出的常见 Azure 设置,请参阅以下文章:

更改网络安全组

对 NSG 最常见的更改包括:

将网络安全组与网络接口关联或取消关联

若要详细了解 NSG 的关联和取消关联,请参阅关联或取消关联网络安全组

将网络安全组与子网关联或取消关联

使用 Set-AzVirtualNetworkSubnetConfig 将 NSG 关联到子网或从子网取消关联 NSG。

# Define parameters for the virtual network and subnet configuration
$VNetParams = @{
    Name              = "myVNet"
    ResourceGroupName = "myResourceGroup"
}
$SubnetParams = @{
    Name              = "mySubnet"
    AddressPrefix     = "10.0.0.0/24"
    NetworkSecurityGroup = $networkSecurityGroup
}

# Retrieve the virtual network
$virtualNetwork = Get-AzVirtualNetwork @VNetParams

# Update the subnet configuration
Set-AzVirtualNetworkSubnetConfig -VirtualNetwork $virtualNetwork @SubnetParams

# Update the virtual network
Set-AzVirtualNetwork -VirtualNetwork $virtualNetwork

删除网络安全组

如果 NSG 与任何子网或网络接口相关联,则无法删除它。 请先从所有子网和网络接口取消关联 NSG,然后再尝试将其删除。

使用 Remove-AzNetworkSecurityGroup 删除 NSG。

# Define parameters for the network security group to be removed
$NSGParams = @{
    Name              = "myNSG"
    ResourceGroupName = "myResourceGroup"
}

# Remove the network security group
Remove-AzNetworkSecurityGroup @NSGParams

使用安全规则

NSG 包含零个或零个以上的安全规则。 可对安全规则执行创建查看所有查看详细信息更改以及删除等操作。

创建安全规则

可以为每个 Azure 位置和订阅创建的单 NSG 规则数有限。 有关详细信息,请参阅 Azure 订阅和服务限制、配额和约束

使用 Add-AzNetworkSecurityRuleConfig 创建 NSG 规则。

# Define parameters for the network security group and security rule
$NSGParams = @{
    Name              = "myNSG"
    ResourceGroupName = "myResourceGroup"
}
$RuleParams = @{
    Name                 = "RDP-rule"
    Description          = "Allow RDP"
    Access               = "Allow"
    Protocol             = "Tcp"
    Direction            = "Inbound"
    Priority             = 300
    SourceAddressPrefix  = "*"
    SourcePortRange      = "*"
    DestinationAddressPrefix = "*"
    DestinationPortRange = 3389
}

# Retrieve the network security group
$networkSecurityGroup = Get-AzNetworkSecurityGroup @NSGParams

# Add the security rule to the network security group
Add-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $networkSecurityGroup @RuleParams

# Update the network security group
Set-AzNetworkSecurityGroup -NetworkSecurityGroup $networkSecurityGroup

查看所有安全规则

NSG 包含零个或零个以上的规则。 若要详细了解查看规则时出现的信息列表,请参阅安全规则

使用 Get-AzNetworkSecurityRuleConfig 查看 NSG 的安全规则。

# Define parameters for the network security group
$NSGParams = @{
    Name              = "myNSG"
    ResourceGroupName = "myResourceGroup"
}

# Retrieve the network security group
$networkSecurityGroup = Get-AzNetworkSecurityGroup @NSGParams

# List security rules of the network security group in a table
Get-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $networkSecurityGroup | Format-Table Name, Protocol, Access, Priority, Direction, SourcePortRange, DestinationPortRange, SourceAddressPrefix, DestinationAddressPrefix

查看安全规则的详细信息

使用 Get-AzNetworkSecurityRuleConfig 查看安全规则的详细信息。

# Define parameters for the network security group and security rule
$NSGParams = @{
    Name              = "myNSG"
    ResourceGroupName = "myResourceGroup"
}
$RuleParams = @{
    Name = "RDP-rule"
}

# Retrieve the network security group
$networkSecurityGroup = Get-AzNetworkSecurityGroup @NSGParams

# View details of the security rule
Get-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $networkSecurityGroup @RuleParams

备注

此过程仅适用于自定义安全规则。 如果选择了默认安全规则,则此过程不适用。

更改安全规则

使用 Set-AzNetworkSecurityRuleConfig 更新 NSG 规则。

# Define parameters for the network security group and security rule
$NSGParams = @{
    Name              = "myNSG"
    ResourceGroupName = "myResourceGroup"
}
$RuleParams = @{
    Name                 = "RDP-rule"
    Description          = "Allow RDP"
    Access               = "Allow"
    Protocol             = "Tcp"
    Direction            = "Inbound"
    Priority             = 200
    SourceAddressPrefix  = "*"
    SourcePortRange      = "*"
    DestinationAddressPrefix = "*"
    DestinationPortRange = 3389
}

# Retrieve the network security group
$networkSecurityGroup = Get-AzNetworkSecurityGroup @NSGParams

# Update the security rule in the network security group
Set-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $networkSecurityGroup @RuleParams

# Update the network security group
Set-AzNetworkSecurityGroup -NetworkSecurityGroup $networkSecurityGroup

备注

此过程仅适用于自定义安全规则。 不允许更改默认安全规则。

删除安全规则

使用 Remove-AzNetworkSecurityRuleConfig 删除 NSG 中的安全规则。

# Define parameters for the network security group and security rule
$NSGParams = @{
    Name              = "myNSG"
    ResourceGroupName = "myResourceGroup"
}
$RuleParams = @{
    Name = "RDP-rule"
}

# Retrieve the network security group
$networkSecurityGroup = Get-AzNetworkSecurityGroup @NSGParams

# Remove the security rule from the network security group
Remove-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $networkSecurityGroup @RuleParams

# Update the network security group
Set-AzNetworkSecurityGroup -NetworkSecurityGroup $networkSecurityGroup

备注

此过程仅适用于自定义安全规则。 不允许更改默认安全规则。

使用应用程序安全组

应用程序安全组包含零个或多个网络接口。 若要了解详细信息,请参阅应用程序安全组。 应用程序安全组中的所有网络接口必须存在于同一虚拟网络中。 要了解如何将网络接口添加到应用程序安全组,请参阅将网络接口添加到应用程序安全组

创建应用程序安全组

使用 New-AzApplicationSecurityGroup 创建应用程序安全组。

# Define parameters for the new application security group
$ASGParams = @{
    ResourceGroupName = "myResourceGroup"
    Name              = "myASG"
    Location          = "eastus"
}

# Create the application security group
New-AzApplicationSecurityGroup @ASGParams

查看所有应用程序安全组

使用 Get-AzApplicationSecurityGroup 列出 Azure 订阅中的所有应用程序安全组。

Get-AzApplicationSecurityGroup | format-table Name, ResourceGroupName, Location

查看特定应用程序安全组的详细信息

使用 Get-AzApplicationSecurityGroup 查看应用程序安全组的详细信息。

Get-AzApplicationSecurityGroup -Name myASG

更改应用程序安全组

# Define parameters for the application security group
$ASGParams = @{
    ResourceGroupName = "myResourceGroup"
    Name              = "myASG"
}

# Retrieve the application security group
$applicationSecurityGroup = Get-AzApplicationSecurityGroup @ASGParams

New-AzTag -ResourceId $applicationSecurityGroup.Id -Tag @{ Dept = "Finance" }

删除应用程序安全组

无法删除包含任何网络接口的应用程序安全组。 若要从应用程序安全组中删除所有网络接口,请更改网络接口设置,或删除网络接口。 有关详细信息,请参阅添加到应用程序安全组或从中移除删除网络接口

使用 Remove-AzApplicationSecurityGroup 删除应用程序安全组。

# Define parameters for the application security group to be removed
$ASGParams = @{
    ResourceGroupName = "myResourceGroup"
    Name              = "myASG"
}

# Remove the application security group
Remove-AzApplicationSecurityGroup @ASGParams

权限

若要管理 NSG、安全规则和应用程序安全组,必须将帐户分配到网络参与者角色。 还可以使用分配有以下表中列出的相应权限的自定义角色

备注

如果已在资源组级别分配了“网络参与者”角色,则可能不会看到服务标记的完整列表。 若要查看完整列表,可以改为在订阅范围内分配此角色。 如果只能允许资源组的“网络参与者”角色,则还可以为权限 Microsoft.Network/locations/serviceTags/readMicrosoft.Network/locations/serviceTagDetails/read 创建自定义角色。 在订阅范围内分配它们,并在资源组范围内分配“网络参与者”角色。

网络安全组

操作 名称
Microsoft.Network/networkSecurityGroups/read 获取 NSG。
Microsoft.Network/networkSecurityGroups/write 创建或更新 NSG。
Microsoft.Network/networkSecurityGroups/delete 删除 NSG。
Microsoft.Network/networkSecurityGroups/join/action 将 NSG 与子网或网络接口关联。

备注

若要对 NSG 执行 write 操作,订阅帐户必须至少具有对资源组的 read 权限以及 Microsoft.Network/networkSecurityGroups/write 权限。

网络安全组规则

操作 名称
Microsoft.Network/networkSecurityGroups/securityRules/read 获取规则。
Microsoft.Network/networkSecurityGroups/securityRules/write 创建或更新规则。
Microsoft.Network/networkSecurityGroups/securityRules/delete 删除规则。

应用程序安全组

操作 名称
Microsoft.Network/applicationSecurityGroups/joinIpConfiguration/action 将 IP 配置加入到应用程序安全组中。
Microsoft.Network/applicationSecurityGroups/joinNetworkSecurityRule/action 将安全规则加入到应用程序安全组中。
Microsoft.Network/applicationSecurityGroups/read 获取应用程序安全组。
Microsoft.Network/applicationSecurityGroups/write 创建或更新应用程序安全组。
Microsoft.Network/applicationSecurityGroups/delete 删除应用程序安全组。