Skip to main content

Network Security Group

An Azure Network Security Group (NSG) is a stateful firewall that allows or denies network traffic to resources in a virtual network using prioritised rules. It can be attached to a subnet, a network interface, or both.

Network Security Group

Each rule has a priority from 100 to 4096, a direction, a source and destination, a port, a protocol, and an allow or deny action. Rules are evaluated from the lowest priority number first, and the first match wins. Every NSG also has default rules that allow virtual network traffic and the Azure load balancer, and deny all other inbound traffic.

How Are Subnet and NIC NSGs Evaluated Together?

For inbound traffic, the subnet NSG is evaluated first and then the NIC NSG, and both must allow the traffic. For outbound traffic the order is reversed. A correct-looking rule can be overridden by a deny in the other NSG.

Bash
az network nsg rule create --resource-group mumbai-prod-rg \
--nsg-name db-subnet-nsg --name allow-app-postgres \
--priority 100 --direction Inbound --access Allow --protocol Tcp \
--source-address-prefixes 10.20.1.0/24 --destination-port-ranges 5432

How Do You Troubleshoot Blocked Traffic?

Use Network Watcher IP flow verify and the effective security rules view, which show the combined set of rules actually applied to a NIC.

flowchart LR I(["Inbound traffic"]):::person -->|first| S["Subnet NSG"]:::tool S -->|allowed| N["NIC NSG"]:::tool N -->|allowed| V["VM"]:::cloud classDef person fill:#EDE9FE,color:#1E293B classDef tool fill:#326CE5,color:#FFFFFF classDef cloud fill:#D1FAE5,color:#1E293B

Why It Matters in Production

A delivery platform like Swiggy can allow only the application subnet to reach the database on port 5432, so even a compromised web VM cannot probe the database on other ports.

Security

Never leave RDP (3389) or SSH (22) open to 0.0.0.0/0. Use Azure Bastion or a VPN for management access.

Tip

Use service tags and application security groups in rules instead of raw IP ranges, so rules stay correct as addresses change. Prefer virtual network flow logs, since NSG flow logs are being retired.

Frequently Asked Questions

Are NSGs stateful?

Yes. If an inbound connection is allowed, the return traffic is allowed automatically without a separate rule.

Do I need an NSG on every subnet?

It is strongly recommended. Without one, a subnet relies only on default platform behaviour, so most designs attach an NSG to each subnet and apply least privilege.

How is an NSG different from Azure Firewall?

An NSG is a free Layer 3 and 4 filter attached to subnets or NICs. Azure Firewall is a managed, centralised service with FQDN filtering and threat intelligence.

Found a mistake, an outdated step or a broken link?