How to create and evaluate Azure Network Security Group rules, including components, rule precedence, subnet versus NIC associations, and using service tags to allow or restrict service traffic
In this lesson we’ll cover how to create and evaluate Azure Network Security Group (NSG) rules. You’ll learn the key NSG rule components, how rules are evaluated when NSGs are applied to subnets and NICs, and how service tags simplify managing rules for Azure services.
Any, IP/CIDR, Service Tag, or Application Security Group (ASG)
Any, 10.0.1.0/24, Storage, VirtualNetwork
Action
Allow or Deny
Determines whether matching traffic is permitted
Action determines whether traffic matching the rule is permitted or blocked.Service tags let you reference groups of IP ranges for Azure services (for example VirtualNetwork, Internet, Storage, AzureLoadBalancer) instead of maintaining explicit IP lists. Azure maintains the underlying IP ranges for service tags so you don’t have to.Now we’ll walk through a portal demo showing NSG creation, NSG associations, creating rules, checking effective rules, and using service tags.
I have three Linux VMs in a small lab. None of them have NSGs attached initially.
Because there are no NSGs attached, there is no NSG-level filtering. Inbound connectivity depends on whether the VM has a public IP and whether the guest OS firewall allows the traffic. If no inbound rule exists (and the OS firewall blocks the port), attempts to connect may time out:
Copy
# Attempt to SSH to the VM's public IP (no inbound NSG rule yet)ssh kodeloud@172.191.36.135# (connection attempt times out or hangs)
Create an NSG (for example nsg-lab-01) in the same region as your VMs and open its Rules page. NSGs include default system rules with high numeric priorities such as 65000, 65001, and 65500.
Default inbound rules typically include:
Default inbound rule
Purpose
AllowVNetInbound
Allows traffic from within the same virtual network
AllowAzureLoadBalancerInbound
Allows Azure load balancer probe traffic
DenyAllInbound
Denies all other inbound traffic by default
Default outbound rules usually allow VNet and Internet outbound, so new VMs can reach the internet until outbound rules are changed.
You can associate NSGs to a subnet (affects all NICs in the subnet) or directly to a NIC (affects a single VM). In this demo we associate the NSG to the subnet containing the VMs.
Even with the NSG associated to the subnet, inbound SSH from the internet is blocked until you add an inbound rule to allow it.Add an inbound security rule with these settings:
Source: Any (or restrict to My IP)
Source port range: *
Destination: Any (or specify subnet/NIC)
Service: SSH (auto-fills TCP/22)
Action: Allow
Priority: 100 (lower = higher precedence)
Name: AllowAnySSHInbound
The portal will show a caution icon if a rule allows Any source to SSH, since that exposes the VM to the internet — limit it in production.
Now you can SSH to any VM in that subnet because the subnet-level rule permits port 22:
Copy
# Connect to a VM in the subnet after adding the allow rulessh kodekloud@172.191.36.135# Accept host key and enter password when prompted
Note that a subnet-level rule affects all VMs in the subnet.
The VM’s effective rules are the combination of the subnet NSG and the NIC NSG.
Evaluation happens by priority: lower numeric priorities are evaluated first. If an earlier rule matches and denies traffic, it will block the flow even if another NSG (at the other level) would allow it.
To demonstrate, create a second NSG and attach it to the NIC of vm-nsg-lab-3 while SSH remains allowed at the subnet level. Inspect the VM’s Networking pane and view the effective security rules.
If the NIC-level NSG contains a Deny rule for SSH while the subnet NSG allows SSH, the NIC-level deny will block SSH to that particular VM. Use the portal’s Effective Security Rules view to identify which rule (and which NSG) is allowing or blocking traffic.
Service tags: allow Storage while blocking general Internet
Service tags simplify allowing or blocking traffic to Azure services. In this scenario we will restrict outbound Internet access but permit outbound traffic to Azure Storage using the Storage service tag.Open your storage account and confirm it’s accessible via public endpoints.
Open a blob container and pick a file to download (example: a small image in the vision container).
From a VM that currently has outbound Internet access, you can verify the blob is reachable with curl:
Copy
# From a VM with outbound Internet allowedcurl -I https://cskodekloudaz01.blob.core.windows.net/vision/note.jpeg# or download:curl -o note.jpeg https://cskodekloudaz01.blob.core.windows.net/vision/note.jpeg
Next, modify the NSG outbound rules for the VM/subnet:
Add a Deny outbound rule with destination service tag = Internet, all ports, and a relatively high precedence (for example priority 200) to block general Internet access.
Remember that default Allow outbound rules may exist at higher numeric priorities (lower precedence). Your Deny at 200 will be evaluated before those defaults.
After adding the Deny Internet outbound rule, general outbound connectivity fails:
Copy
# This should now fail due to the Deny Internet outbound rulecurl https://www.microsoft.com# No response or connection times out
To allow only Storage while keeping the rest of the Internet blocked, create an Allow outbound rule that targets the Storage service tag and the appropriate ports (usually 443 for HTTPS). Specify needed ports as comma-separated values (for example 443,80) or ranges.
Add the Allow rule for Storage with a higher precedence (lower numeric priority, for example 100) than the Deny Internet rule. After adding the rule, you should be able to download blobs from your storage account while general Internet access remains blocked:
Copy
# Downloading blob should succeed because Storage outbound is allowedcurl -o note.jpeg https://cskodekloudaz01.blob.core.windows.net/vision/note.jpeg# General internet remains blockedcurl https://www.microsoft.com# This will fail or time out
You can also use explicit IP addresses or CIDR ranges as destinations. Azure accepts comma-separated entries like 10.1.1.0/24,10.1.2.0/24 when you need to allow specific internal ranges.
When NSGs are applied at both subnet and NIC levels, evaluation uses the combined set of rules. A Deny at either level can block traffic even if the other level has an Allow. Always verify effective security rules in the portal and design priorities so intended Allows are evaluated before Denys.
Service tags reduce operational overhead by removing the need to maintain large IP lists for Azure services. They are especially useful when you want to allow access to a specific Azure service (for example Storage) while blocking broader Internet access.
I hope this lesson clarifies how to create NSG rules, how priority and effective rule evaluation work, and how service tags help simplify NSG management.