Network Security Groups (NSGs)

Azure Network Security Groups (NSGs) are a fundamental building block for network security in Azure. They allow you to define inbound and outbound security rules to filter network traffic to and from Azure resources in an Azure virtual network, as well as on-premises resources. NSGs act as a basic firewall at the network layer.

What is a Network Security Group?

An NSG contains a list of security rules that allow or deny network traffic. Each rule has:

Key Concepts

Security Rules

NSGs enforce security policies through security rules. There are two types of security rules:

Each NSG has default rules that are created automatically. You can override these defaults with your own custom rules.

Default Rules

When you create an NSG, it comes with the following default security rules:

Association

You can associate an NSG with:

If an NSG is associated with both a subnet and a NIC, the rules from both NSGs are applied. The rules are processed in the following order: Network Interface NSG rules, then Subnet NSG rules.

Use Cases

Creating and Managing NSGs

You can create and manage NSGs using the Azure portal, Azure PowerShell, Azure CLI, or ARM templates.

Example: Azure CLI to Create an NSG

az network nsg create --resource-group MyResourceGroup --name MyNsg

Example: Azure CLI to Add an Inbound Rule

az network nsg rule create --resource-group MyResourceGroup --nsg-name MyNsg --name AllowHTTP --protocol tcp --priority 100 --destination-port-range 80 --access Allow --direction Inbound
Important: Rule priorities are crucial. Lower numbers are processed first. Be careful when defining deny rules, as they can unintentionally block legitimate traffic if not ordered correctly.

NSGs vs. Azure Firewall

While NSGs provide network layer filtering, Azure Firewall is a cloud-native, intelligent network firewall that protects your virtual network resources. Azure Firewall offers more advanced features like threat intelligence-based filtering, centralized logging, and application-level filtering.

Tip: For simpler scenarios, NSGs are sufficient. For more complex or centralized security requirements, consider Azure Firewall.
graph LR A[Client] -- HTTPS --> B(Internet); B -- Port 443 --> C(Azure Firewall); C -- Port 443 --> D(VNet); D -- Port 443 --> E(NSG - Allow HTTP); E -- Port 443 --> F(Web Server VM);

This diagram illustrates a common scenario where traffic first passes through Azure Firewall and then is subjected to NSG rules before reaching the VM.

Best Practices

Learn about Azure Firewall Back to Virtual Networks