Network Security Groups
NSG rules and priority · default rules · subnet and NIC evaluation order · ASGs · virtual network flow logs · IP flow verify · Bastion NSG rules · security admin rules
By the tools4free.dev team · Last reviewed November 13, 2026
Practice questions for this topic →
Security rules
| Property | What to know |
|---|---|
| Name | Unique within the NSG; up to 80 characters. |
| Priority | 100 to 4096. Lower number = higher priority. Once traffic matches a rule, processing stops. You can't create two rules with the same priority and direction. |
| Source / destination | Any, an IP address, a CIDR block, a service tag or an application security group. To name an Azure VM, use its private IP address. |
| Protocol | TCP, UDP, ICMP, ESP, AH or Any. ESP and AH aren't available in the portal; use an ARM template. |
| Direction | Inbound or outbound. |
| Port range | One port, a range, or a comma-separated mix such as 80, 10000-10005. |
| Action | Allow or Deny. |
- Five-tuple: rules are matched on source, source port, destination, destination port and protocol.
- Stateful: a flow record is kept for existing connections. An outbound allow rule for port 80 needs no inbound rule for the response, and an inbound allow needs no outbound rule for the reply.
- Rule changes hit new connections only. If you remove the rule that allowed an SSH session, the open session stays up; a new connection is blocked.
- Private addresses: for inbound traffic the NSG processes traffic after Azure translates the public IP to the private IP; for outbound traffic it processes before the translation. Write rules against private IPs.
- Augmented rules allow several IP addresses, ranges and ports in one rule. You still can't put several service tags or several application security groups in one rule.
- Service tag: a group of IP address prefixes from a given Azure service. It helps minimize the complexity of frequent updates to network security rules.
- Portal rule settings: priority is a value from 100 to 4096 unique within the NSG; leave gaps between priorities (100, 200, 300) to add rules later; the description can't exceed 140 characters; the protocol defaults to Any.
- Platform traffic: DHCP, DNS, IMDS and health monitoring use the host addresses 168.63.129.16 and 169.254.169.254. By default these aren't subject to your NSGs unless you target them with the service tags
AzurePlatformDNS,AzurePlatformIMDSorAzurePlatformLKM. - Outbound port 25: Microsoft recommends an authenticated SMTP relay (typically TCP 587). On pay-as-you-go subscriptions outbound port 25 is blocked from all resources and can't be unblocked.
Permissions: to manage NSGs, rules and application security groups you need the Network Contributor role (or a custom role). Associating an NSG to a subnet or NIC uses the Microsoft.Network/networkSecurityGroups/join/action permission.
Default rules and evaluation order
Azure creates six default rules in every NSG. They have the lowest priority (highest numbers), so your rules are always processed first. You can't remove them, but a rule with a higher priority overrides them.
| 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 |
Subnet NSG and NIC NSG
| Direction | First | Second |
|---|---|---|
| Inbound | NSG on the subnet | NSG on the network interface |
| Outbound | NSG on the network interface | NSG on the subnet |
- Both must allow. For inbound traffic that is denied by the subnet NSG, the NIC NSG never sees it. To allow port 80 to a VM that has an NSG on both levels, both NSGs need a rule that allows it.
- Only one level associated: traffic is filtered by that NSG alone. An NSG on a subnet applies to every resource in the subnet, including traffic between VMs in the same subnet.
- No NSG at all: a VM with a standard public IP address is secure by default; for traffic to flow from the internet, an NSG must be associated with the subnet or the NIC. Outbound traffic from a VM with no NSG on its NIC or subnet flows freely.
- Intra-subnet traffic: VMs in one subnet can talk by default, through a default NSG rule that allows intra-subnet traffic. A subnet-NSG rule that denies all inbound and outbound traffic stops that.
- Best practice: avoid associating NSGs with both a subnet and its NICs. Rules at several levels can conflict and are hard to troubleshoot.
- Reuse and delete: the same NSG can be associated with as many subnets and NICs as you like. You can't delete an NSG that is still associated; dissociate it first.
- Service subnets: several Azure services deploy into subnets. Check a service's port requirements before you apply an NSG; if you deny the ports it needs, the service doesn't work.
Check what applies
- Effective security rules for a NIC show the aggregate rules applied to it. The portal lists them if the NIC is attached to a running VM and associated with an NSG.
- IP flow verify (Network Watcher) tells you whether a packet is allowed or denied to or from a VM, based on its direction, protocol, local and remote IP, and local and remote port. It returns Access allowed or Access denied and the name of the rule and NSG responsible. It gives no link to the NSG when a default security rule decides.
- IP flow verify looks at all NSGs applied to the NIC (subnet or NIC level) and at the Virtual Network Manager rules on the VM's virtual network.
- Limits of IP flow verify: it needs a Network Watcher instance in the VM's subscription and region; it tests TCP and UDP rules only (use NSG diagnostics for ICMP); and it tests rules on the VM's NIC only (use NSG diagnostics for virtual machine scale sets).
Application security groups
An application security group (ASG) lets you group VMs and write rules against the group instead of IP addresses. Security follows the application's structure and you don't maintain address lists.
- Membership: NICs are members of an ASG. A NIC can belong to several ASGs, up to Azure's limits.
- Same virtual network: every NIC in an ASG must be in the virtual network of the first NIC added. You can add a NIC only to an ASG in the same virtual network and location as the NIC.
- Source and destination both ASGs: the NICs in both groups must be in the same virtual network.
- Scope of a rule: a rule that names an ASG applies only to NICs that are members. If a NIC isn't in the ASG, the rule doesn't apply to it, even though the NSG is associated with the subnet.
- Create and change: an ASG is created in a region; you can't change its location. You can't delete an ASG that still contains network interfaces.
- Portal limit: you can add or remove a NIC from an ASG in the portal only if the NIC is attached to a VM; otherwise use PowerShell or the CLI.
Example rule set (three tiers behind one subnet NSG)
| Priority | Rule | Source | Destination | Port | Action |
|---|---|---|---|---|---|
| 100 | Allow-HTTP-Inbound-Internet | Internet | AsgWeb | 80 TCP | Allow |
| 110 | Allow-Database-BusinessLogic | AsgLogic | AsgDb | 1433 TCP | Allow |
| 120 | Deny-Database-All | * | AsgDb | 1433 Any | Deny |
Why rule 120 is needed: AllowVNetInBound allows all traffic inside the virtual network. Rule 110 has a higher priority than rule 120, so AsgLogic reaches AsgDb and everything else is denied. No rule is needed to block the internet from AsgLogic or AsgDb, because DenyAllInbound already does.
Virtual network flow logs
Virtual network flow logs are a Network Watcher feature. They log IP traffic through a virtual network and write it to Azure Storage. They overcome some limitations of NSG flow logs, which are retiring.
| Setting | Requirement |
|---|---|
| Target | Create a flow log for a virtual network, a subnet or a network interface. |
| Resource provider | Microsoft.Insights must be registered. |
| Storage account | Same region as the virtual network; same subscription or a subscription in the same Microsoft Entra tenant; standard performance (premium isn't supported). |
| Retention | Set in days; 0 keeps data until you delete it. Retention days need a Standard general-purpose v2 account. |
| Traffic analytics | Optional. Processing interval is every 1 hour (default) or every 10 minutes; needs a Log Analytics workspace. |
| Customer-managed keys | If you change or rotate the keys on the storage account, flow logs stop working. Disable and re-enable them. |
- Versus NSG flow logs: you enable logging at the virtual network, so you don't enable it at several levels (subnet NSG and NIC NSG). Virtual network flow logs also record traffic that Virtual Network Manager security admin rules allow or deny, and the encryption status of traffic.
- Avoid duplicates: disable NSG flow logs before you enable virtual network flow logs on the same workloads, to avoid duplicate records and extra cost.
- Gateways: because logging is at the virtual network, traffic through resources such as gateways is captured by default, which can raise the log volume.
- Cost: charged per GB of network flow logs collected, with a free tier of 5 GB per month per subscription. Traffic analytics and storage are billed separately.
- Not supported: some services don't support virtual network flow logs, among them Azure Container Instances, Azure Functions, App Service, Azure DNS Private Resolver and Azure SQL Managed Instance.
- Blob operations: don't edit, overwrite or delete a flow log blob while logging into it; that can make later writes for that hour's blob fail.
Read a flow log
- Logs work at Layer 4, are collected at one-minute intervals, are written as JSON, and don't affect your resources or traffic.
- Each record holds the NIC, the 5-tuple, the direction, the flow state, the encryption state and throughput.
aclIDnames the resource that evaluated the traffic (an NSG or Virtual Network Manager).rulenames the rule that allowed or denied it.- A platform rule entry is traffic the Azure platform processed itself, not a rule you configured. It doesn't change traffic behavior; filter it out if you only analyze your own rules.
| Tuple field | Values |
|---|---|
| Flow direction | I inbound, O outbound |
| Flow state | B begin, C continuing, E end, D deny |
| Flow encryption | X encrypted; NX unencrypted (the default); other NX_ codes explain why encryption wasn't applied |
| Packets and bytes | For C and E records, the counts are aggregates since the previous record of the flow. Add the records to get the conversation total. |
Sample from the page: 1663145998065,101.33.218.153,10.0.0.6,55188,22,6,I,D,NX,0,0,0,0 reads as inbound (I), denied (D), unencrypted, from 101.33.218.153 port 55188 to 10.0.0.6 port 22, protocol 6, with zero packets and bytes.
Retirement: NSG flow logs retire on September 30, 2027 and no longer support new NSG flow log creation. After that date Azure deletes existing NSG flow log resources and drops traffic analytics for them; the records already in storage are kept under their retention policy. Migrate to virtual network flow logs.
NSG rules for Azure Bastion
Azure Bastion supports NSGs on the AzureBastionSubnet and on the target VM subnets. If you apply an NSG to the Bastion subnet you must create every required rule below. Leaving one out blocks platform updates and can block VM connectivity.
| Rule | Direction | Source | Destination | Port | Purpose |
|---|---|---|---|---|---|
| AllowHttpsInbound | Inbound | Internet | * | 443 | Users reach Bastion over HTTPS. Source can be Internet or specific public IPs. |
| AllowGatewayManagerInbound | Inbound | GatewayManager | * | 443 | Control plane talks to the Bastion host. |
| AllowBastionHostCommunication | Inbound | VirtualNetwork | VirtualNetwork | 8080, 5701 | Bastion components talk to each other. |
| AllowAzureLoadBalancerInbound | Inbound | AzureLoadBalancer | * | 443 | Load balancer health probes. |
| AllowSshRdpOutbound | Outbound | * | VirtualNetwork | 22, 3389 | Bastion reaches the target VMs over private IP. |
| AllowAzureCloudOutbound | Outbound | * | AzureCloud | 443 | Diagnostics and metering logs. |
| AllowBastionCommunication | Outbound | VirtualNetwork | VirtualNetwork | 8080, 5701 | Bastion components talk to each other. |
| AllowHttpOutbound | Outbound | * | Internet | 80 | Session validation, shareable link, certificate validation. |
- Ports 3389 and 22 aren't needed inbound on the AzureBastionSubnet, and the target VM needs no public IP address.
- Custom port feature: if you use it with the Standard SKU, allow outbound traffic to the
VirtualNetworkservice tag instead of fixed 22 and 3389. - Target VM subnet: add an inbound rule for ports 3389 and 22 (or your custom ports) with the AzureBastionSubnet range as the source. Without it, Bastion can't reach your VMs even when it is configured correctly. Scoping the source to that range means only Bastion can open the ports, not the wider internet.
Security admin rules (Azure Virtual Network Manager)
Security admin rules are global rules that a central team deploys with Azure Virtual Network Manager. They apply to virtual networks in a network group and are evaluated before NSG rules.
| Action | What happens next |
|---|---|
| Allow | Evaluated first; then traffic continues to NSG rules, which can still allow or deny it. |
| Always allow | Evaluation stops. Traffic goes to the resource and NSG rules can't block it. |
| Deny | Evaluation stops. Traffic is blocked and not delivered to the destination. |
- Scope: security admin rules apply at the virtual network level; NSGs apply at the subnet and NIC level. They can only apply to virtual networks the network manager manages.
- Priority: an integer from 1 to 4,096; lower number wins.
- Fields: priority, action (Allow, Deny, Always allow), direction, protocol (TCP, UDP, ICMP, ESP, AH or any), and optional source and destination (IP addresses or service tags) and ports.
- One configuration per region: you can deploy only one security admin configuration to a region. To get several sets of rules, create several rule collections in that configuration.
- Deploy to take effect: configurations don't apply until you deploy them to the regions that hold your target virtual networks. Rules are applied with eventual consistency, after a short delay, and new resources in the virtual network get them too.
- Network group workflow: create the network manager and its scope, create network groups, create a security admin configuration with rule collections, associate the rule collections with network groups, then deploy.
- Not applied by default to virtual networks that contain Azure SQL Managed Instances or Azure Databricks. An
AllowRulesOnlysetting applies only the Allow rules to such networks. - Not applied at the subnet level to subnets of Application Gateway, Bastion, Azure Firewall, Route Server, VPN Gateway, Virtual WAN and ExpressRoute Gateway. Other subnets in the same virtual network still get the rules.
- Other gaps: security admin rules don't apply to private endpoints in the scope of a managed virtual network, and don't support the service tags
AzurePlatformDNS,AzurePlatformIMDSandAzurePlatformLKM. - Typical use: block high-risk ports such as 22 and 3389 from the internet, enforce compliance, and enforce segmentation, with exceptions where needed.
CLI and PowerShell
| Task | Azure CLI | PowerShell |
|---|---|---|
| Create an NSG | az network nsg create --resource-group myResourceGroup --name myNSG | New-AzNetworkSecurityGroup @NSGParams |
| Add an NSG rule | az network nsg rule create --resource-group myResourceGroup --nsg-name myNSG --name RDP-rule --priority 300 --destination-address-prefixes '*' --destination-port-ranges 3389 --protocol Tcp --description "Allow RDP" | Add-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $networkSecurityGroup @RuleParams |
| Associate an NSG with a subnet | az network vnet subnet update --resource-group myResourceGroup --vnet-name myVNet --name mySubnet --network-security-group myNSG | Set-AzVirtualNetworkSubnetConfig -VirtualNetwork $virtualNetwork @SubnetParams |
| Associate an NSG with a network interface | az network nic update --name myNIC --resource-group myResourceGroup --network-security-group myNSG | $nic.NetworkSecurityGroup = $nsg; $nic | Set-AzNetworkInterface |
| Create an application security group | az network asg create --resource-group myResourceGroup --name myASG --location eastus | New-AzApplicationSecurityGroup @ASGParams |
| Add a NIC to an application security group | az network nic ip-config update --name ipv4config --nic-name myNIC --resource-group myResourceGroup --application-security-groups myASG | $nic | Set-AzNetworkInterfaceIpConfig @IP; $nic | Set-AzNetworkInterface |
| View a NIC's effective security rules | az network nic list-effective-nsg --name myNIC --resource-group myResourceGroup | Get-AzEffectiveNetworkSecurityGroup -NetworkInterfaceName myNIC -ResourceGroupName myResourceGroup |
| Register the Insights provider (needed for flow logs) | az provider register --namespace Microsoft.Insights | Register-AzResourceProvider -ProviderNamespace Microsoft.Insights |
| Create a virtual network flow log | az network watcher flow-log create --location 'eastus' --resource-group 'myResourceGroup' --name 'myVNetFlowLog' --vnet 'myVNet' --storage-account 'myStorageAccount' | New-AzNetworkWatcherFlowLog -Enabled $true -Name 'myVNetFlowLog' -NetworkWatcherName 'NetworkWatcher_eastus' -ResourceGroupName 'NetworkWatcherRG' -StorageId $storageAccount.Id -TargetResourceId $vnet.Id -FormatVersion 2 |
Exam Tips
Tick each tip once you can answer it without looking.
“Which NSG is evaluated first for inbound traffic when a subnet NSG and a NIC NSG both exist?” → The subnet NSG, then the NIC NSG. Trap: Outbound is the reverse (NIC first). Both must allow the traffic.
“Which NSG is evaluated first for outbound traffic when both levels have an NSG?” → The NIC NSG, then the subnet NSG. Trap: Don't reuse the inbound order; it is reversed.
“Two rules at priorities 200 (Deny TCP 443) and 300 (Allow TCP 443) both match” → Priority 200 wins; the Deny applies and processing stops. Trap: A higher number is a lower priority, so the Allow at 300 is never reached.
“Add a rule with the same priority and direction as an existing rule” → Not possible; priority must be unique per direction in an NSG. Trap: Two rules with the same priority and direction can conflict.
“Default rules block inbound internet traffic to a VM; what do you create to allow it?” → A custom allow rule (priority 100 to 4096) that matches the traffic. Trap: You can't delete defaults, but you can override them with higher-priority rules.
“Allow web VMs to reach database VMs without IP lists, as the VMs scale” → Application security groups as source and destination. Trap: All NICs in a rule's ASGs must be in the same virtual network; an ASG can't span VNets.
“Put NICs from two virtual networks in one ASG” → Not possible. Trap: Every NIC in an ASG must be in the VNet of the first NIC added.
“You remove the rule that allowed an active SSH session” → The open session stays up; new connections are blocked. Trap: Rule changes only affect new connections.
“Replace NSG flow logs” → Virtual network flow logs (enable at the virtual network, subnet or NIC). Trap: NSG flow logs retire September 30, 2027 and can't be newly created.
“Flow log storage account is in another region than the virtual network” → Not allowed; the account must be in the same region, with standard performance. Trap: Premium storage accounts aren't supported.
“A flow log tuple shows I,D” → Inbound flow that was denied. Trap: D is Deny; O would be outbound.
“Check which rule blocks traffic to a VM” → IP flow verify (Network Watcher). Trap: It tests TCP and UDP only; use NSG diagnostics for ICMP or scale sets.
“Bastion NSG on AzureBastionSubnet: which inbound rule carries the control plane?” → 443 inbound from the GatewayManager service tag. Trap: Missing rules block platform updates; 22 and 3389 aren't needed inbound on this subnet.
“Target VM subnet NSG must let Bastion connect” → Inbound 22 and 3389 from the AzureBastionSubnet range. Trap: Don't open them to the internet; the VM needs no public IP.
“A central team must block RDP on every VNet, even where app teams have allowed it in NSGs” → A Virtual Network Manager security admin rule with the Deny action. Trap: Security admin rules are evaluated before NSG rules, and a Deny ends evaluation. An Allow action would pass on to the NSGs.
“Security admin rule action that overrides a conflicting NSG deny” → Always allow. Trap: Allow continues to NSG rules, which can still deny it.
“Deploy a second security admin configuration to the same region” → Not possible; add another rule collection to the existing configuration. Trap: One security admin configuration per region.
Sources
- Azure network security groups overview
- How network security groups filter network traffic
- Application security groups overview
- Create, change, or delete network security groups
- Create, change, or delete a network interface
- Virtual network flow logs overview
- Manage virtual network flow logs
- NSG flow logs overview (retirement notice)
- IP flow verify overview
- Configure NSG rules for Azure Bastion
- Security admin rules in Azure Virtual Network Manager
- What is Azure Virtual Network Manager?
- NSG diagnostics overview
Checked against Microsoft Learn on November 13, 2026.