Azure Network Security Group (NSG) Rules

This document provides a comprehensive guide to understanding and configuring Network Security Group (NSG) rules in Azure. NSGs are a fundamental component for network security in Azure, allowing you to filter network traffic to and from Azure resources in an Azure virtual network.

What is a Network Security Group?

A Network Security Group (NSG) contains a list of security rules that allow or deny network traffic to resources connected to Azure Virtual Network. NSGs can be associated with Network Interfaces (NICs) or subnets, or both. When an NSG is associated with both, the rules applied are for the NIC. If only associated with a subnet, the rules apply to all resources in that subnet.

Key Concepts

Types of NSG Rules

NSGs have two types of rules:

Default Security Rules:
Every NSG has a set of default rules that cannot be deleted. These rules provide basic network connectivity and are applied before custom rules.
Custom Security Rules:
You can create custom rules to define your specific security requirements. Custom rules are processed after the default rules.

Default Rule Examples

Name Priority Protocol Source Destination Ports Action
AllowVnetInBound 65500 Any VirtualNetwork VirtualNetwork * Allow
AllowAzureLoadBalancerInBound 65501 Any AzureLoadBalancer Any * Allow
DenyAllInbound 65502 Any Any Any * Deny
AllowVnetOutBound 65500 Any Any VirtualNetwork * Allow
AllowInternetOutBound 65501 Any Any Internet * Allow
DenyAllOutbound 65502 Any Any Any * Deny

Creating and Managing NSG Rules

You can manage NSG rules using the Azure portal, Azure CLI, Azure PowerShell, or REST API.

Using the Azure Portal

  1. Navigate to your Network Security Group resource in the Azure portal.
  2. Under "Settings," select "Inbound security rules" or "Outbound security rules."
  3. Click "Add" to create a new rule.
  4. Configure the rule's priority, name, source, destination, protocol, ports, and action.
  5. Click "Add" to save the rule.

Example: Allowing SSH access

To allow inbound SSH (TCP port 22) from a specific IP address range:


# Azure CLI Example (Conceptual)
az network nsg rule create \
  --resource-group MyResourceGroup \
  --nsg-name MyNSG \
  --name AllowSSH \
  --priority 300 \
  --protocol Tcp \
  --destination-port-ranges 22 \
  --access Allow \
  --direction Inbound \
  --source-address-prefixes '203.0.113.0/24'
            

Best Practices

Important: NSGs operate at the network layer (Layer 4) and do not inspect packet content. For application-layer filtering, consider Azure Firewall or Web Application Firewall (WAF).
Security Alert: Avoid using Any for source or destination unless absolutely necessary and thoroughly understood. Similarly, be cautious with wide port ranges.

Next Steps

Learn more about Network Security Groups and related Azure networking features: