Network Security Groups (NSG)

A Network Security Group (NSG) is a fundamental component of Azure's networking services. It acts as a virtual firewall for your virtual machines (VMs) and other network resources, enabling you to control inbound and outbound network traffic at the network interface (NIC) or subnet level.

What is a Network Security Group?

An NSG contains a list of security rules that allow or deny network traffic to resources connected to Azure Virtual Networks (VNets). Each NSG consists of:

These rules are evaluated based on priority, with lower numbers indicating higher priority. When a match is found, the rule is applied, and processing stops.

Key Concepts

Creating and Managing NSGs

You can manage NSGs through the Azure portal, Azure CLI, Azure PowerShell, or ARM templates.

Using Azure CLI:

# Create a Network Security Group
az network nsg create --resource-group MyResourceGroup --name MyNSG

# Associate an NSG with a subnet
az network vnet subnet update --resource-group MyResourceGroup --vnet-name MyVNet --name MySubnet --network-security-group MyNSG

# Add an inbound security rule to allow SSH traffic (port 22)
az network nsg rule create --resource-group MyResourceGroup --nsg-name MyNSG --name AllowSSH --protocol Tcp --priority 100 --destination-port-range 22 --access Allow --direction Inbound

# Add an outbound security rule to deny all traffic to a specific IP range
az network nsg rule create --resource-group MyResourceGroup --nsg-name MyNSG --name DenySpecificOutbound --protocol '*' --priority 110 --destination-address-prefixes "192.168.1.0/24" --access Deny --direction Outbound

Important Considerations:

  • By default, Azure creates two NSGs for each VNet: one for the subnet and one for the VM's NIC. These often contain default rules.
  • The order of rules is crucial. A deny rule with a lower priority number will override an allow rule with a higher priority number if both apply to the same traffic.
  • Always consider the "DenyAllInbound" and "DenyAllOutbound" default rules, which have the lowest priority (4096).

NSG Rule Priorities and Defaults

When you create an NSG, it comes with a set of default rules. These are processed first:

  1. AllowVNetInBound: Allows traffic from other VNets. (Priority 65000)
  2. AllowAzureLoadBalancerInBound: Allows Azure load balancer health probes to reach VMs. (Priority 65001)
  3. DenyAllInbound: Denies all inbound traffic not explicitly allowed by other rules. (Priority 65500)
  4. AllowVNetOutBound: Allows traffic to other VNets. (Priority 65000)
  5. DenyAllOutbound: Denies all outbound traffic not explicitly allowed by other rules. (Priority 65500)

You can define your custom rules with priorities ranging from 100 to 4096. Your custom rules are evaluated before the default rules.

Best Practice: It's generally recommended to associate NSGs at the subnet level for consistent security policies across resources. Associate NSGs at the NIC level only when you need specific exceptions for individual VMs.

Security Best Practices