Azure NSGs, Bastion, Service Endpoints & Private Endpoints
Securing access to an Azure virtual network means controlling which traffic reaches your resources and how administrators and platform services connect to them. Network security groups (NSGs) filter traffic with stateful, priority-ordered allow and deny rules that can reference IP ranges, Microsoft-managed service tags or application security groups (ASGs), which group VMs by role instead of by IP address. Effective security rules show the merged result when NSGs apply at both subnet and NIC level. Azure Bastion gives browser-based RDP and SSH to VMs that have no public IP, and service endpoints and private endpoints connect your VNet to Azure PaaS services in two very different ways. This lesson explains how each control works, what the default rules and common service tags really match, how to read effective rules, which Bastion SKU provides which feature, and how to configure service endpoints and private endpoints — including the DNS that private endpoints depend on.
On this page10 sections
- Filter traffic with network security groups
- Use service tags and default rules correctly
- Apply NSGs at the subnet or the NIC level
- Group workloads with application security groups
- Evaluate effective security rules
- Reach VMs privately with Azure Bastion
- Service endpoints versus private endpoints for PaaS
- Configure a service endpoint
- Configure private endpoints and their DNS
- Scenario: Bastion for admins plus a private endpoint for storage
- Create inbound and outbound NSG rules using priority, the 5-tuple and service tags, and explain the default rules and NSG statefulness
- Apply NSGs at subnet and NIC level and predict the result when both apply
- Use application security groups to reference workloads by role, within their NIC and VNet constraints
- Evaluate effective security rules on a network interface and choose between effective rules, IP flow verify and next hop
- Deploy Azure Bastion with the right subnet, NSG rules and SKU, including access to peered VNets
- Configure service endpoints and private endpoints for Azure PaaS, including private endpoint DNS, and choose between them
Filter traffic with network security groups
A network security group (NSG) is a stateful set of security rules that allows or denies network traffic to and from Azure resources in a virtual network. Each custom rule has a priority between 100 and 4096, and a lower number means higher priority: Azure evaluates rules from the lowest number upward and stops at the first rule that matches. There is no "deny overrides allow" inside one NSG — if a rule at priority 200 allows a flow that a rule at priority 300 would deny, the allow wins because it matches first. Priorities must be unique per direction within one NSG (two inbound rules cannot share 300), but an inbound and an outbound rule may use the same number, and leaving gaps (100, 110, 120…) makes it easy to insert rules later.
Every rule matches on a 5-tuple — source, source port, destination, destination port and protocol (TCP, UDP, ICMP or Any) — plus a direction (inbound or outbound) and an action (Allow or Deny). Source and destination can be an IP address or CIDR range, a service tag such as Internet or VirtualNetwork, or an application security group.
NSGs are stateful
An NSG tracks flows, so you write a rule only for the direction in which a connection starts. If an inbound rule allows TCP 443 to a web server, the server's replies are allowed back out automatically — no matching outbound rule is needed — and an outbound allow likewise lets responses back in. Statefulness also means that removing an allow rule may not cut connections that are already established: existing flows can continue, while every new flow is evaluated against the updated rules.
The default rules
Every NSG contains default rules that you cannot delete but can override with any custom rule, because every custom priority (100–4096) is evaluated before them.
| Direction | Priority | Default rule | Effect |
|---|---|---|---|
| Inbound | 65000 | AllowVNetInBound | Allows traffic from VirtualNetwork to VirtualNetwork |
| Inbound | 65001 | AllowAzureLoadBalancerInBound | Allows traffic from the AzureLoadBalancer tag (health probes) |
| Inbound | 65500 | DenyAllInBound | Denies everything else, including all internet-originated traffic |
| Outbound | 65000 | AllowVnetOutBound | Allows traffic to VirtualNetwork |
| Outbound | 65001 | AllowInternetOutBound | Allows traffic to the Internet tag |
| Outbound | 65500 | DenyAllOutBound | Denies everything else |
Two consequences follow. First, internet traffic is allowed only outbound by default; any inbound service from the internet (HTTPS, SSH, RDP) needs an explicit allow rule. Second, traffic inside the virtual network is open by default, which matters whenever you want to restrict it (see the service-tag section below).
Apply NSGs at the subnet or the NIC level
You can associate an NSG with a subnet, with a network interface (NIC), or with both. An NSG on a subnet applies to every resource in that subnet, which makes it the efficient place for broad rules such as blocking inbound RDP from the internet across a tier. An NSG on a NIC applies only to that interface, which is useful when one machine needs an exception the rest of the subnet does not. One NSG can be associated with many subnets and NICs.
When both are present, traffic must pass both NSGs. For inbound traffic Azure evaluates the subnet NSG first and then the NIC NSG; for outbound traffic it evaluates the NIC NSG first and then the subnet NSG. Each NSG reaches its own first-match verdict independently — priorities do not compare across the two NSGs, so a priority-100 allow on the NIC does not beat a priority-4000 deny on the subnet. If either NSG denies the flow, it is dropped. A common mistake is adding an allow rule to the NIC NSG while the subnet NSG still denies the same traffic — the packet never arrives.
NSG rules also apply to traffic between two VMs in the same subnet: those VMs can normally talk only because the default AllowVNetInBound rule permits it, so a higher-priority deny blocks it.
Group workloads with application security groups
An application security group (ASG) is a named group of network interfaces that you reference as the source or destination of an NSG rule instead of IP addresses. Maintaining rules by IP becomes fragile as machines scale in and out or change addresses; an ASG lets you write the rule by role and keep it unchanged as membership changes.
You create ASGs such as WebServers and DbServers, then add each VM's network interface to the right group — ASGs are assigned to NICs, not to subnets (and not to the NSG itself). In the NSG you write a rule like "allow TCP 1433 from WebServers to DbServers". When you add a web server, you attach its NIC to WebServers and it inherits every rule that references the group — no rule edits, no IP bookkeeping. ASGs work cleanly with virtual machine scale sets, whose NIC configuration can carry the ASG.
Constraints to remember:
- A NIC can belong to several ASGs at once (for example
WebServersandMonitoredHosts). - All NICs in an ASG must be in the same virtual network as the first NIC added to it.
- When a rule uses ASGs as both source and destination, the NICs in both groups must be in the same VNet. Traffic arriving from a peered VNet therefore cannot be matched by an ASG; reference it by its IP prefix (or a tag) instead.
An ASG only says which machines; the NSG still decides what traffic is allowed, and the default rules still apply beneath your ASG rules.
Evaluate effective security rules
Effective security rules show, for one network interface, the merged set of rules actually in force: the NSG on that NIC plus the NSG on its subnet, with default rules included, service tags expanded to their IP prefixes and rules that reference ASGs included. It is the way to read the real outcome when rules stack at two levels.
To use it, open the VM's networking page or the network interface and choose Effective security rules. Points that matter in practice:
- The view is per network interface. A VM with two NICs can have different effective rules on each, so check the NIC that actually carries the traffic.
- The VM must be running for effective rules to be shown.
- The portal lists the first 50 rules; download the CSV to see the complete list.
- Read it with the processing logic in mind: first match by priority inside each NSG, and a deny in either NSG wins across the pair.
Effective rules list everything; they do not give a verdict for one specific connection. Network Watcher provides the complementary tools:
| Tool | Question it answers |
|---|---|
| Effective security rules | Which rules apply to this NIC, merged across subnet and NIC NSGs? |
| Network Watcher IP flow verify | Is this one flow (direction, protocol, local/remote IP and port) allowed or denied — and by which rule? |
| Network Watcher next hop | Where is traffic to this destination routed (a routing question, not a filtering one)? |
So use IP flow verify when you need a quick allow/deny for a single connection with the responsible rule named, effective security rules when you need to audit the whole rule set on a NIC, and next hop when the packet is being sent somewhere unexpected rather than being blocked.
Reach VMs privately with Azure Bastion
Azure Bastion is a fully managed PaaS service that gives RDP and SSH access to virtual machines through the Azure portal over TLS on port 443, connecting to each VM on its private IP. The VMs need no public IP, no inbound RDP/SSH from the internet and no agent or extra software installed; administrators need only a browser (or, on higher SKUs, the native client).
A dedicated Bastion host (Basic, Standard or Premium SKU) is deployed into a subnet named exactly AzureBastionSubnet, sized /26 or larger, and uses a Standard SKU public IP (the exception is a Premium private-only deployment, which has no public IP). If you put an NSG on AzureBastionSubnet itself, it must carry the rules Bastion needs (for example inbound 443 from Internet and GatewayManager, and outbound RDP/SSH to the VNet).
Rules on the target VMs
Bastion connects to a VM from inside the virtual network, so the target VM's NSG must allow RDP (3389) or SSH (22) from the AzureBastionSubnet address range — not port 443 (that is the admin-to-Bastion leg) and not the administrators' public IPs (their packets never reach the VM directly). You can then deny all other inbound traffic from the internet.
One host for peered VNets
A Basic or higher Bastion host reaches VMs in its own VNet and in directly peered VNets. Peering is not transitive, so in a hub-and-spoke design you place one Bastion host in the hub and it serves every spoke peered to that hub; a spoke peered only to another spoke is not reachable.
Bastion SKUs
| SKU | What it adds |
|---|---|
| Developer | Free, shared infrastructure; no AzureBastionSubnet or public IP to deploy; connects to one VM at a time; no peered-VNet access |
| Basic | Cheapest dedicated host; peered-VNet access; concurrent sessions |
| Standard | Native client support (az network bastion rdp / ssh), IP-based connection, shareable links, custom ports, host scaling (more instances) |
| Premium | Everything in Standard plus session recording (graphical sessions saved to a storage blob container) and private-only deployment |
Bastion diagnostic logs record connection metadata (who connected, from where, to which VM and when), not the session contents — recording the sessions themselves needs Premium session recording. Bastion complements NSGs; it does not replace them.
Service endpoints versus private endpoints for PaaS
Service endpoints and private endpoints both secure access from a virtual network to Azure PaaS services such as Azure Storage, Azure SQL Database and Key Vault, but they work differently: a service endpoint keeps the service on its public endpoint and lets its firewall trust your subnet, while a private endpoint gives the service a private IP inside your VNet.
| Aspect | Service endpoint | Private endpoint |
|---|---|---|
| How access works | Subnet traffic reaches the service's public endpoint over the Azure backbone with the subnet's private identity | A NIC with a private IP in your subnet, mapped through Private Link to one resource |
| Service public endpoint | Required — VNet rules apply only while public access is enabled for selected networks | Not needed; public network access can be disabled |
| Scope | A whole service type (for example all storage accounts) from that subnet; the firewall picks which accounts trust it | One specific resource and one sub-resource (for example the blob service of one account) |
| Reach from peered VNets / on-premises | No — only the enabled subnet; on-premises traffic is not covered | Yes — peered VNets and on-premises over VPN or ExpressRoute |
| DNS changes | None | Needs private DNS so the name resolves to the private IP |
| Cost | Free | Billed per endpoint hour and per GB processed |
Choose a service endpoint when traffic only needs to come from Azure subnets, at no cost and with no DNS work. Choose a private endpoint when the service must be reachable on a private IP, from on-premises or peered networks, or with public access turned off entirely.
Configure a service endpoint
Configuring a service endpoint is a two-sided change: enable the endpoint on the subnet, then trust that subnet on the service.
- On the subnet, enable the service endpoint for the service type, such as
Microsoft.StorageorMicrosoft.Sql. - On the resource's networking/firewall settings, set public network access to Enabled from selected virtual networks and IP addresses and add a virtual network rule for that subnet.
Behaviour to understand:
- The source address changes. Once the endpoint is enabled, the subnet's traffic to that service arrives from its private VNet address instead of its public IP. Any existing firewall IP rule that admitted the subnet by its public IP stops matching, so add the VNet rule first (or together) to avoid an outage.
- IP network rules accept public ranges only. You cannot admit a private (RFC 1918) range through an IP rule; private sources are admitted with VNet rules.
- On-premises is not covered. Traffic from on-premises reaches the service through its public IP (typically its NAT address), so it needs an IP rule for that public address — or a private endpoint.
- It depends on the public endpoint. If public network access is set to Disabled, VNet rules stop applying and only private endpoints work.
Configure private endpoints and their DNS
A private endpoint is a network interface with a private IP from your subnet that connects through Azure Private Link to one resource and one target sub-resource. A storage account that needs both Blob and Files privately needs two private endpoints (sub-resources blob and file), each with its own private IP and DNS record. Because it is just an IP in your VNet, the endpoint is reachable from the VNet, from peered VNets and from on-premises over VPN or ExpressRoute.
Creating the endpoint does not by itself block the public endpoint. To make the service private-only, set its public network access to Disabled as a separate step.
How name resolution works
Clients keep using the normal hostname, such as contoso.blob.core.windows.net. When a private endpoint exists, the public DNS answer becomes a CNAME to contoso.privatelink.blob.core.windows.net. Inside your network, a private DNS zone named privatelink.blob.core.windows.net holds an A record mapping that name to the endpoint's private IP. Outside your network the same CNAME resolves to the public IP, so no certificate or connection-string changes are needed.
When you create the endpoint in the portal with private DNS integration, Azure creates (or reuses) the privatelink zone for that service — for example privatelink.vaultcore.azure.net for Key Vault or privatelink.database.windows.net for Azure SQL — adds the A record and links the zone to the virtual network.
| Where the client resolves | What makes the private IP come back |
|---|---|
| VM using Azure-provided DNS (168.63.129.16) | The privatelink zone linked to the VM's VNet — no forwarder needed |
| VM using custom DNS servers in Azure | The custom servers forward the service's public zone (for example blob.core.windows.net) to 168.63.129.16 from a VNet linked to the zone |
| On-premises client | On-premises DNS conditionally forwards the public zone (for example blob.core.windows.net) to an Azure DNS Private Resolver inbound endpoint (or a DNS forwarder VM) in a linked VNet — 168.63.129.16 is not reachable from on-premises |
Forward the public zone, not only the privatelink zone, so the CNAME chain is followed and resolved inside Azure.
Scenario: Bastion for admins plus a private endpoint for storage
Your compliance team sets two rules for a new application tier: administrators must manage the virtual machines without any VM being exposed to the internet, and the application's storage account must not be reachable over its public endpoint at all.
For administrative access, deploy Azure Bastion (Basic or higher) into an AzureBastionSubnet of /26 or larger in the hub VNet that the application VNet is peered to. Remove public IPs from the VMs. In the VMs' NSG, allow RDP or SSH from the AzureBastionSubnet range and rely on DenyAllInBound for internet traffic. Admins connect through the portal over TLS with no exposed management ports.
For the storage account, create a private endpoint for the blob sub-resource in the application subnet (and a second one for file if Azure Files is used), with private DNS integration so privatelink.blob.core.windows.net is linked to the VNet. Then set the account's public network access to Disabled. If on-premises servers also need the account, add a conditional forwarder for blob.core.windows.net to a DNS Private Resolver inbound endpoint. The result: no internet-facing VMs and a storage account reachable only on its private IP.
Tip. Questions describe a connectivity requirement and a constraint — lowest cost, no public IP, no DNS changes, on-premises access, or other traffic must stay blocked — and expect you to reason from how the control behaves. Typical setups stack subnet and NIC NSGs with conflicting priorities, rely on what a service tag such as VirtualNetwork or AzureLoadBalancer really matches, or add an allow without the deny needed to block the rest. Bastion items test the subnet name and size, the VM NSG rule, peering reach and which SKU a feature needs. PaaS items test service endpoint scope and side effects versus private endpoint sub-resources and DNS resolution.
- NSG priorities run 100-4096, lower is evaluated first, the first match wins, and priorities must be unique per direction within one NSG.
- NSGs are stateful: return traffic of an allowed flow needs no reverse rule, and removing a rule may not cut existing connections.
- Default rules allow VirtualNetwork and AzureLoadBalancer inbound, allow internet only outbound, and deny the rest at priority 65500.
- VirtualNetwork includes peered and gateway-connected ranges; AzureLoadBalancer is only the 168.63.129.16 health-probe source; you cannot define your own service tags.
- A narrow allow restricts nothing on its own: add an explicit deny above the 65000 defaults to block the remaining VNet or internet traffic.
- With subnet and NIC NSGs, inbound is checked subnet then NIC, outbound NIC then subnet, and a deny in either wins.
- ASGs are assigned to NICs, a NIC can join several ASGs, and NICs in a rule's ASGs must share one VNet.
- Effective security rules are per NIC and list the merged rule set; IP flow verify gives a verdict for one flow; next hop answers routing.
- Azure Bastion needs AzureBastionSubnet /26 or larger, reaches directly peered VNets from Basic up, and the VM NSG must allow 3389/22 from that subnet.
- Native client, IP-based connection, shareable links and host scaling need Standard; session recording and private-only need Premium.
- Service endpoints keep the public endpoint, cover only the enabled subnet (not on-premises) and switch the subnet's source IP for that service.
- A private endpoint maps one resource sub-resource to a private IP and needs a privatelink DNS zone linked to the VNet; disable public access separately.
Frequently asked questions
How does NSG rule priority work in Azure?
Each custom NSG rule has a priority between 100 and 4096, and a lower number is evaluated first. Azure applies the first rule whose 5-tuple matches the traffic and stops, so a priority-100 rule overrides a conflicting priority-200 rule in the same NSG. Priorities must be unique per direction within one NSG, and the default rules at 65000-65500 are evaluated after all custom rules.
Do Azure NSGs need rules for return traffic?
No. Network security groups are stateful, so when a rule allows a connection in one direction, the response traffic for that flow is allowed automatically without a reverse rule. Because flows are tracked, removing an allow rule may not interrupt connections already established; new connections are evaluated against the updated rules.
What does the VirtualNetwork service tag include?
The VirtualNetwork service tag covers the virtual network's own address space plus peered virtual networks, address ranges connected through a virtual network gateway (including on-premises networks over VPN or ExpressRoute) and prefixes used in user-defined routes. That is why the default AllowVNetInBound rule admits more than just the local VNet.
What is the difference between a service endpoint and a private endpoint?
A service endpoint lets a subnet reach an Azure PaaS service's public endpoint over the Azure backbone so the service firewall can trust that subnet; it is free, needs no DNS changes and does not cover on-premises or peered networks. A private endpoint gives one specific resource a private IP in your VNet through Private Link, works from peered VNets and on-premises, needs private DNS, and lets you disable public network access.
Can application security groups be assigned to a subnet?
No. Application security groups are assigned to network interfaces, and a NIC can belong to several ASGs. All NICs in an ASG must be in the same virtual network, and a rule that uses ASGs as both source and destination requires the NICs in both groups to be in the same VNet.
Which NSG rule does a VM need for Azure Bastion?
The target VM's NSG must allow RDP on port 3389 or SSH on port 22 inbound from the AzureBastionSubnet address range, because Bastion connects to the VM's private IP from inside the virtual network. It does not need port 443 or the administrators' public IPs, and the VM needs no public IP or agent.
Which Azure Bastion SKU supports the native client and session recording?
Native client connections (az network bastion rdp or ssh), IP-based connection, shareable links, custom ports and host scaling require the Standard SKU or higher. Session recording and private-only deployment require the Premium SKU. The free Developer SKU connects to one VM at a time and does not reach peered virtual networks.
How does DNS work for an Azure private endpoint?
The service's public hostname resolves through a CNAME to a privatelink name, such as contoso.privatelink.blob.core.windows.net. A private DNS zone with that privatelink name holds an A record for the endpoint's private IP and must be linked to the virtual network. VMs using Azure-provided DNS resolve it directly; on-premises DNS must conditionally forward the public zone to an Azure DNS Private Resolver inbound endpoint.
Source
This lesson covers the "Implement and manage virtual networking" domain of the official AZ-104 exam guide. Vendors revise their guides — check the source for the current version.
- Microsoft AZ-104 study guide — Microsoft Learn
Sign up free to mark lessons complete, bookmark topics and track your exam readiness.