Bird
Raised Fist0
Azurecloud~7 mins

Network Security Groups (NSG) in Azure - Commands & Configuration

Choose your learning style10 modes available
Introduction
Network Security Groups help control traffic to and from your cloud resources by allowing or blocking network connections. They act like a security guard that decides who can enter or leave your virtual network.
When you want to allow only specific types of traffic to your virtual machines, like web traffic on port 80.
When you need to block unwanted access from the internet to your database servers.
When you want to separate different parts of your application with different security rules.
When you want to monitor and control traffic flow in your cloud network for better security.
When you want to quickly update security rules without changing your virtual machines.
Config File - nsg-template.json
nsg-template.json
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Network/networkSecurityGroups",
      "apiVersion": "2022-05-01",
      "name": "example-nsg",
      "location": "eastus",
      "properties": {
        "securityRules": [
          {
            "name": "AllowHTTP",
            "properties": {
              "protocol": "Tcp",
              "sourcePortRange": "*",
              "destinationPortRange": "80",
              "sourceAddressPrefix": "*",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 100,
              "direction": "Inbound"
            }
          },
          {
            "name": "DenyAllInbound",
            "properties": {
              "protocol": "*",
              "sourcePortRange": "*",
              "destinationPortRange": "*",
              "sourceAddressPrefix": "*",
              "destinationAddressPrefix": "*",
              "access": "Deny",
              "priority": 4096,
              "direction": "Inbound"
            }
          }
        ]
      }
    }
  ]
}

This JSON file is an Azure Resource Manager (ARM) template that creates a Network Security Group named example-nsg in the eastus region.

It has two rules: AllowHTTP lets inbound TCP traffic on port 80 from any source, and DenyAllInbound blocks all other inbound traffic as a fallback.

Each rule has a priority number; lower numbers have higher priority.

Commands
This command creates a new Network Security Group named example-nsg in the example-rg resource group located in eastus region.
Terminal
az network nsg create --resource-group example-rg --name example-nsg --location eastus
Expected OutputExpected
{ "NewNSG": { "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Network/networkSecurityGroups/example-nsg", "location": "eastus", "name": "example-nsg", "resourceGroup": "example-rg", "securityRules": [], "type": "Microsoft.Network/networkSecurityGroups" } }
→
--resource-group - Specifies the resource group where the NSG will be created
→
--name - Sets the name of the Network Security Group
→
--location - Defines the Azure region for the NSG
This command adds a rule named AllowHTTP to the example-nsg NSG to allow inbound TCP traffic on port 80 from any source.
Terminal
az network nsg rule create --resource-group example-rg --nsg-name example-nsg --name AllowHTTP --protocol Tcp --direction Inbound --priority 100 --source-address-prefixes '*' --source-port-ranges '*' --destination-address-prefixes '*' --destination-port-ranges 80 --access Allow
Expected OutputExpected
{ "access": "Allow", "description": null, "destinationAddressPrefix": "*", "destinationPortRange": "80", "direction": "Inbound", "name": "AllowHTTP", "priority": 100, "protocol": "Tcp", "sourceAddressPrefix": "*", "sourcePortRange": "*", "provisioningState": "Succeeded" }
→
--priority - Sets the priority of the rule; lower numbers are higher priority
→
--direction - Specifies if the rule applies to inbound or outbound traffic
→
--access - Defines whether to allow or deny the traffic
This command adds a rule named DenyAllInbound to block all other inbound traffic not matched by higher priority rules.
Terminal
az network nsg rule create --resource-group example-rg --nsg-name example-nsg --name DenyAllInbound --protocol '*' --direction Inbound --priority 4096 --source-address-prefixes '*' --source-port-ranges '*' --destination-address-prefixes '*' --destination-port-ranges '*' --access Deny
Expected OutputExpected
{ "access": "Deny", "description": null, "destinationAddressPrefix": "*", "destinationPortRange": "*", "direction": "Inbound", "name": "DenyAllInbound", "priority": 4096, "protocol": "*", "sourceAddressPrefix": "*", "sourcePortRange": "*", "provisioningState": "Succeeded" }
→
--priority - Sets a high priority number to ensure this rule is evaluated after others
→
--access - Blocks the traffic
This command shows the details of the example-nsg to verify the rules are applied correctly.
Terminal
az network nsg show --resource-group example-rg --name example-nsg
Expected OutputExpected
{ "name": "example-nsg", "resourceGroup": "example-rg", "securityRules": [ { "name": "AllowHTTP", "properties": { "access": "Allow", "direction": "Inbound", "priority": 100, "protocol": "Tcp", "sourceAddressPrefix": "*", "sourcePortRange": "*", "destinationAddressPrefix": "*", "destinationPortRange": "80" } }, { "name": "DenyAllInbound", "properties": { "access": "Deny", "direction": "Inbound", "priority": 4096, "protocol": "*", "sourceAddressPrefix": "*", "sourcePortRange": "*", "destinationAddressPrefix": "*", "destinationPortRange": "*" } } ] }
→
--resource-group - Specifies the resource group of the NSG
→
--name - Specifies the NSG name to show details
Key Concept

