Your profile

One profile for every MSDEVBUILD site. Only you can see it, and the site owner if you write in.

Lesson 35 of 47
❮Prev Next❯

NSGs, Application Security Groups and Effective Rules

Module 4: Networking ⏱ 12 min read Updated

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    30-second answer

    A network security group (NSG) holds stateful allow and deny rules (source, destination, port, protocol), checked by priority from 100 to 4096, lowest number first; the first match wins. Every NSG has default rules at 65000 and above (allow VNet, allow Azure Load Balancer, deny all inbound). You attach an NSG to a subnet, a NIC, or both. For inbound traffic the subnet NSG is evaluated first, then the NIC NSG; outbound runs the other way, and both must allow. An application security group (ASG) groups NICs so rules can target "the web servers" instead of IP addresses. Effective security rules and IP flow verify show what really applies.

    Simulate it » No Azure account needed

    Exam objective: 4.2.1 Create and configure network security groups (NSGs) and application security groups. 4.2.2 Evaluate effective security rules in NSGs. (Skills measured as of April 17, 2026)

    Simulations

    Feel it before you read it. No account needed, nothing leaves this page.

    Azure portal: Write NSG rules against an ASG and read the effective rules Write a rule against an ASG and read the effective rules.
    </> Embed

    Embed this simulation

    Free for trainers, teachers and bloggers. Paste this into your page:

    Loading the portal…

    Search for <strong>application security groups</strong>.

    👆 Click the glowing item in the portal to continue

    Read the steps as text
    1. Search for application security groups.
    2. Type application security groups in the search bar and pick Application security groups under Services.
    3. An ASG is just a label for NICs. Create asg-web in the same region as the VNet.
    4. On nic-web01, open Application security groups and add asg-web. All NICs in an ASG must be in the same VNet.
    5. In nsg-snet-web, add an inbound rule whose Destination is the ASG, not an IP list. New web VMs are covered the moment they join asg-web.
    6. Lowest number wins. Anything not allowed falls to DenyAllInBound at 65500. Default rules can't be deleted, only overridden by a lower number.
    7. Effective security rules shows every NSG that applies. Inbound, the subnet NSG is checked first, then the NIC NSG, and both must allow. Here the NIC NSG blocks 443.
    8. Network Watcher → IP flow verify names the exact rule. Add an allow for 443 on nsg-web01 (or remove that NSG) and verify again.

    🧪 Simulation for learning. Screens are simplified versions of the Azure portal; names, prices and IDs are examples. Nothing here touches real Azure.

    😟 THE PROBLEM

    Every new web server means editing five NSG rules with new IP addresses.

    😊 THE SOLUTION

    Group the NICs in an application security group and write rules against the group.

    • IP lists in every rule✓ Rules that target an ASG
    • Allowed on the subnet, still blocked✓ Check the NIC NSG too
    • Guessing which rule wins✓ Effective rules and IP flow verify
    🛠️ Solved with: NSGs and ASGs

    Start with a question

    Why does a visitor get past reception but still can't open the lab door?

    Reception checks the visitor list for the whole building. The lab has its own card reader with its own list. Being on reception’s list doesn’t put you on the lab’s.

    A subnet NSG is reception and a NIC NSG is the lab door. Traffic coming in has to pass both. And instead of writing every visitor’s name, you can write “anyone with a contractor badge”. That badge is an application security group.

    Explain it like I’m 10

    The technical explanation

    Rules

    Each rule has a name, priority (100 to 4096), source and destination (IP, CIDR, service tag or ASG), source and destination ports, protocol (TCP, UDP, ICMP or Any), direction and action (Allow or Deny). Azure checks rules from the lowest number up and stops at the first match.

    Default rules you can’t delete:

    DirectionNamePriorityEffect
    InboundAllowVnetInBound65000Allow from the VirtualNetwork tag (your VNet, peered VNets, on-premises via gateway)
    InboundAllowAzureLoadBalancerInBound65001Allow health probes from 168.63.129.16
    InboundDenyAllInBound65500Deny everything else
    OutboundAllowVnetOutBound65000Allow to VirtualNetwork
    OutboundAllowInternetOutBound65001Allow to Internet
    OutboundDenyAllOutBound65500Deny everything else

    NSGs are stateful: when inbound traffic is allowed, its replies go out without an outbound rule. An NSG must be in the same region as what it’s attached to, and one NSG can be attached to many subnets and NICs.

    Subnet NSG plus NIC NSG

    1. 1 Subnet NSG

      Checked first. Deny here and the packet is dropped.

    2. 2 NIC NSG

      Checked second. It must also allow the traffic.

    3. 3 VM

      Only traffic both NSGs allow arrives. Outbound runs NIC first, then subnet.

    Inbound packet to a VM with NSGs on both levels

    Most designs put NSGs on subnets and avoid NIC NSGs unless one VM really needs different rules. Two layers make troubleshooting harder.

    Application security groups

    Create an ASG (for example asg-web), add NICs to it, and use it as the source or destination in NSG rules: Internet → asg-web : 443 allow, asg-web → asg-db : 1433 allow. New web servers just join the ASG. Limits: all NICs in an ASG are in the same VNet, and if a rule uses ASGs for both source and destination, they must be in the same VNet.

    Effective security rules

    On a running VM’s NIC, Help → Effective security rules shows each NSG that applies (subnet and NIC) with its merged rules. Network Watcher → IP flow verify tests one flow and names the rule that allowed or denied it. NSG diagnostics does the same for a broader range of sources.

    Portal and CLI

    Portal: Network security groups → + Create, then Inbound security rules → + Add, then Subnets → Associate (or NIC → Network security group). Application security groups → + Create, then on the NIC Application security groups → Add.

    az network asg create -g rg-net-sec -n asg-web
    az network nic create -g rg-net-sec -n nic-web01 --vnet-name vnet-app --subnet snet-web --application-security-groups asg-web
    az vm create -g rg-net-sec -n web01 --image Ubuntu2204 --nics nic-web01
    az network nsg create -g rg-net-sec -n nsg-snet-web
    az network nsg rule create -g rg-net-sec --nsg-name nsg-snet-web -n Allow-HTTPS-to-web \
      --priority 200 --direction Inbound --access Allow --protocol Tcp \
      --source-address-prefixes Internet --destination-asgs asg-web --destination-port-ranges 443
    az network vnet subnet update -g rg-net-sec --vnet-name vnet-app -n snet-web --network-security-group nsg-snet-web
    az network nic list-effective-nsg -g rg-net-sec -n nic-web01
    az network watcher test-ip-flow -g rg-net-sec --vm web01 --direction Inbound --protocol TCP \
      --local 10.20.1.4:443 --remote 203.0.113.10:50000

    PowerShell: New-AzNetworkSecurityRuleConfig, New-AzNetworkSecurityGroup, New-AzApplicationSecurityGroup, Get-AzEffectiveNetworkSecurityGroup.

    Architect level Augmented rules and service tags

    A single rule can list several IP ranges, ports and service tags (augmented security rules), so you rarely need dozens of near-identical rules. Service tags such as Storage.EastUS, Sql, AzureMonitor or GatewayManager are kept up to date by Microsoft, which is why they beat hard-coded IP lists for outbound rules to Azure services.

    Real-world business scenario

    Healthcare

    Litware Health (fictional)

    The problem
    The patient portal's NSGs had 60 rules listing individual IPs. Every scale-out meant a ticket, and a forgotten NIC NSG kept new web servers dark for a day.
    What they used
    Three ASGs (asg-web, asg-api, asg-db) and one NSG per subnet with role-based rules: Internet to asg-web on 443, asg-web to asg-api on 8443, asg-api to asg-db on 1433. NIC-level NSGs removed. IP flow verify added to the runbook.
    Why that choice
    Rules describe roles, so scale-out needs no rule changes, and one NSG layer makes effective rules easy to read.
    The result
    The rule count dropped from 60 to 9, and new servers serve traffic as soon as they join their ASG.

    When to use it and when not to

    ✓ Use it when…

    • Allowing only the ports each tier needs, on every subnet.
    • Role-based rules with ASGs so scale-out doesn't need rule edits.
    • Effective rules and IP flow verify whenever traffic is mysteriously blocked.

    ✕ Think twice when…

    • NSGs on both subnet and NIC without a reason. One layer is easier to troubleshoot.
    • Filtering by domain name or with threat intelligence. That needs Azure Firewall.
    • Opening RDP or SSH to the internet. Use Azure Bastion.

    ↔ Alternatives to consider

    • Azure Firewall for FQDN rules, threat intelligence and central logging.
    • Azure Virtual Network Manager security admin rules to enforce rules across many VNets.
    • Azure Bastion instead of management ports open to the internet.
    Ready for more?The full lesson adds the technical explanation, when to use it, interview questions and revision notes.

    Test yourself with exercises

    5 original practice questions in the AZ-104 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.

    Question 1

    Question 1 of 5

    An NSG contains the following inbound rules: priority 100 deny TCP 3389 from Internet, priority 200 allow TCP 3389 from 203.0.113.0/24. A user at 203.0.113.25 connects to a VM on TCP 3389. What happens?

    Question 2

    Question 2 of 5

    A subnet NSG allows inbound TCP 443 from the internet. The network interface of VM1 in the subnet has an NSG that contains only the default rules. Can internet users connect to VM1 on TCP 443?

    Question 3

    Question 3 of 5

    You manage web servers and database servers in the same virtual network. Web servers are added and removed frequently. You need an NSG rule that allows TCP 1433 from the web servers to the database servers without updating IP addresses. What should you use?

    Question 4

    Question 4 of 5

    You need to identify every NSG rule that applies to the network interface of a running VM, including rules from the subnet NSG. What should you use?

    Question 5

    Question 5 of 5 · Select 2

    Which two statements about network security groups are true? Each correct answer presents a complete solution.

    Interview preparation

    Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.

    Beginner Two rules match the same traffic, one allows and one denies. Which wins?

    Model answer

    The one with the lower priority number. Azure processes rules from the lowest number up and stops at the first match, so priority 100 beats priority 200 regardless of which one allows.

    Intermediate You allowed port 443 in the subnet NSG, but traffic is still blocked. Why?

    Model answer

    There's probably an NSG on the NIC too. Inbound traffic has to pass the subnet NSG and then the NIC NSG, and an NSG with no allow rule for 443 ends at DenyAllInBound. I'd open the NIC's effective security rules or run IP flow verify to see the exact rule.

    Experienced developer Why use application security groups instead of IP addresses in rules?

    Model answer

    Rules then describe roles. "Internet to asg-web on 443, asg-web to asg-db on 1433" stays the same when servers are added, removed or re-addressed; you just put the new NIC in the ASG. The limits are that all NICs in an ASG must be in the same VNet, and ASGs used together in a rule must be in the same VNet too.

    Scenario Security asks you to block all RDP from the internet but allow it from the office range 203.0.113.0/24. How do you write it?

    Model answer

    An allow rule for TCP 3389 from 203.0.113.0/24 at priority 100, and a deny rule for 3389 from Internet at priority 200. The office range matches first. Better still, remove the need entirely with Azure Bastion and only allow 3389 from the Bastion subnet.

    Quick revision

    Key points

    • Rules have priority 100 to 4096, lowest first, first match wins. Defaults sit at 65000+.
    • Inbound - subnet NSG then NIC NSG. Outbound - NIC NSG then subnet NSG. Both must allow.
    • NSGs are stateful. Allowed inbound traffic gets its replies back automatically.
    • ASGs group NICs (same VNet) so rules target roles, not IPs.
    • Effective security rules and IP flow verify show the real result.

    Important terminology

    Network security group (NSG)
    A set of stateful allow and deny rules for inbound and outbound traffic, attached to subnets and/or network interfaces.
    Priority
    A number from 100 to 4096. Lower numbers are processed first, and processing stops at the first match.
    Default security rules
    AllowVnetInBound (65000), AllowAzureLoadBalancerInBound (65001), DenyAllInBound (65500), and the outbound equivalents including AllowInternetOutBound. They can't be deleted, only overridden.
    Service tag
    A Microsoft-maintained name for a group of IP ranges, such as Internet, VirtualNetwork, AzureLoadBalancer or Storage.
    Application security group (ASG)
    A label you attach to NICs so NSG rules can use it as a source or destination instead of IP addresses.
    Effective security rules
    The combined rules from every NSG that applies to a NIC, shown on the NIC in the portal or with az network nic list-effective-nsg.

    Common mistakes

    • Forgetting a NIC-level NSG when you've only checked the subnet.
    • Assuming a higher number has higher priority.
    • Blocking the AzureLoadBalancer tag and breaking health probes.

    Exam tips

    • "Which rule applies?" = find the lowest priority number that matches.
    • "Allowed on the subnet but still blocked" = check the NIC NSG.
    • "Rules must not change when servers are added" = application security groups.

    Five quick revision questions

    1. 1.What is the valid priority range for custom rules?Show

      100 to 4096.

    2. 2.Which NSG is evaluated first for inbound traffic?Show

      The subnet NSG.

    3. 3.What is the priority of DenyAllInBound?Show

      65500.

    4. 4.Can an ASG contain NICs from different VNets?Show

      No.

    5. 5.Which CLI command shows effective rules on a NIC?Show

      az network nic list-effective-nsg.

    Official references

    Finished this lesson? Tick it off. Your progress is saved in this browser.

    Next up: Connect to VMs Securely with Azure Bastion

    Studying with a friend? Send them this lesson

    It's free and needs no sign-up. Their progress saves in their own browser.

    Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges. Play ❯

    Install MSDEVBUILD Azure

    Free · no account · works offline

    Save the whole course for offline ›

    Hi, I’m your Nimbus

    Your study buddy. Pick what we do next.

    MSDEVBUILD · Multi-Stack Dev Build

    Sign in to MSDEVBUILD

    One account for Azure certifications and the blog: sign in here and you are signed in on both. Your XP, streak, practice deck and certificate stay in sync on every device. Optional: everything works without an account.