Create, change, or delete a network security group

When you use security rules in network security groups (NSGs), you can filter the type of network traffic that flows in and out of virtual network subnets and network interfaces. To learn more about NSGs, see Network security group overview. Next, complete the Filter network traffic tutorial to gain some experience with NSGs.

Prerequisites

If you don't have an Azure account with an active subscription, create a trial account. Complete one of these tasks before starting the remainder of this article:

  • Portal users: Sign in to the Azure portal with your Azure account.

  • PowerShell users: Run PowerShell locally from your computer.

    If you're running PowerShell locally, use Azure PowerShell module version 1.0.0 or later. Run Get-Module -ListAvailable Az.Network to find the installed version. If you need to install or upgrade, see Install Azure PowerShell module. Run Connect-AzAccount -Environment AzureChinaCloud to sign in to Azure.

  • Azure CLI users: Run Azure CLI locally from your computer.

    If you're running the Azure CLI locally, use Azure CLI version 2.0.28 or later. Run az --version to find the installed version. If you need to install or upgrade, see Install the Azure CLI. Run az login to sign in to Azure.

Assign the Network Contributor role or a custom role with the appropriate permissions.

Work with network security groups

You can create, view all, view details of, change, and delete an NSG. You can also associate or dissociate an NSG from a network interface or a subnet.

Create a network security group

The number of NSGs that you can create for each Azure region and subscription is limited. To learn more, see Azure subscription and service limits, quotas, and constraints.

Use az network nsg create to create an NSG named myNSG in the existing myResourceGroup resource group.

az network nsg create --resource-group MyResourceGroup --name myNSG

View all network security groups

Use az network nsg list to list all the NSGs in your subscription.

az network nsg list --out table

View details of a network security group

Use az network nsg show to view the details of an NSG.

az network nsg show --resource-group myResourceGroup --name myNSG

To learn more about the common Azure settings that are listed, see the following articles:

Change a network security group

The most common changes to an NSG are:

Associate or dissociate a network security group to or from a network interface

For more information about the association and dissociation of an NSG, see Associate or dissociate a network security group.

Associate or dissociate a network security group to or from a subnet

Use az network vnet subnet update to associate or dissociate an NSG to or from a subnet.

az network vnet subnet update --resource-group myResourceGroup --vnet-name myVNet --name mySubnet --network-security-group myNSG

Delete a network security group

If an NSG is associated to any subnets or network interfaces, it can't be deleted. Dissociate an NSG from all subnets and network interfaces before you attempt to delete it.

Use az network nsg delete to delete an NSG.

az network nsg delete --resource-group myResourceGroup --name myNSG

Work with security rules

An NSG contains zero or more security rules. You can create, view all, view details of, change, and delete a security rule.

Create a security rule

The number of rules per NSG that you can create for each Azure location and subscription is limited. To learn more, see Azure subscription and service limits, quotas, and constraints.

Use az network nsg rule create to create an NSG rule.

az network nsg rule create --resource-group myResourceGroup --nsg-name myNSG --name RDP-rule --priority 300 \
    --destination-address-prefixes '*' --destination-port-ranges 3389 --protocol Tcp --description "Allow RDP"

View all security rules

An NSG contains zero or more rules. To learn more about the list of information when you view the rules, see Security rules.

Use az network nsg rule list to view the security rules of an NSG.

az network nsg rule list --resource-group myResourceGroup --nsg-name myNSG

View the details of a security rule

Use az network nsg rule show to view the details of a security rule.

az network nsg rule show --resource-group myResourceGroup --nsg-name myNSG --name RDP-rule

Note

This procedure applies only to a custom security rule. It doesn't work if you choose a default security rule.

Change a security rule

Use az network nsg rule update to update an NSG rule.

az network nsg rule update --resource-group myResourceGroup --nsg-name myNSG --name RDP-rule --priority 200

Note

This procedure applies only to a custom security rule. You aren't allowed to change a default security rule.

Delete a security rule

Use az network nsg rule delete to delete a security rule from an NSG.

az network nsg rule delete --resource-group myResourceGroup --nsg-name myNSG --name RDP-rule

Note

This procedure applies only to a custom security rule. You aren't allowed to change a default security rule.

Work with application security groups

An application security group contains zero or more network interfaces. To learn more, see Application security groups. All network interfaces in an application security group must exist in the same virtual network. To learn how to add a network interface to an application security group, see Add a network interface to an application security group.

Create an application security group

Use az network asg create to create an application security group.

az network asg create --resource-group myResourceGroup --name myASG --location chinanorth3

View all application security groups

Use az network asg list to list all the application security groups in a resource group.

az network asg list --resource-group myResourceGroup --out table

View the details of a specific application security group

Use az network asg show to view the details of an application security group.

az network asg show --resource-group myResourceGroup --name myASG

Change an application security group

Use az network asg update to update the tags for an application security group.

az network asg update --resource-group myResourceGroup --name myASG --tags Dept=Finance

Note

You can't change the resource group, subscription, or location of an application security group by using the Azure CLI.

Delete an application security group

You can't delete an application security group if it contains any network interfaces. To remove all network interfaces from the application security group, either change the network interface settings or delete the network interfaces. To learn more, see Add or remove from application security groups or Delete a network interface.

Use az network asg delete to delete an application security group.

az network asg delete --resource-group myResourceGroup --name myASG

Permissions

To manage NSGs, security rules, and application security groups, your account must be assigned to the Network Contributor role. You can also use a custom role with the appropriate permissions assigned, as listed in the following tables.

Note

You might not see the full list of service tags if the Network Contributor role was assigned at a resource group level. To view the full list, you can assign this role at a subscription scope instead. If you can only allow the Network Contributor role for the resource group, you can then also create a custom role for the permissions Microsoft.Network/locations/serviceTags/read and Microsoft.Network/locations/serviceTagDetails/read. Assign them at a subscription scope along with the Network Contributor role at the resource group scope.

Network security group

Action Name
Microsoft.Network/networkSecurityGroups/read Get an NSG.
Microsoft.Network/networkSecurityGroups/write Create or update an NSG.
Microsoft.Network/networkSecurityGroups/delete Delete an NSG.
Microsoft.Network/networkSecurityGroups/join/action Associate an NSG to a subnet or network interface.

Note

To perform write operations on an NSG, the subscription account must have at least read permissions for the resource group along with Microsoft.Network/networkSecurityGroups/write permission.

Network security group rule

Action Name
Microsoft.Network/networkSecurityGroups/securityRules/read Get a rule.
Microsoft.Network/networkSecurityGroups/securityRules/write Create or update a rule.
Microsoft.Network/networkSecurityGroups/securityRules/delete Delete a rule.

Application security group

Action Name
Microsoft.Network/applicationSecurityGroups/joinIpConfiguration/action Join an IP configuration to an application security group.
Microsoft.Network/applicationSecurityGroups/joinNetworkSecurityRule/action Join a security rule to an application security group.
Microsoft.Network/applicationSecurityGroups/read Get an application security group.
Microsoft.Network/applicationSecurityGroups/write Create or update an application security group.
Microsoft.Network/applicationSecurityGroups/delete Delete an application security group.