Set Up Network Security Groups (NSGs) in Azure

Network Security Groups (NSGs) act as a basic firewall for virtual machines and other network resources in Azure. They contain a list of security rules that allow or deny network traffic. This guide walks you through the process of setting up and configuring NSGs.

What are Network Security Groups?

An NSG is a collection of network security rules that are associated with network interfaces (NICs) of virtual machines or with subnets of a virtual network. Each rule specifies a source and destination, port range, and protocol, along with an allowance or denial action.

Prerequisites

Steps to Set Up a Network Security Group

1

Create a Network Security Group

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

Azure Portal:

  1. Navigate to the Azure portal.
  2. Search for "Network security groups" and select it.
  3. Click "+ Create".
  4. Select your Subscription and Resource group.
  5. Provide a Name for your NSG (e.g., `myNsg`).
  6. Choose the Region.
  7. Click "Review + create", then "Create".

Azure CLI:


az network nsg create --resource-group  --name myNsg --location 
        
2

Associate NSG with a Subnet or NIC

You can associate an NSG with a subnet or directly with a network interface (NIC). Associating with a subnet applies the rules to all resources within that subnet. Associating with a NIC applies rules only to that specific virtual machine.

Associate with a Subnet (Recommended):

This is the most common and efficient approach.

  1. In the Azure portal, navigate to your created NSG.
  2. Under "Settings", click "Subnets".
  3. Click "+ Associate".
  4. Select the Virtual network and the Subnet you want to associate the NSG with.
  5. Click "OK".

Associate with a NIC:

If you need more granular control for a specific VM.

  1. Navigate to the Network Interface of your virtual machine.
  2. Under "Settings", click "Network security group".
  3. Click "Edit".
  4. Select your NSG from the dropdown.
  5. Click "Save".
3

Configure Security Rules

NSGs come with default rules. You'll typically want to add custom rules to allow or deny specific traffic.

Default Rules:

Adding a Custom Rule:

  1. In your NSG's page in the Azure portal, click "Inbound security rules" or "Outbound security rules".
  2. Click "+ Add".
  3. Fill in the details for your rule:
    • Source: Any, IP Addresses, Service Tag, Application Security Group.
    • Source port ranges: * (for any) or specific ports.
    • Destination: Any, IP Addresses, Service Tag, Application Security Group.
    • Service: Common services like SSH, RDP, HTTP, HTTPS, or specify Custom.
    • Destination port ranges: Ports required for your service.
    • Protocol: TCP, UDP, ICMP, Any.
    • Action: Allow or Deny.
    • Priority: A number between 100 and 4096. Lower numbers have higher priority. Rules are processed in priority order.
    • Name: A descriptive name for your rule (e.g., AllowSSH).
    • Description: (Optional) More details about the rule.
  4. Click "Add".
Important: Rule processing stops at the first rule that matches the traffic. Therefore, order (priority) is critical. It's best practice to have a DenyAllInbound rule with a high priority (e.g., 4095) as the last rule to catch any traffic not explicitly allowed.

Example: Allowing inbound SSH (TCP port 22)


{
  "name": "AllowSSH",
  "properties": {
    "priority": 100,
    "protocol": "Tcp",
    "access": "Allow",
    "direction": "Inbound",
    "sourceAddressPrefix": "*",
    "sourcePortRange": "*",
    "destinationAddressPrefix": "*",
    "destinationPortRange": "22"
  }
}
        
4

Testing and Verification

After configuring your NSG rules, test connectivity to ensure your rules are behaving as expected. You can use tools like telnet, ping (if ICMP is allowed), or attempt to connect to your services from different network locations.

Note: NSG rules take a moment to propagate. If you don't see immediate results, wait a few minutes and try again.

Best Practices

Conclusion

Network Security Groups are a fundamental component of Azure networking security. By understanding and effectively configuring them, you can significantly enhance the security posture of your Azure resources.