NSGs & ASGs
rules · default rules · priority · evaluation order · will traffic be allowed · ASGs · effective rules
By the tools4free.dev team · Last reviewed November 13, 2026
Practice questions for this topic →
How NSGs work
- An NSG holds inbound and outbound security rules that allow or deny traffic. Rules are stateful: if a rule allows a flow, the reply needs no rule in the other direction.
- Rules are matched on the 5-tuple: source, source port, destination, destination port and protocol.
- Priority: 100 to 4096 for custom rules. A lower number is processed first, and processing stops at the first match. You can't create two rules with the same priority and direction.
- Rule properties: name (unique in the NSG, up to 80 characters), priority, source, destination (Any, IP, CIDR, service tag or ASG), protocol (TCP, UDP, ICMP, ESP, AH or Any), direction, port range, and action (Allow or Deny). Description: up to 140 characters.
- Removing or changing a rule affects only new connections. An open session, such as SSH, stays up.
- Each subnet and each NIC has zero or one NSG. One NSG can be associated with many subnets and NICs. Microsoft recommends associating the NSG with either the subnet or the NIC, not both, to avoid overlapping rules.
- For inbound traffic NSGs see the private IP (after public-to-private translation). For outbound traffic NSGs process before translation. Rules reference the VM's private IP, not its public IP.
- A subnet NSG that denies all inbound and outbound traffic stops VMs in that subnet from talking to each other.
Default rules (in every NSG)
| Direction | Name | Priority | Source | Destination | Access |
|---|---|---|---|---|---|
| Inbound | AllowVNetInBound | 65000 | VirtualNetwork | VirtualNetwork | Allow |
| Inbound | AllowAzureLoadBalancerInBound | 65001 | AzureLoadBalancer | 0.0.0.0/0 | Allow |
| Inbound | DenyAllInbound | 65500 | 0.0.0.0/0 | 0.0.0.0/0 | Deny |
| Outbound | AllowVnetOutBound | 65000 | VirtualNetwork | VirtualNetwork | Allow |
| Outbound | AllowInternetOutBound | 65001 | 0.0.0.0/0 | Internet | Allow |
| Outbound | DenyAllOutBound | 65500 | 0.0.0.0/0 | 0.0.0.0/0 | Deny |
- All default rules use ports 0-65535 and protocol Any. They can't be removed or edited, but a custom rule with a lower number overrides them. Because custom rules (100-4096) come before 65000, they are always evaluated first.
- Result of the defaults alone: inbound from VirtualNetwork or AzureLoadBalancer is allowed, inbound from the Internet is denied, outbound to VirtualNetwork and Internet is allowed, and everything else is denied.
- The
VirtualNetworktag includes peered vnets' prefixes when peering allows access.
Evaluation order: will this traffic be allowed?
| Direction | First NSG | Second NSG |
|---|---|---|
| Inbound | Subnet NSG (if any) | NIC NSG (if any) |
| Outbound | NIC NSG (if any) | Subnet NSG (if any) |
- 1. Pick the direction and the order above. The same order applies to traffic inside a subnet.
- 2. Inside each NSG, sort the rules for that direction by priority number, lowest first. Custom rules come first, then 65000, 65001 and 65500. The first rule that matches decides, Allow or Deny.
- 3. A Deny in the first NSG ends it: the second NSG never sees the traffic. An Allow passes the traffic to the second NSG, which must also allow it.
- 4. If a level has no NSG, that level doesn't filter.
- 5. Return traffic of an allowed flow is allowed automatically.
- 6. Virtual Network Manager security admin rules are evaluated before NSG rules. Allow continues on to NSG evaluation. Always allow and Deny end evaluation.
Worked example from Microsoft: the subnet NSG has no rule that allows port 80, and the NIC NSG allows port 80 from the Internet. Inbound port 80 is denied at the subnet NSG (DenyAllInbound). The NIC NSG never sees it. Both must allow.
No NSG and public IPs
- A VM with a Standard public IP is secure by default. With no NSG on the subnet or NIC, inbound internet traffic is blocked. To let it in, associate an NSG that allows it.
- A Basic public IP (retired Sep 30, 2025) is open by default, and an NSG is optional. Microsoft's "diagnose a traffic filter problem" article says that with no NSG and a public IP, all ports stay open, without naming the SKU. The articles that name the SKU agree with the rule above: the SKU decides.
Service tags, augmented rules and platform traffic
- Service tags (
VirtualNetwork,AzureLoadBalancer,Internet,Storageand so on) are Microsoft-maintained IP groups. You can't put multiple service tags or multiple ASGs in one rule's source or destination. - Augmented rules allow multiple IPs, ranges and ports in one rule.
- DHCP, DNS, IMDS and health monitoring use 168.63.129.16 and 169.254.169.254. NSGs don't filter these by default. To filter them, target the
AzurePlatformDNS,AzurePlatformIMDSorAzurePlatformLKMtags. - Outbound SMTP on port 25: Enterprise Agreement subscriptions aren't blocked (Dev/Test is blocked by default). Pay-as-you-go, MSDN, Azure Pass, Free and CSP are blocked and can't be unblocked. Use an authenticated SMTP relay, for example on port 587.
- NSG flow logs retire on Sep 30, 2027. New NSG flow log creation is already blocked. Migrate to virtual network flow logs.
Application security groups (ASGs)
- An ASG groups NICs by application role. Use the ASG as the source or destination of a rule instead of IP addresses.
- A NIC can be in several ASGs (up to 20 per IP configuration). All NICs in one ASG must be in the same virtual network. If ASGs are the source and the destination of one rule, both must be in the same vnet.
- A NIC that isn't a member isn't affected by the rule, even if the NSG is on its subnet.
- An ASG's location can't be changed. An ASG that still has NICs can't be deleted.
- Create and use: create the ASG (in a region), assign NICs through the NIC's settings (the ASG must be in the same region as the NIC), then reference the ASG in rules.
| Learn example priority | Rule | Effect |
|---|---|---|
| 100 | Allow Internet to AsgWeb, TCP 80 | Web tier is reachable from the Internet |
| 110 | Allow AsgLogic to AsgDb, TCP 1433 | Logic tier reaches the database |
| 120 | Deny any to AsgDb, TCP 1433 | Blocks everything else. Needed because AllowVNetInBound would allow in-vnet traffic |
Configure, associate and diagnose
- Create a rule: give Source, Source port ranges, Destination, Service or Destination port ranges, Protocol, Action, Priority, Name and Description. Leave gaps in priorities, such as 100, 200 and 300.
- Associate: NSG › Subnets › Associate, or
az network vnet subnet update --network-security-group. An NSG with associated subnets or NICs can't be deleted; dissociate first. - Fix a blocked port: create an Allow rule with a priority lower than the denying rule. If NSGs are on both the subnet and the NIC, create it in both.
- Effective security rules: the aggregate of the rules from the NIC NSG and the subnet NSG for a NIC. The VM must be running and at least one NSG must be associated. The portal shows only the first 50 rules (download for all). Rule names start with
defaultSecurityRules/for defaults andsecurityRules/for custom rules. Duplicates appear when the NIC and subnet NSGs have the same rule. - Effective security rules vs IP flow verify: the first lists the aggregated rules for a NIC. IP flow verify tests a specific packet and names the rule.
- Permissions: Network Contributor, or
networkSecurityGroups/read,write,delete,join/action(associate an NSG with a subnet or NIC),securityRules/*, and for ASGsapplicationSecurityGroups/joinIpConfiguration/actionandjoinNetworkSecurityRule/action.
Limits
| Item | Value |
|---|---|
| Custom rule priority | 100-4096 |
| Default rule priorities | 65000, 65001, 65500 |
| NSGs per subscription per region | 5,000 |
| Rules per NSG | 2,000 |
| IPs and ranges per source or destination in a rule | 6,000 |
| ASGs per subscription | 3,000 |
| ASGs per NIC IP configuration | 20 |
| ASGs in a single rule (source or destination) | 10 |
| ASGs across all rules of one NSG | 100 |
| IP configurations per ASG | 4,000 |
CLI and PowerShell
| Task | Azure CLI | PowerShell |
|---|---|---|
| Create NSG | az network nsg create -g myResourceGroup -n myNSG | New-AzNetworkSecurityGroup -Name myNSG -ResourceGroupName myResourceGroup -Location eastus |
| Create rule | az network nsg rule create -g myRG --nsg-name myNSG -n RDP-rule --priority 300 --destination-port-ranges 3389 --protocol Tcp --description "Allow RDP" | Add-AzNetworkSecurityRuleConfig ..., then Set-AzNetworkSecurityGroup |
| Update rule | az network nsg rule update ... --priority 200 | Set-AzNetworkSecurityRuleConfig, then Set-AzNetworkSecurityGroup |
| Associate to a subnet | az network vnet subnet update -g myRG --vnet-name myVNet -n mySubnet --network-security-group myNSG | Set-AzVirtualNetworkSubnetConfig -NetworkSecurityGroup, then Set-AzVirtualNetwork |
| Create ASG | az network asg create -g myRG -n myASG -l eastus | New-AzApplicationSecurityGroup |
| Effective security rules | az network nic list-effective-nsg --name vm-nic -g test-rg | Get-AzEffectiveNetworkSecurityGroup -NetworkInterfaceName vm-nic -ResourceGroupName test-rg |
Exam Tips
Tick each tip once you can answer it without looking.
“Inbound rules have no Allow for port 80, and an internet request fails” → DenyAllInbound (65500) blocks it: add an Allow rule with a priority below 65500, such as 100. Trap: Editing DenyAllInbound. Default rules can't be edited.
“Allow rule at priority 200, Deny rule at priority 100, same traffic” → Deny wins: 100 is processed first. Trap: Allow, because it was created later.
“Two rules with the same priority and direction” → Not allowed. Trap: The first one created wins.
“Custom rule priority range” → 100-4096. Trap: 1-65535.
“Subnet NSG allows inbound 443; the NIC NSG has no allow for 443” → Denied at the NIC NSG by DenyAllInbound: both must allow. Trap: Allowed because the subnet NSG allows it.
“Outbound: which NSG is evaluated first” → The NIC NSG, then the subnet NSG. Trap: Subnet first. That's the inbound order.
“VM in a vnet connects to a VM in a peered vnet with default NSG rules” → Allowed: AllowVNetInBound and AllowVnetOutBound, because the VirtualNetwork tag includes peered vnets. Trap: Denied until a rule is added.
“Outbound to the internet from a VM with only default rules” → Allowed (AllowInternetOutBound, 65001). Trap: Denied by default.
“Delete the AllowVnetInBound default rule” → Not possible: override it with a lower-numbered Deny rule. Trap: Deleting it in the portal.
“Remove the allow-SSH rule while a session is open” → The existing session stays; new connections are blocked. Trap: The session dropping immediately.
“Allow the response to an allowed outbound web request” → No inbound rule needed: NSGs are stateful. Trap: Adding an inbound ephemeral port rule.
“Web and DB VMs in one subnet; only the app tier may reach the DB” → ASGs: Allow AsgLogic to AsgDb on 1433 (110), then Deny any to AsgDb on 1433 (120). Trap: Listing individual VM IPs.
“A NIC in vnet2 is added to an ASG whose first NIC is in vnet1” → Not allowed: all NICs in an ASG must be in the same vnet. Trap: Thinking peering makes it allowed.
“Delete an ASG that still has NICs” → Blocked: remove the NICs first. Trap: Cascade delete.
“See the combined subnet and NIC NSG rules for a VM” → Effective security rules on the NIC (the VM must be running). Trap: Opening the subnet NSG only.
“Rule must target a VM that has a public IP” → Use the VM's private IP: NSGs see the private IP for inbound traffic. Trap: The public IP.
“Role needed to associate an NSG with a subnet” → Microsoft.Network/networkSecurityGroups/join/action (in Network Contributor). Trap: Reader on the NSG.
“Block DNS or IMDS platform traffic with an NSG” → Use the AzurePlatformDNS or AzurePlatformIMDS service tags: these aren't filtered by default. Trap: Denying 168.63.129.16 as an IP.
“Send mail directly on port 25 from a pay-as-you-go VM” → Blocked: use an authenticated SMTP relay. Trap: Opening port 25 in the NSG.
“VM has a Standard public IP and no NSG on the subnet or NIC; inbound from the internet” → Blocked: Standard is closed until an NSG allows the traffic. Trap: Assuming no NSG means everything is open. That describes a Basic IP.
Sources
- Azure network security groups overview
- How network security groups filter network traffic
- Azure application security groups overview
- Create, change, or delete network security groups
- Diagnose a VM network traffic filter problem
- Network Watcher IP flow verify
- Diagnose a VM traffic filter problem (Network Watcher)
- Public IP addresses in Azure
- Create, change, or delete virtual network peering
- Azure subscription and service limits
Checked against Microsoft Learn on November 13, 2026.