1 minute reading time
(175 words)
Create Network Security Group (NSG) using Azure CLI
How to create a Network Security Group (NSG) using Azure CLI
In this example, we will create an NSG for the subnet rather than attaching individual NSGs to each VM.
The virtual machine has been created without an NSG by specifying an empty variable --nsg""
We are using an existing resource group and vnet
Resource group: prod-ukw-core-rg
Vnet: prod-ukw-core-vnet
Create NSG using az network nsg create
# define nsg variables NsgName=prod-ukw-core-vnet-nsg RGroup=prod-ukw-core-rg Location=ukwest az network nsg create \ --name $NsgName \ --resource-group $RGroup \ --location $Location
Associate the new NSG to a subnet
# define subnet and nsg variables RGroup=prod-ukw-core-rg Subnet=subnet01 Vnet=prod-ukw-core-vnet Nsg=prod-ukw-core-vnet-nsg az network vnet subnet update \ -g $RGroup \ -n $Subnet \ --vnet-name $Vnet \ --network-security-group $Nsg
NSG has been created with the default rules and is associated with subnet01
In the vnet diagram view, we can see the nsg attached to the subnet
References:
Azure CLI - az network nsg
https://docs.microsoft.com/en-us/cli/azure/network/nsgAzure CLI - az network vnet subnet
by Author
https://docs.microsoft.com/en-us/cli/azure/network/vnet/subnet
Comments