Network security group

Also known as: NSG

On this page 7

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.

FieldMeaningExample value
PriorityOrder of evaluation, lower number first110
DirectionInbound or outboundInbound
Source and destinationIP range, service tag or application security groupInternet, 10.1.2.0/24, AppGateway
Port rangeSingle port, range or any443, 1024-65535
ProtocolTCP, UDP, ICMP or anyTCP
ActionAllow or denyAllow
Fields of a network security group rule

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 nameDirectionPriorityEffect
AllowVnetInBoundInbound65000Allows traffic within the virtual network
AllowAzureLoadBalancerInBoundInbound65001Allows health probes from the load balancer
DenyAllInBoundInbound65500Blocks all other inbound traffic
AllowVnetOutBoundOutbound65000Allows traffic to the virtual network
AllowInternetOutBoundOutbound65001Allows outbound internet access
DenyAllOutBoundOutbound65500Blocks all other outbound traffic
Default inbound and outbound rules in an NSG

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.

ControlPlatformAttached toDeny rulesInspection depth
Network security groupAzureSubnet or network interfaceYes, by priorityIP, port, protocol
AWS security groupAWSElastic network interfaceNo, allow onlyIP, port, protocol
AWS network ACLAWSSubnetYes, statelessIP, port, protocol
Azure FirewallAzureHub virtual networkYesDomain names, threat intelligence, TLS
Google Cloud firewall rulesGoogle CloudVPC network with tagsYes, by priorityIP, port, protocol
Network security group and related controls

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.

SymptomCauseFix
Virtual machine reachable from anywhereSource set to Any or InternetRestrict sources to known ranges or tags
Load balancer marks backends unhealthyCustom deny blocks probe trafficKeep AzureLoadBalancer inbound allowed
Allowed traffic still blockedSecond NSG on subnet or interface denies itCheck effective security rules
Lateral movement possible inside networkAllowVnetInBound left in placeAdd deny rules between tiers
Rule limit reachedMany single-address rulesConsolidate with tags and address prefixes
Frequent NSG problems and their fixes

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.