Networking is where most Azure security mistakes happen. A virtual machine deployed with a flat, permissive network is one mis-click away from the open internet. Two building blocks prevent that: the Azure Virtual Network, which defines your private address space, and Azure Network Security Groups, which filter the traffic flowing through it. This guide explains both, with working CLI examples.
What Is an Azure Virtual Network? – Azure Virtual Network and Network Security
An Azure Virtual Network (VNet) is your private network in the cloud — the equivalent of the LAN in your datacenter. You define an address space in CIDR notation, for example 10.0.0.0/16, and carve it into subnets such as 10.0.1.0/24 for web servers and 10.0.2.0/24 for databases. Resources in the same VNet can communicate privately by default; nothing reaches them from the internet unless you explicitly allow it.
What Are Azure Network Security Groups? – Azure Virtual Network and Network Security
Azure Network Security Groups are stateful packet filters attached to subnets or to individual network interfaces. Each NSG contains rules with this anatomy:
- Priority (100–4096): lower numbers are evaluated first
- Direction: inbound or outbound
- Source / destination: IP ranges, or service tags such as
InternetandVirtualNetwork - Protocol and ports: for example TCP destination port 443
- Action: Allow or Deny
Azure adds three default inbound rules you cannot delete: allow traffic from inside the VNet, allow Azure load balancer probes, and deny everything else. Your custom rules slot in above them by priority.

Prerequisites
- Azure CLI installed and signed in
- A resource group, for example
rg-linux-prod
Step 1: Create a VNet with Two Subnets
az network vnet create \
--resource-group rg-linux-prod \
--name vnet-prod \
--address-prefix 10.0.0.0/16 \
--subnet-name web \
--subnet-prefix 10.0.1.0/24
az network vnet subnet create \
--resource-group rg-linux-prod \
--vnet-name vnet-prod \
--name data \
--address-prefix 10.0.2.0/24
Step 2: Create an NSG and Attach It to the Web Subnet
az network nsg create \
--resource-group rg-linux-prod \
--name nsg-web
az network vnet subnet update \
--resource-group rg-linux-prod \
--vnet-name vnet-prod \
--name web \
--network-security-group nsg-web
Step 3: Write Azure Network Security Groups Rules
Allow HTTPS from anywhere, allow HTTP, and allow SSH only from a management IP range:
az network nsg rule create \
--resource-group rg-linux-prod \
--nsg-name nsg-web \
--name allow-https \
--priority 100 \
--direction Inbound \
--access Allow \
--protocol Tcp \
--destination-port-ranges 443 \
--source-address-prefixes '*'
az network nsg rule create \
--resource-group rg-linux-prod \
--nsg-name nsg-web \
--name allow-ssh-admin \
--priority 110 \
--direction Inbound \
--access Allow \
--protocol Tcp \
--destination-port-ranges 22 \
--source-address-prefixes 203.0.113.0/24
Notice the design principle: the database subnet gets no NSG rules allowing internet traffic at all, so database servers stay unreachable from outside even if someone later adds a public IP by mistake. Think of NSGs as the cloud equivalent of a host firewall — and still run both, as our UFW configuration guide explains for the operating-system layer.
Step 4: Verify Effective Rules
Rules from subnet NSGs and NIC NSGs combine, which can surprise you. Inspect what actually applies to a VM:
az network nsg list-effective \
--resource-group rg-linux-prod \
--vm-name vm-web-01
The Microsoft Learn overview of Azure Network Security Groups documents rule evaluation order and augmented rules in full detail.
Design Tips That Prevent Expensive Mistakes
A few decisions made on day one are painful to reverse, so make them deliberately. Plan CIDR ranges on paper first: VNets that you may one day peer, or connect to on-premises networks over VPN, must not have overlapping address spaces. Many teams adopt a simple convention — one /16 per environment, /24 per tier — and document it. Use service tags instead of raw IPs in NSG rules where possible: the Storage or Sql tags track Microsoft’s published ranges automatically. Consider Application Security Groups once rule counts grow: instead of writing rules per IP, you tag NICs as “web servers” or “database servers” and write rules between groups, which reads like the architecture diagram. Finally, remember that outbound traffic is allowed by default; if compliance requires egress control, plan for Azure Firewall or an NVA rather than dozens of hand-written deny rules.
Troubleshooting
- Traffic still blocked after adding an Allow rule: check priority ordering — a Deny rule with a lower number wins. Also check whether a second NSG on the NIC is denying the traffic.
- VMs in two subnets cannot talk: the default rules allow VNet-internal traffic, so look for a custom Deny rule or a host firewall on the target machine.
- Rule updates feel slow: NSG changes usually apply within a minute or two; if a flow still fails, use Network Watcher’s IP flow verify in the portal to see exactly which rule decided the packet’s fate.
Conclusion
A well-planned VNet with segmented subnets plus carefully prioritized Azure Network Security Groups gives you a network that is secure by default and auditable with a few CLI commands. Design the address space before you deploy, keep SSH restricted to admin ranges, and let the deny-by-default rule do its job everywhere else.