If you remember nothing else from this pattern, remember: Network Security Groups control who can talk to your cloud resources by setting simple allow or deny rules based on ports, protocols, and directions.

Common Mistakes
Setting the same priority number for multiple rules
Azure requires unique priority numbers; duplicate priorities cause errors or unexpected rule evaluation.
Assign a unique priority number to each rule, with lower numbers evaluated first.
Not specifying the direction of traffic in rules
Rules without direction default to inbound or may not behave as expected, leaving traffic unfiltered.
Always specify --direction as Inbound or Outbound to clearly define the rule's scope.
Forgetting to create a deny rule for unwanted traffic
Without explicit deny rules, some traffic might be allowed by default, exposing resources.
Add a catch-all deny rule with a high priority number to block all traffic not explicitly allowed.
Summary
Create a Network Security Group to start controlling network traffic.
Add rules with specific priorities to allow or deny traffic on certain ports and protocols.
Verify the NSG and its rules to ensure your security settings are applied correctly.

Practice

(1/5)
1. What is the primary purpose of an Azure Network Security Group (NSG)?
easy
A. To monitor application performance
B. To store data securely in Azure Blob Storage
C. To control inbound and outbound traffic to Azure resources
D. To manage user identities and access

Solution

  1. Step 1: Understand NSG function

    NSGs are designed to filter network traffic to and from Azure resources.
  2. Step 2: Identify correct purpose

    They do not store data, manage identities, or monitor performance.
  3. Final Answer:

    To control inbound and outbound traffic to Azure resources -> Option C
  4. Quick Check:

    NSG controls traffic [OK]
Hint: NSG controls network traffic, not data or users [OK]
Common Mistakes:
  • Confusing NSG with storage or identity services
  • Thinking NSG monitors performance
  • Assuming NSG stores data
2. Which of the following is the correct way to specify a rule priority in an NSG rule?
easy
A. Priority is a string describing the rule
B. Priority is an IP address range
C. Priority is a boolean value true or false
D. Priority must be a unique integer between 100 and 4096

Solution

  1. Step 1: Understand NSG rule priority format

    NSG rule priority is a unique integer between 100 and 4096 that determines rule evaluation order.
  2. Step 2: Eliminate incorrect types

    Priority is not a string, boolean, or IP range.
  3. Final Answer:

    Priority must be a unique integer between 100 and 4096 -> Option D
  4. Quick Check:

    Priority = integer 100-4096 [OK]
Hint: Priority is a number from 100 to 4096 [OK]
Common Mistakes:
  • Using strings or booleans for priority
  • Confusing priority with IP ranges
  • Not using unique priority numbers
