Fix Azure Virtual Network Nsg Errors
When working with Azure Virtual Network, you may encounter a configuration error that prevents your deployment from working. This guide explains the most common mistake with nsg and shows the exact fix.
A Common Mistake
Creating a Network Security Group (NSG) with overly permissive rules (e.g., allowing all traffic from any source), creating a security vulnerability.
The incorrect command:
az network nsg create --name my-nsg --resource-group my-rg
az network nsg rule create --name allow-all --nsg-name my-nsg --resource-group my-rg --priority 100 --direction Inbound --access Allow --protocol * --source-address-prefixes * --source-port-ranges * --destination-address-prefixes * --destination-port-ranges *
Error output:
NSG rule created: Allow all inbound traffic from any source.
This is equivalent to disabling the firewall. Any internet host can reach any port on any VM in the subnet. Attackers can scan for open ports, exploit vulnerabilities, and gain access.
The Correct Approach
The right way to configure nsg in Azure Virtual Network:
az network nsg create --name my-nsg --resource-group my-rg
az network nsg rule create --name allow-ssh --nsg-name my-nsg --resource-group my-rg --priority 100 --direction Inbound --access Allow --protocol Tcp --source-address-prefixes 203.0.113.0/24 --source-port-ranges * --destination-address-prefixes 10.100.1.0/24 --destination-port-ranges 22
az network nsg rule create --name deny-all --nsg-name my-nsg --resource-group my-rg --priority 1000 --direction Inbound --access Deny --protocol * --source-address-prefixes * --source-port-ranges * --destination-address-prefixes * --destination-port-ranges *
Successful result:
NSG rules created.
Only SSH from the management IP range is allowed. All other inbound traffic is denied. Default NSG rules (AllowVNetInBound, AllowAzureLoadBalancerInBound, DenyAllInBound) are overridden appropriately.
How to Prevent This
Follow Least Privilege: allow only required ports and source IPs. Use application security groups (ASGs) for rule organization. NSG flow logs capture traffic for analysis. Default rules cannot be deleted but can be overridden with higher priority denies. NSGs can be applied to subnets or NICs.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. Secure your cloud with DodaTech.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro