A network security group (NSG) is a Microsoft Azure resource that contains an ordered list of stateful allow and deny rules for filtering network traffic to and from resources in an Azure Virtual Network. In cloud networking, the NSG is the basic layer 3 and layer 4 access control in Azure: each rule matches source, destination, port and protocol, and the platform enforces it at the subnet or network interface level.
An NSG plays a role similar to a security group in AWS, but with two important differences: it supports explicit deny rules with numeric priorities, and it can be attached to whole subnets as well as individual network interfaces. It is not a full firewall, because it does not inspect application content or domain names.
How a network security group evaluates traffic
An NSG evaluates rules in order of priority, from the lowest number to the highest, and stops at the first rule that matches. Priorities range from 100 to 4096, so a deny rule at priority 200 overrides an allow rule at priority 300 for the same traffic.
Filtering is stateful: when an inbound connection is allowed, the return traffic of that flow is allowed automatically, and the same applies to outbound connections. Administrators therefore write rules for the direction in which a connection starts, not for both directions of every flow.
| Field | Meaning | Example value |
|---|---|---|
| Priority | Order of evaluation, lower number first | 110 |
| Direction | Inbound or outbound | Inbound |
| Source and destination | IP range, service tag or application security group | Internet, 10.1.2.0/24, AppGateway |
| Port range | Single port, range or any | 443, 1024-65535 |
| Protocol | TCP, UDP, ICMP or any | TCP |
| Action | Allow or deny | Allow |
Default rules, service tags and application security groups
Every NSG contains six default rules that cannot be deleted, only overridden by custom rules with a lower priority number. They allow traffic inside the virtual network and from the Azure load balancer, allow outbound internet access and deny all other inbound traffic.
| Rule name | Direction | Priority | Effect |
|---|---|---|---|
| AllowVnetInBound | Inbound | 65000 | Allows traffic within the virtual network |
| AllowAzureLoadBalancerInBound | Inbound | 65001 | Allows health probes from the load balancer |
| DenyAllInBound | Inbound | 65500 | Blocks all other inbound traffic |
| AllowVnetOutBound | Outbound | 65000 | Allows traffic to the virtual network |
| AllowInternetOutBound | Outbound | 65001 | Allows outbound internet access |
| DenyAllOutBound | Outbound | 65500 | Blocks all other outbound traffic |
Service tags such as Internet, VirtualNetwork, Storage or AzureCloud represent groups of IP prefixes that Microsoft maintains, which removes the need to track address ranges by hand. Application security groups go further and let rules refer to logical roles such as web or database servers, so the rule set stays the same when virtual machines are added or replaced.
NSG compared with other cloud traffic controls
An NSG is one layer in a defense-in-depth design, and it is often compared with controls on other platforms and with dedicated firewalls. The comparison below shows where each control fits.
| Control | Platform | Attached to | Deny rules | Inspection depth |
|---|---|---|---|---|
| Network security group | Azure | Subnet or network interface | Yes, by priority | IP, port, protocol |
| AWS security group | AWS | Elastic network interface | No, allow only | IP, port, protocol |
| AWS network ACL | AWS | Subnet | Yes, stateless | IP, port, protocol |
| Azure Firewall | Azure | Hub virtual network | Yes | Domain names, threat intelligence, TLS |
| Google Cloud firewall rules | Google Cloud | VPC network with tags | Yes, by priority | IP, port, protocol |
In a hub-and-spoke network, NSGs typically protect individual subnets in the spokes, while a central cloud firewall handles outbound internet traffic and traffic between spokes. NSGs alone cannot filter by fully qualified domain name, which is why outbound control to specific SaaS endpoints usually requires Azure Firewall or a third-party appliance.
Designing NSG rules in practice
When an NSG is attached to both a subnet and a network interface, traffic must pass both. Inbound traffic is evaluated first by the subnet NSG and then by the interface NSG, outbound traffic in the reverse order, and a deny in either one blocks the flow. Most teams therefore apply NSGs at the subnet level only to keep troubleshooting simple.
- Leave gaps between priorities, for example steps of 10, so new rules can be inserted without renumbering.
- Prefer service tags and application security groups over raw IP ranges wherever they fit.
- Never expose management ports such as 22 or 3389 to the Internet tag; use Azure Bastion or a VPN gateway instead.
- Manage rules as code with Bicep or Terraform, so every change is reviewed and auditable.
- Enable flow logs to record allowed and denied flows for troubleshooting and security analysis.
Azure Network Watcher offers IP flow verify and effective security rules views, which show which rule allows or blocks a given flow. Flow logging is moving from NSG flow logs to virtual network flow logs, which cover a whole network and are the recommended option for new deployments.
Limitations and common mistakes
An NSG is simple to create but easy to misconfigure as rule sets grow. Most incidents come from overly broad allow rules or from misunderstanding how default rules and priorities interact.
| Symptom | Cause | Fix |
|---|---|---|
| Virtual machine reachable from anywhere | Source set to Any or Internet | Restrict sources to known ranges or tags |
| Load balancer marks backends unhealthy | Custom deny blocks probe traffic | Keep AzureLoadBalancer inbound allowed |
| Allowed traffic still blocked | Second NSG on subnet or interface denies it | Check effective security rules |
| Lateral movement possible inside network | AllowVnetInBound left in place | Add deny rules between tiers |
| Rule limit reached | Many single-address rules | Consolidate with tags and address prefixes |
An NSG supports up to 1,000 rules by default, but large rule sets are a sign of weak structure rather than strong security. NSGs also do not apply to every Azure service, so platform services often need private endpoints and service-level firewalls in addition to subnet rules.
Put simply
A network security group is like a guest list at the door of a group of cloud servers. The list says who may come in, through which door and for what purpose, and who must be turned away.
The doorman reads the list from top to bottom and follows the first line that fits the visitor. Anyone not on the list is not let in by default. Once a guest is admitted, the guest may also leave the same way without being checked again. The key point is that only expected traffic gets through.
In practice
A healthcare software provider hosts a patient portal in Azure with three subnets: a web tier behind Azure Application Gateway, an application tier and a database tier running SQL Server on virtual machines. An external audit finds that the database subnet accepts traffic from every machine in the virtual network, which is the default AllowVnetInBound behavior.
The network team defines application security groups for each tier and writes NSG rules in Bicep: the web tier accepts port 443 only from the GatewayManager and application gateway subnet, the application tier accepts port 8443 only from the web group, and the database tier accepts port 1433 only from the application group. A deny rule at priority 4000 blocks all other virtual network traffic, and virtual network flow logs feed the security monitoring system.
The follow-up audit confirms that a compromised web server can no longer reach the database directly. Changes to the rules now go through code review, and lateral movement between tiers is blocked at the network layer.