3. Given this NSG rule:
Priority: 200, Direction: Inbound, Access: Deny, Source: Any, Destination: 10.0.0.4, Protocol: TCP, Port: 22
What happens when a user tries to SSH (port 22) to 10.0.0.4?
medium
A. The connection is denied
B. The connection is allowed only if from a specific IP
C. The connection is allowed
D. The connection is logged but allowed

Solution

  1. Step 1: Analyze rule details

    The rule denies inbound TCP traffic on port 22 to destination 10.0.0.4 from any source.
  2. Step 2: Determine effect on SSH connection

    SSH uses TCP port 22, so the connection attempt matches the deny rule and is blocked.
  3. Final Answer:

    The connection is denied -> Option A
  4. Quick Check:

    Deny inbound TCP port 22 = connection denied [OK]
Hint: Deny inbound on port 22 blocks SSH connections [OK]
Common Mistakes:
  • Assuming 'Any' source means allow
  • Confusing deny with logging
  • Ignoring direction and port in rule
4. You created an NSG rule to allow inbound HTTP traffic on port 80, but the traffic is still blocked. What is the most likely cause?
medium
A. The NSG is not attached to the subnet or network interface
B. A higher priority deny rule blocks port 80 traffic
C. The source IP address is not specified
D. The protocol is set to UDP instead of TCP

Solution

  1. Step 1: Check rule priority and conflicts

    NSG rules are evaluated by priority; a deny rule with higher priority can block traffic despite allow rules.
  2. Step 2: Confirm rule attachment and protocol

    While attachment and protocol matter, the most common cause is a conflicting higher priority deny rule.
  3. Final Answer:

    A higher priority deny rule blocks port 80 traffic -> Option B
  4. Quick Check:

    Higher priority deny overrides allow [OK]
Hint: Check for higher priority deny rules first [OK]
Common Mistakes:
  • Ignoring rule priority order
  • Assuming NSG attachment is always correct
  • Confusing protocol types
5. You want to restrict access so only a specific IP address (203.0.113.5) can access a VM on port 3389 (RDP). Which NSG rule configuration achieves this securely?
hard
A. Priority: 300, Direction: Inbound, Access: Allow, Source: 203.0.113.5, Destination: Any, Protocol: TCP, Port: 3389
B. Priority: 100, Direction: Inbound, Access: Deny, Source: Any, Destination: Any, Protocol: TCP, Port: 3389
C. Priority: 200, Direction: Inbound, Access: Allow, Source: Any, Destination: Any, Protocol: TCP, Port: 3389
D. Priority: 400, Direction: Outbound, Access: Allow, Source: 203.0.113.5, Destination: Any, Protocol: TCP, Port: 3389

Solution

  1. Step 1: Allow only specific IP inbound on port 3389

    Priority: 300, Direction: Inbound, Access: Allow, Source: 203.0.113.5, Destination: Any, Protocol: TCP, Port: 3389 allows inbound TCP traffic on port 3389 only from IP 203.0.113.5, which matches the requirement.
  2. Step 2: Check other options for correctness

    Priority: 100, Direction: Inbound, Access: Deny, Source: Any, Destination: Any, Protocol: TCP, Port: 3389 denies all inbound TCP 3389 traffic, blocking access. Priority: 200, Direction: Inbound, Access: Allow, Source: Any, Destination: Any, Protocol: TCP, Port: 3389 allows all sources, which is insecure. Priority: 400, Direction: Outbound, Access: Allow, Source: 203.0.113.5, Destination: Any, Protocol: TCP, Port: 3389 is outbound, irrelevant for inbound access.
  3. Final Answer:

    Priority: 300, Direction: Inbound, Access: Allow, Source: 203.0.113.5, Destination: Any, Protocol: TCP, Port: 3389 -> Option A
  4. Quick Check:

    Allow inbound TCP 3389 from specific IP [OK]
Hint: Allow inbound from specific IP, deny others by priority [OK]
Common Mistakes:
  • Allowing all sources instead of specific IP
  • Using outbound direction instead of inbound
  • Denying all traffic without exception
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard