Overview and What You Will Learn
A Network Security Group (NSG) is a list of allow and deny rules that filters inbound and outbound traffic to a subnet or a network interface, and the lowest priority number that matches a packet wins. In this lab, you will create an NSG that allows only HTTPS inbound, attach it to a subnet, prove that other ports are blocked, and see rule priority decide the outcome when two rules conflict.
Why This Matters in Production
A correctly scoped NSG stops exposure before it exists. Imagine an engineer at a growing company like Grab who opens RDP to the internet on a production VM "just to finish a quick fix." Within days, automated scanners find the port and start brute-force logins against it. An NSG that allows only the traffic the VM actually needs makes that mistake impossible to make by accident.
Core Principles
Every NSG ships with default rules at the bottom of the priority range, and your own rules go above them with numbers from 100 to 4096. Azure checks rules from the lowest number upward and stops at the first match.
100 to 4096} R1 -->|Match| X([Rule decides]) R1 -->|No match| R2{Default rules
65000 to 65500} R2 -->|VNet or LB| A([Allowed]) R2 -->|Anything else| D([Denied]) classDef decision fill:#F59E0B,color:#111827 classDef good fill:#10B981,color:#FFFFFF classDef bad fill:#EF4444,color:#FFFFFF classDef neutral fill:#475569,color:#FFFFFF class R1,R2 decision class A good class D bad class P,X neutral
The default inbound rules explain why a fresh NSG is already restrictive:
| Priority | Name | Effect |
|---|---|---|
| 65000 | AllowVnetInBound | Allows traffic from inside the virtual network |
| 65001 | AllowAzureLoadBalancerInBound | Allows Azure load balancer health probes |
| 65500 | DenyAllInBound | Denies everything else, including the internet |
Outbound works the other way around. The defaults allow VNet and internet traffic out, so a VM can reach the internet unless you add a deny rule.
RememberAn NSG can be attached to a subnet, a network interface, or both. When both exist, inbound traffic must be allowed by the subnet NSG and then by the NIC NSG, so a deny at either level blocks it.
Detailed Step-by-Step Practical Lab
How to Create the Resource Group and VNet
export RG="rg-nsg-lab-mumbai" az group create --name $RG --location centralindia az network vnet create \ --resource-group $RG \ --name vnet-nsg-lab \ --address-prefix 10.0.0.0/16 \ --subnet-name subnet-web \ --subnet-prefix 10.0.1.0/24How to Create an NSG That Allows Only HTTPS Inbound
State the direction and source explicitly instead of relying on defaults. The Internet service tag covers all public address space, including Azure-owned ranges such as Cloud Shell.
az network nsg create \ --resource-group $RG \ --name nsg-web-lab az network nsg rule create \ --resource-group $RG \ --nsg-name nsg-web-lab \ --name allow-https \ --priority 100 \ --direction Inbound \ --access Allow \ --protocol Tcp \ --source-address-prefixes Internet \ --destination-port-ranges 443How to Associate an NSG With a Subnet
A rule only applies once the NSG is attached to a subnet or a NIC.
az network vnet subnet update \ --resource-group $RG \ --vnet-name vnet-nsg-lab \ --name subnet-web \ --network-security-group nsg-web-labHow to Create a Test VM With Listeners on Several Ports
To test the NSG, the VM needs something listening on the ports you probe. This cloud-init file starts simple web servers on 443 and 8080. The NSG filters ports, not protocols, so plain HTTP on 443 is enough for the test. SSH is deliberately not allowed, so you will manage nothing on this VM directly.
cat > cloud-init-listeners.yaml << 'EOF'#cloud-configruncmd: - mkdir -p /srv/test - sh -c 'echo "NSG test page on $(hostname)" > /srv/test/index.html' - sh -c 'cd /srv/test && setsid nohup python3 -m http.server 443 > /var/log/test443.log 2>&1 &' - sh -c 'cd /srv/test && setsid nohup python3 -m http.server 8080 > /var/log/test8080.log 2>&1 &'EOF az vm create \ --resource-group $RG \ --name vm-nsg-test \ --image Ubuntu2204 \ --size Standard_B1s \ --vnet-name vnet-nsg-lab \ --subnet subnet-web \ --nsg "" \ --admin-username azureadmin \ --generate-ssh-keys \ --custom-data cloud-init-listeners.yamlTip
--nsg ""stops the command from creating a second NSG on the network interface, because the subnet NSG already covers everything in the subnet.
How to View the Effective Security Rules on a VM
A VM can be affected by an NSG on its subnet and another on its NIC at the same time. The effective rules command shows the combined result, so you do not have to reason through both by hand. The VM must be running.
NIC_ID=$(az vm show \ --resource-group $RG \ --name vm-nsg-test \ --query "networkProfile.networkInterfaces[0].id" --output tsv)NIC_NAME=$(basename "$NIC_ID") az network nic list-effective-nsg \ --resource-group $RG \ --name "$NIC_NAME" \ --query "value[].effectiveSecurityRules[].[name, priority, access, direction]" \ --output tableHow to Test That the NSG Blocks Unwanted Ports
Give cloud-init a minute after the VM is created, then probe three ports from Cloud Shell. A blocked port times out because the NSG silently drops the packet, while a closed port with an allowed path would refuse the connection immediately.
VM_IP=$(az vm show \ --resource-group $RG \ --name vm-nsg-test \ --show-details \ --query publicIps --output tsv) for p in 443 8080 22; do timeout 5 bash -c "</dev/tcp/$VM_IP/$p" 2>/dev/null \ && echo "port $p: open" || echo "port $p: blocked"donePort 22 is blocked even though SSH is running on the VM, which is the NSG doing its job.
How to See Rule Priority Decide the Outcome
Allow port 8080 with a high number, confirm it opens, then add a deny with a lower number and watch it win. Rule changes can take a few seconds to apply, so wait before testing.
az network nsg rule create \ --resource-group $RG \ --nsg-name nsg-web-lab \ --name allow-8080 \ --priority 300 \ --direction Inbound \ --access Allow \ --protocol Tcp \ --source-address-prefixes Internet \ --destination-port-ranges 8080 sleep 30timeout 5 bash -c "</dev/tcp/$VM_IP/8080" 2>/dev/null && echo "8080: open" || echo "8080: blocked"# Expected: open az network nsg rule create \ --resource-group $RG \ --nsg-name nsg-web-lab \ --name deny-8080 \ --priority 200 \ --direction Inbound \ --access Deny \ --protocol Tcp \ --source-address-prefixes Internet \ --destination-port-ranges 8080 sleep 30timeout 5 bash -c "</dev/tcp/$VM_IP/8080" 2>/dev/null && echo "8080: open" || echo "8080: blocked"# Expected: blocked, because priority 200 is evaluated before 300Common MistakeAssuming the rule you created last wins. Evaluation depends only on the priority number, so a deny at 200 beats an allow at 300 no matter which was created first.
How to Clean Up
az group delete --name $RG --yes --no-waitProduction Best Practices & Common Pitfalls
SecurityNever open management ports such as RDP (3389) or SSH (22) to
0.0.0.0/0or theInternettag on a production NSG. Use Azure Bastion for browser-based access, or restrict the source to your organization's known IP ranges when Bastion is not an option.
TipWhen a connection fails unexpectedly, use
az network nic list-effective-nsgto see the combined rules, andaz network watcher test-ip-flowto find the exact rule that allows or denies a specific packet.
- Rule priority sets evaluation order, not creation order. Always read the priority field, and leave gaps such as 100, 200, 300 so you can insert rules later without renumbering.
- Make allow rules narrow. Name the exact port, protocol, and source, using service tags or specific ranges instead of
*. - Do not treat the default deny as your only protection. Anyone who later adds a broad allow rule above it removes that safety net, so keep your own rules explicit and reviewed.
- Group VMs by role with Application Security Groups. Rules can then target a role such as "web servers" instead of fixed IP addresses that change over time.
- Attach at the subnet level where you can. Using both subnet and NIC NSGs means traffic must pass both, which makes troubleshooting harder.
- Plan for traffic logging. NSG flow logs are being replaced by virtual network flow logs, so use the newer option for new auditing setups.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
az network nsg create |
Create a new Network Security Group |
az network nsg rule create |
Add an inbound or outbound rule |
az network vnet subnet update --network-security-group |
Associate an NSG with a subnet |
az network nic list-effective-nsg |
Show the combined effective rules for a NIC |
az network watcher test-ip-flow |
Find which rule allows or denies a packet |
az network nsg rule list --nsg-name NAME --output table |
List the rules in an NSG |