Azure Networking Tutorials

Understanding Network Security Groups (NSGs) in Azure

Network Security Groups (NSGs) are a fundamental component of network security in Azure. They act as a basic firewall for your virtual machines (and other Azure resources) to control inbound and outbound network traffic. This tutorial will guide you through the concepts, creation, and management of NSGs.

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 (VNet). NSGs can be associated with:

When you associate an NSG with a subnet and a NIC, Azure applies the rules from both. The order of evaluation and priority of these rules are crucial.

Key Concepts

Important: Default Rules

When an NSG is created, it automatically includes a set of default rules. These default rules cannot be deleted but their priorities can be changed. They include rules for allowing inbound/outbound traffic on essential ports and denying all other traffic by default.

Creating a Network Security Group

You can create an NSG using the Azure portal, Azure CLI, Azure PowerShell, or ARM templates.

Using the Azure Portal:

  1. Navigate to the Azure portal.
  2. Search for "Network security groups" and select it.
  3. Click "Create".
  4. Fill in the subscription, resource group, name, and region.
  5. Click "Review + create" and then "Create".

Associating an NSG with a Subnet or NIC:

After creating an NSG, you need to associate it:

  1. Go to your Network Security Group resource.
  2. Under "Settings", select "Network Interfaces" or "Subnets".
  3. Click "Associate" and choose the desired NIC or subnet.

Managing Security Rules

Once an NSG is created and associated, you can add, modify, or delete security rules.

Adding a Custom Rule:

  1. Navigate to your Network Security Group.
  2. Under "Settings", select "Security rules".
  3. Click "Add".
  4. Provide a name, priority, source/destination details, protocol, port, and action (Allow/Deny).
  5. Click "Add".
Example: Allowing SSH Inbound Traffic

# Azure CLI command example
az network nsg rule create \
  --resource-group <YourResourceGroup> \
  --nsg-name <YourNSGName> \
  --name AllowSSHInbound \
  --priority 300 \
  --direction Inbound \
  --access Allow \
  --protocol Tcp \
  --src-port-range '*' \
  --dst-port-range 22 \
  --src-address-prefix '*' \
  --dst-address-prefix '*'
            

Tip: Use Service Tags

Instead of specific IP addresses for sources or destinations like Internet or VirtualNetwork, consider using Azure service tags. These tags represent IP address groups of Azure services and are automatically updated by Azure.

Inbound vs. Outbound Security Rules

Rule Processing Order

Azure processes NSG rules in the following order:

  1. Default Inbound Rules (lowest priority first)
  2. Custom Inbound Rules (lowest priority first)
  3. Default Outbound Rules (lowest priority first)
  4. Custom Outbound Rules (lowest priority first)

The first rule that matches the traffic in terms of priority, direction, protocol, and IP addresses determines whether the traffic is allowed or denied. If no rule matches, the traffic is denied by default.

Best Practices

Note: NSGs vs. Azure Firewall

While NSGs provide essential network filtering, Azure Firewall offers more advanced, centralized firewall capabilities, including threat intelligence-based filtering, web filtering, and network traffic analytics. NSGs are often used in conjunction with Azure Firewall for defense-in-depth.