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:
- Subnets: Rules applied to a subnet affect all resources within that subnet.
- Network Interfaces (NICs): Rules applied to a NIC affect only that specific resource.
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
- Security Rules: Each NSG consists of multiple security rules. These rules are numbered and processed in order of priority.
- Priority: A number from 100 to 4096. Lower numbers have higher priority. Azure processes rules starting from the lowest priority number.
- Direction: Rules can be for Inbound or Outbound traffic.
- Protocol: TCP, UDP, ICMP, or Any.
- Source/Destination: Specify IP addresses, CIDR blocks, service tags, or application security groups.
- Source/Destination Port Ranges: Define the ports for traffic.
- Action: Allow or Deny.
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:
- Navigate to the Azure portal.
- Search for "Network security groups" and select it.
- Click "Create".
- Fill in the subscription, resource group, name, and region.
- Click "Review + create" and then "Create".
Associating an NSG with a Subnet or NIC:
After creating an NSG, you need to associate it:
- Go to your Network Security Group resource.
- Under "Settings", select "Network Interfaces" or "Subnets".
- 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:
- Navigate to your Network Security Group.
- Under "Settings", select "Security rules".
- Click "Add".
- Provide a name, priority, source/destination details, protocol, port, and action (Allow/Deny).
- Click "Add".
# 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
- Inbound Rules: Control traffic coming *into* your Azure resources (e.g., from the internet or other VNets).
- Outbound Rules: Control traffic going *out* from your Azure resources (e.g., to the internet or other VNets).
Rule Processing Order
Azure processes NSG rules in the following order:
- Default Inbound Rules (lowest priority first)
- Custom Inbound Rules (lowest priority first)
- Default Outbound Rules (lowest priority first)
- 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
- Use the principle of least privilege: Only allow the traffic that is absolutely necessary.
- Group NSGs logically with subnets or application components.
- Utilize service tags for simplified management.
- Regularly review your NSG rules.
- Consider using Application Security Groups (ASGs) for more granular control over application workloads.
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.