Azure Documentation

Creating Network Security Group Rules

This tutorial guides you through the process of creating and managing Network Security Group (NSG) rules in Azure. NSG rules control inbound and outbound traffic to Azure resources.

Understanding NSG Rules

An NSG contains a list of security rules that allow or deny network traffic to resources connected to Azure Virtual Network (VNet). NSGs are associated with network interfaces (NICs) or subnets. When traffic is directed to a resource within an Azure VNet, the NSG associated with the network interface or subnet that the traffic is destined for, processes the traffic.

Each rule has the following properties:

Creating a Rule via Azure Portal

Follow these steps to create a new NSG rule using the Azure portal:

  1. Navigate to the Azure portal and select your Network Security Group.
  2. In the left-hand menu, under Settings, select Inbound security rules or Outbound security rules depending on the direction of traffic you want to control.
  3. Click the + Add button to create a new rule.
  4. Configure the rule properties:
    • Source and Destination: Choose from options like Any, IP Addresses, Service Tags, or Application Security Groups.
    • Source port ranges and Destination port ranges: Enter the desired port numbers or ranges.
    • Protocol: Select TCP, UDP, ICMP, or Any.
    • Action: Choose Allow or Deny.
    • Priority: Assign a unique priority number. Ensure it doesn't conflict with existing rules.
    • Name: Provide a descriptive name for the rule.
    • Description (Optional): Add a brief explanation of the rule's purpose.
  5. Click Add to save the new rule.

Example: Allowing SSH Traffic

To allow inbound SSH traffic (TCP port 22) from any source to a virtual machine:

Example: Denying Outbound HTTP Traffic

To deny outbound HTTP traffic (TCP port 80) to any destination:

Important: Remember that NSGs process rules in order of priority. A lower number signifies a higher priority. The default rules are processed last. Explicitly defined rules should have a lower priority number than the default rules they intend to override.

Rule Priorities and Default Rules

Every NSG comes with a set of default inbound and outbound rules. These rules cannot be deleted but can be overridden by rules with a lower priority number. Understanding these default rules is crucial:

Direction Priority Source Destination Protocol Port Action Description
Inbound 65500 Any Any Any * Deny Denies all inbound traffic.
Inbound 65501 VirtualNetwork VirtualNetwork Any * Allow Allows all traffic within the virtual network.
Inbound 65502 AzureLoadBalancer Any Any * Allow Allows Azure Load Balancer health probes.
Outbound 65500 Any Any Any * Deny Denies all outbound traffic.
Outbound 65501 VirtualNetwork VirtualNetwork Any * Allow Allows all traffic within the virtual network.
Outbound 65502 Any Internet Any * Allow Allows all outbound traffic to the internet.

Warning: Be cautious when modifying or creating rules. Incorrectly configured rules can block legitimate traffic or expose your resources to unwanted access. Always test your rules after implementation.

Next Steps

Now that you know how to create NSG rules, learn how to associate NSGs with subnets and network interfaces.