Managing Network Security Group Rules

This guide covers the essential operations for managing Network Security Group (NSG) rules in Azure, ensuring granular control over network traffic to and from your Azure resources.

Understanding NSG Rule Components

Each NSG rule consists of several key components:

Creating a New NSG Rule

You can create NSG rules using the Azure portal, Azure CLI, or Azure PowerShell. Here’s an example using the Azure CLI to allow SSH (port 22) from a specific IP address to a subnet:


az network nsg rule create \
  --resource-group MyResourceGroup \
  --nsg-name MyNsg \
  --name AllowSSHInbound \
  --priority 300 \
  --direction Inbound \
  --access Allow \
  --protocol Tcp \
  --source-address-prefixes "203.0.113.0/24" \
  --source-port-ranges "*" \
  --destination-address-prefixes "*" \
  --destination-port-ranges 22
            

Modifying an Existing NSG Rule

To modify a rule, you typically update its properties. For instance, changing the priority or source IP range.

Remember that changing a rule’s priority can significantly affect network traffic flow due to the order of rule evaluation.


az network nsg rule update \
  --resource-group MyResourceGroup \
  --nsg-name MyNsg \
  --name AllowSSHInbound \
  --priority 310 \
  --source-address-prefixes "198.51.100.0/24"
            

Deleting an NSG Rule

Deleting a rule is straightforward. Ensure you understand the impact of removing a rule before proceeding.


az network nsg rule delete \
  --resource-group MyResourceGroup \
  --nsg-name MyNsg \
  --name AllowSSHInbound
            

Best Practices for Managing NSG Rules

Important Consideration: Default Rules

Every NSG comes with a set of default rules. These rules cannot be deleted but their priority can be adjusted if necessary. Be aware of these default rules (e.g., AllowVnetInBound, DenyAllInBound) as they form the baseline for your network security.

Advanced Scenarios

For more in-depth information, please refer to the official Azure Network Security Group documentation.