Design Azure VNets, subnets and NSGs
Plan address space and subnets, filter traffic with NSGs and trace the packet path through routes and peering.
By Invicly Tech, , in Microsoft Azure Cloud Engineering. About 14 minutes to read.
In short
An Azure virtual network (VNet) is your own private IP address space in one region, split into subnets that hold VMs, private endpoints and other resources. Network security groups (NSGs) filter traffic in and out of subnets and network interfaces, and route tables decide where each packet goes next. Good design starts with address space that never overlaps with anything you may connect to later.
Think of it like this: The VNet is an office building, subnets are its floors, NSGs are the door guards on each floor, and the route table is the sign in the lift telling you which floor to go to.
Explained simply
A virtual network, or VNet, is a private network that you own inside one Azure region. You give it a range of private IP addresses, such as 10.10.0.0/16, and Azure keeps it isolated from everyone else's networks. Virtual machines, private endpoints and many other resources get their IP addresses from it.
You split the VNet into smaller ranges called subnets. A common habit is one subnet per role: web servers in one, internal APIs in another, databases in a third. Some Azure services insist on their own subnet with a fixed name, such as AzureBastionSubnet or GatewaySubnet.
A network security group, or NSG, is a list of rules that says which traffic may enter or leave a subnet or a single network card. Each rule names a source, a destination, a port, a protocol and allow or deny. Azure checks the rules in order of priority and stops at the first one that matches.
Routing decides where a packet goes next. Azure creates system routes for you: traffic inside the VNet stays local, and other traffic heads to the internet. You can add your own routes, connect VNets with peering, and give a subnet a fixed outbound public IP with a NAT Gateway.
Press Play on a diagram to walk through it one step at a time.
- Step 1Route lookup
Longest matching prefix picks the next hop
- Step 2Subnet NSG
Inbound rules checked by priority
- Step 3NIC NSG
Checked next, if one is attached
- Step 4VM
Guest OS firewall has the last word
- snet-web 10.10.1.0/24
- Front-end VMs or App Gateway
- snet-app 10.10.2.0/24
- Internal API servers
- snet-data 10.10.3.0/24
- Private endpoints for databases
- AzureBastionSubnet 10.10.250.0/26
- Secure admin access
- 1Describe the flow
Source IP, destination IP, port, protocol
- 2Read effective routes
Is the next hop what you expect?
- 3Read effective NSG rules
Which rule matches first?
- 4Test with IP flow verify
Network Watcher names the rule
- 5Fix and retest
Change one thing at a time
- Back to step 1
In depth
Planning address space you will not regret
Changing a VNet's address space later is possible, but changing the subnets that already hold resources is painful. So plan first. Use private ranges from RFC 1918 (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) and give every VNet, every environment and every on-premises site its own block that never overlaps. Overlap is the one mistake you cannot route around: two networks that both use 10.0.0.0/16 cannot be peered or joined by VPN.
Size subnets for growth. Azure keeps 5 addresses in each subnet (network address, three for its own services, and broadcast), so a /24 gives 251 usable addresses and a /29 only 3. Services that scale out, such as App Service integration or AKS, can eat many addresses quickly.
- Write the plan down in one table before you create anything
- Leave gaps between blocks for future subnets
- Azure Firewall needs a /26 called AzureFirewallSubnet
- Azure Bastion needs AzureBastionSubnet, /26 or larger
- Hub 10.0.0.0/16
- Firewall, gateways, Bastion
- Prod spoke 10.1.0.0/16
- web, app, data subnets
- Dev spoke 10.2.0.0/16
- Same subnet layout as prod
- On-premises 192.168.0.0/16
- Kept outside the Azure block
How NSG rules are evaluated
Each NSG holds custom rules with priorities from 100 to 4096; the lowest number is checked first and the first match decides. Under your rules sit default rules you cannot delete. Inbound, they allow traffic from inside the VNet and from the Azure load balancer probe, then deny everything else. Outbound, they allow traffic to the VNet and to the internet, then deny everything else.
For inbound traffic Azure applies the subnet NSG first and then the NIC NSG; outbound runs the other way round. Traffic must be allowed by both if both exist, which is why many teams use only subnet NSGs. Service tags, such as Internet, VirtualNetwork or AzureLoadBalancer, stand in for IP lists that Microsoft keeps up to date. Application security groups let you name a group of VMs, such as asg-web, and write rules against the group instead of IPs.
- AllowVnetInBound 65000, AllowAzureLoadBalancerInBound 65001, DenyAllInBound 65500
- AllowVnetOutBound 65000, AllowInternetOutBound 65001, DenyAllOutBound 65500
- Leave gaps such as 100, 110, 120 so you can insert rules later
- NSGs are stateful: return traffic is allowed automatically
Routes: system, custom and the packet path
Every subnet starts with system routes: the VNet's own range goes to VnetLocal, peered ranges go to VNetPeering, and 0.0.0.0/0 goes to Internet. You can add a route table with user-defined routes (UDRs), and a gateway can learn routes from on-premises through BGP.
Azure first picks the route with the longest matching prefix, so a /24 beats a /16. Only if two routes have exactly the same prefix does the source decide: a user-defined route wins over a BGP route, which wins over a system route. The result for one network interface is called its effective routes, and reading that list is the fastest way to answer where a packet really goes. A route with next hop None drops traffic on purpose.
- Step 1Collect routes
System, BGP and user-defined
- Step 2Longest prefix
The most specific match wins
- Step 3Tie on prefix
User-defined, then BGP, then system
- Step 4Next hop
VnetLocal, peering, gateway, appliance, Internet or None
Peering, service endpoints and NAT Gateway
VNet peering joins two VNets so their resources talk over private IPs on Microsoft's backbone, in the same region or across regions. You create it from both sides, the ranges must not overlap, and it is not transitive. Peered traffic is billed per GB in each direction.
Service endpoints are an older, simpler way to reach PaaS services: they keep traffic from a subnet on the Azure backbone and let the service firewall allow that subnet, but the service keeps its public IP. Private endpoints, covered in the next topic, give the service a private IP instead.
For outbound internet traffic, attach a NAT Gateway to the subnet. It gives every VM in that subnet the same fixed public IPs, scales to many connections, and replaces implicit default outbound access. With network API versions released after March 31, 2026, subnets in new VNets are private by default and have no default outbound access.
- Peering: create both directions, no overlap, not transitive
- Service endpoint: backbone path, public IP stays
- NAT Gateway: fixed outbound IPs per subnet, no inbound
- A NAT Gateway outbound path takes priority over default outbound access
Learn it in 5 short steps
About 35m each. Track them with a timer in your plan.
- 1Plan non-overlapping address space~35m
- 2Split subnets by role~35m
- 3Write NSG rules~35m
- 4Read effective routes~35m
- 5Use peering and NAT Gateway~35m
Key points
- Pick a private range that will not clash with on-premises or other VNets you may peer with.
- Azure reserves 5 addresses in every subnet: the first four and the last one.
- NSG rules run from the lowest priority number (100) upwards; the first match wins.
- NSGs are stateful: reply traffic for an allowed flow is allowed back automatically.
- Peering is not transitive: if A peers with B and B with C, A still cannot reach C.
Key terms
Tap a card to flip it. 0/6 known
Examples
az network vnet create -g rg-net -n vnet-app --address-prefixes 10.10.0.0/16 \
--subnet-name snet-web --subnet-prefixes 10.10.1.0/24
az network nsg create -g rg-net -n nsg-web
az network nsg rule create -g rg-net --nsg-name nsg-web -n allow-https \
--priority 100 --direction Inbound --access Allow --protocol Tcp \
--destination-port-ranges 443 --source-address-prefixes Internet
az network vnet subnet update -g rg-net --vnet-name vnet-app -n snet-web \
--network-security-group nsg-webCreate a VNet, an NSG that allows HTTPS only, and attach it to the web subnet.
Finding the rule that blocks port 8080
An API on vm-app listens on 8080, but calls from vm-web time out. Step 1: describe the flow: 10.10.1.4 to 10.10.2.4, TCP 8080. Step 2: list the effective NSG rules on vm-app's network interface; you see only the default rules plus allow-https on 443. Step 3: ask Network Watcher to verify the flow; it reports DenyAllInBound. Step 4: add a rule allowing TCP 8080 from the web subnet only, at priority 110. Step 5: run IP flow verify again and expect Access: Allow with your new rule's name.
az network nic list-effective-nsg -g rg-net -n vm-appVMNic -o table
az network watcher test-ip-flow -g rg-net --vm vm-app --direction Inbound \
--protocol TCP --local 10.10.2.4:8080 --remote 10.10.1.4:50000
az network nsg rule create -g rg-net --nsg-name nsg-app -n allow-web-8080 \
--priority 110 --direction Inbound --access Allow --protocol Tcp \
--source-address-prefixes 10.10.1.0/24 --destination-port-ranges 8080Giving a subnet a fixed outbound IP
A partner only accepts calls from one known IP address. Step 1: create a Standard public IP. Step 2: create a NAT Gateway that uses it. Step 3: attach the NAT Gateway to snet-app. Step 4: from a VM in that subnet, ask an echo service for your public IP and expect the NAT Gateway's address. Send that single IP to the partner. NAT Gateway and its public IP bill by the hour, so delete both when the lab ends.
az network public-ip create -g rg-net -n pip-nat --sku Standard
az network nat gateway create -g rg-net -n nat-app --public-ip-addresses pip-nat
az network vnet subnet update -g rg-net --vnet-name vnet-app -n snet-app \
--nat-gateway nat-app
# On a VM in snet-app
curl -s https://ifconfig.meQuick reference
- Azure keeps 5 IPs per subnet: a /24 gives 251 usable, a /29 gives 3
- NSG priority 100 to 4096; lowest number first; first match wins
- Inbound: subnet NSG then NIC NSG; outbound: NIC NSG then subnet NSG
- Route choice: longest prefix, then user-defined over BGP over system
- az network nic show-effective-route-table -g RG -n NIC shows the real routes
- az network nic list-effective-nsg -g RG -n NIC shows the combined NSG rules
- Peering is not transitive and needs non-overlapping ranges
- NAT Gateway = fixed outbound IPs for a subnet; it never accepts inbound connections
Common mistakes
- Using 10.0.0.0/16 everywhere, then finding two VNets cannot be peered because their ranges overlap.
- Making subnets too small; a /29 leaves only 3 usable addresses once Azure takes its 5.
- Opening port 22 or 3389 to the internet instead of using Azure Bastion or just-in-time access.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Create a VNet 10.10.0.0/16 with subnets snet-web (10.10.1.0/24) and snet-app (10.10.2.0/24), then list the subnets. Delete the resource group afterwards.
- Exercise 2medium
Write NSG rules for snet-app so that only snet-web can reach TCP 8080, and everything else from inside the VNet is denied. Explain why the deny rule is needed.
- Exercise 3hard
Peer vnet-app (10.10.0.0/16) with vnet-shared (10.20.0.0/16) in both directions, then prove the peering from a VM's effective routes. Note the cost: peered traffic is billed per GB and the test VM bills per hour.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Should I put the NSG on the subnet or on the network interface?
- Usually on the subnet, so every resource in it gets the same rules and there is one place to look. Use a NIC NSG only when one VM truly needs different rules.
- Can I change a VNet's address space after creating it?
- You can add or remove address ranges, but a range in use by a subnet cannot be removed, and peered VNets need a peering sync afterwards. It is far easier to plan generous ranges up front.
- What is the difference between a service endpoint and a private endpoint?
- A service endpoint keeps traffic on the Azure backbone but the service still uses its public IP. A private endpoint gives the service a private IP in your subnet, so you can turn public access off completely.
- Do I still need a firewall on the VM if I use NSGs?
- NSGs filter at the network level and are usually enough for port control. Many teams keep the guest firewall on as a second layer, but must remember to open ports in both places.