Skip to content

NSGs - Configure

🎯 Lab Objective

In this hands-on lab, you will learn how to:

  • Create Network Security Groups (NSGs) for different network tiers
  • Associate NSGs with subnets and network interfaces
  • Configure security rules to control inbound and outbound traffic
  • Test connectivity between different network segments
  • Implement network segmentation using NSG rules
  • Understand the difference between subnet-level and NIC-level NSG associations

Goal: Implement network security controls to segment web and application tiers while maintaining necessary connectivity.


πŸ—οΈ Pre-Provisioned Environment

The following Azure resources have been pre-deployed in your environment:

Resource Overview

Resource TypeResource NameConfigurationPurpose
Resource GroupNSG-Lab-RGContains all lab resourcesLogical container
Virtual NetworkNSG-Lab-VNetAddress space: 10.0.0.0/16Network foundation
Web SubnetWebSubnetRange: 10.0.1.0/24Web tier network
App SubnetAppSubnetRange: 10.0.2.0/24Application tier network
Web VMWebVMWindows Server with RDPWeb tier server
App VMAppVMUbuntu Linux with SSHApplication tier server
Public IPsVariousExternal connectivityRemote access

Network Architecture

NSG-Lab-VNet (10.0.0.0/16)
β”œβ”€β”€ WebSubnet (10.0.1.0/24)
β”‚ └── WebVM (Windows Server)
β”‚ └── WebSubnet-NSG (to be created)
└── AppSubnet (10.0.2.0/24)
└── AppVM (Ubuntu Linux)
└── AppSubnet-NSG (to be created)

VM Details

VMOperating SystemPrivate IPPublic IPAccess Method
WebVMWindows Server 201910.0.1.4DynamicRDP (port 3389)
AppVMUbuntu 20.04 LTS10.0.2.4DynamicSSH (port 22)

πŸš€ Lab Exercises

Part 1: Create Network Security Groups

Step 1: Navigate to NSG Creation

  1. Open the Azure Portal
  2. Search for β€œNetwork security groups”
  3. Click + Create

Step 2: Create Web Tier NSG

SettingValueNotes
SubscriptionYour subscriptionPre-selected
Resource groupNSG-Lab-RGUse existing
NameWebSubnet-NSGWeb tier security
RegionSame as VNetMust match
  1. Click Review + create
  2. Click Create

Step 3: Create App Tier NSG

Repeat the process with these settings:

SettingValueNotes
NameAppSubnet-NSGApp tier security
Resource groupNSG-Lab-RGSame as before
RegionSame as VNetMust match

⏱️ Creation Time: NSGs typically create within 1-2 minutes.


Part 2: Associate NSGs with Subnets

Step 1: Associate Web Subnet NSG

  1. Navigate to NSG-Lab-RG resource group
  2. Click on WebSubnet-NSG
  3. In the left menu, select Subnets
  4. Click + Associate
SettingValueNotes
Virtual networkNSG-Lab-VNetSelect the VNet
SubnetWebSubnetWeb tier subnet
  1. Click OK

Step 2: Associate App Subnet NSG

  1. Click on AppSubnet-NSG
  2. In the left menu, select Subnets
  3. Click + Associate
SettingValueNotes
Virtual networkNSG-Lab-VNetSelect the VNet
SubnetAppSubnetApp tier subnet
  1. Click OK

πŸ” Verification: Each NSG should show one associated subnet in the Subnets blade.


Part 3: Configure Web Tier Security Rules

Step 1: Allow RDP Access to Web Tier

  1. Navigate to WebSubnet-NSG
  2. In the left menu, click Inbound security rules
  3. Click + Add

Step 2: Configure RDP Rule

SettingValuePurpose
SourceMy IP addressYour current IP
Source port ranges*Any source port
DestinationAnyAny destination
ServiceRDPPre-configured RDP
ActionAllowPermit traffic
Priority1000Rule precedence
NameAllow-RDP-MyIPDescriptive name
DescriptionAllow RDP from my IPRule purpose
  1. Click Add

Step 3: Block Inter-Subnet Communication

  1. Click + Add for another inbound rule
SettingValuePurpose
SourceIP AddressesSpecific IP range
Source IP addresses/CIDR ranges10.0.2.0/24AppSubnet range
Source port ranges*Any source port
DestinationAnyAny destination
ServiceCustomCustom rule
Destination port ranges*All ports
ProtocolAnyAll protocols
ActionDenyBlock traffic
Priority1100Lower than allow rules
NameBlock-From-AppSubnetDescriptive name
  1. Click Add

Part 4: Configure App Tier Security Rules

Step 1: Allow SSH Access to App Tier

  1. Navigate to AppSubnet-NSG
  2. In the left menu, click Inbound security rules
  3. Click + Add

Step 2: Configure SSH Rule

SettingValuePurpose
SourceMy IP addressYour current IP
Source port ranges*Any source port
DestinationAnyAny destination
ServiceSSHPre-configured SSH
ActionAllowPermit traffic
Priority1000Rule precedence
NameAllow-SSH-MyIPDescriptive name
  1. Click Add

Step 3: Allow Web Tier Communication (Optional)

  1. Click + Add for another rule
SettingValuePurpose
SourceIP AddressesSpecific IP range
Source IP addresses/CIDR ranges10.0.1.0/24WebSubnet range
DestinationAnyAny destination
ServiceCustomCustom rule
Destination port ranges80,443HTTP/HTTPS
ProtocolTCPWeb protocols
ActionAllowPermit traffic
Priority1200After other rules
NameAllow-Web-To-AppDescriptive name
  1. Click Add

Part 5: Test Network Connectivity

Step 1: Connect to Web VM

  1. Navigate to WebVM in your resource group
  2. Click Connect β†’ RDP
  3. Download the RDP file and connect using:
    • Username: azureuser
    • Password: LabPassword123!

Step 2: Test Connectivity from Web VM

Once connected to WebVM, open Command Prompt and run:

Terminal window
# Test connectivity to App VM (should fail due to NSG rule)
ping 10.0.2.4
# Test internet connectivity (should succeed)
ping 8.8.8.8
# Test DNS resolution
nslookup google.com

Expected Results:

  • ❌ Ping to AppVM (10.0.2.4) should fail or timeout
  • βœ… Ping to internet (8.8.8.8) should succeed
  • βœ… DNS resolution should work

Step 3: Connect to App VM

  1. Navigate to AppVM in your resource group
  2. Click Connect β†’ SSH
  3. Use Azure Cloud Shell or your preferred SSH client:
Terminal window
ssh azureuser@[AppVM-Public-IP]
# Password: LabPassword123!

Step 4: Test Connectivity from App VM

Once connected to AppVM, run:

Terminal window
# Test connectivity to Web VM (behavior depends on NSG rules)
ping 10.0.1.4
# Test internet connectivity (should succeed)
ping 8.8.8.8
# Test web access
curl -I http://google.com

Expected Results:

  • ❓ Ping to WebVM depends on your NSG configuration
  • βœ… Internet connectivity should work
  • βœ… Web access should function

Part 6: Advanced NSG Configuration

Step 1: Enable ICMP Between Subnets

  1. Navigate to AppSubnet-NSG
  2. Click Inbound security rules
  3. Find the rule blocking WebSubnet traffic
  4. Click + Add to create an exception:
SettingValuePurpose
SourceIP AddressesWebSubnet
Source IP addresses10.0.1.0/24Web tier
ServiceCustomICMP protocol
ProtocolICMPPing protocol
ActionAllowPermit pings
Priority1050Higher than deny rule
NameAllow-ICMP-From-WebDescriptive
  1. Click Add

Step 2: Test ICMP Connectivity

From WebVM command prompt:

Terminal window
# This should now succeed
ping 10.0.2.4

Step 3: Use Service Tags

  1. Create a new rule using Service Tags:
SettingValuePurpose
SourceService TagAzure service
Source service tagVirtualNetworkAll VNet traffic
DestinationAnyAny destination
ServiceCustomCustom ports
Destination port ranges8080Custom app port
ActionAllowPermit traffic

Part 7: Alternative NIC-Level Association

Step 1: Disassociate from Subnet

  1. Navigate to WebSubnet-NSG
  2. Click Subnets
  3. Select the associated subnet
  4. Click Dissociate
  5. Confirm the action

Step 2: Associate with Network Interface

  1. In WebSubnet-NSG, click Network interfaces
  2. Click + Associate
  3. Select the WebVM network interface
  4. Click OK

Step 3: Test Differences

Compare the behavior:

Association LevelScopeManagement
SubnetAll resources in subnetCentralized
NICIndividual VM onlyGranular

Part 8: Monitor and Analyze Traffic

Step 1: View Effective Security Rules

  1. Navigate to WebVM
  2. Click Networking in the left menu
  3. Click Effective security rules
  4. Observe: Combined rules from NSG and default rules

Step 2: Enable NSG Flow Logs (Optional)

  1. Navigate to WebSubnet-NSG
  2. Click NSG flow logs in the left menu
  3. Click + Create flow log
  4. Configure basic settings:
SettingValuePurpose
NameWebNSG-FlowLogsLog identifier
Storage accountCreate newLog storage
Retention7 daysLog retention
  1. Click Create

πŸ”§ Troubleshooting Guide

Common Issues and Solutions

IssuePossible CauseSolution
Can’t RDP to WebVMRDP rule not configuredAdd inbound rule for RDP (3389)
Can’t SSH to AppVMSSH rule missingAdd inbound rule for SSH (22)
Ping always failsICMP not allowedAdd ICMP allow rule
Rules not workingWrong priority orderCheck rule priorities (lower number = higher priority)
VM not accessibleNSG not associatedVerify NSG-subnet association

NSG Rule Evaluation Order

  1. Explicit Allow Rules (lowest priority number first)
  2. Explicit Deny Rules (lowest priority number first)
  3. Default Rules (system-defined, highest priority numbers)

πŸ§ͺ Additional Experiments

Try these optional exercises to deepen your understanding:

  1. Application Security Groups: Create ASGs for more granular control
  2. Service Tags: Experiment with different Azure service tags
  3. Augmented Security Rules: Use source/destination prefixes
  4. Flow Log Analysis: Analyze traffic patterns using flow logs
  5. Multiple NSGs: Test behavior with NSGs on both subnet and NIC

πŸŽ“ Key Takeaways

After completing this lab, you should understand:

  • NSGs provide stateful firewalling for Azure resources
  • Subnet-level NSGs affect all resources in the subnet
  • NIC-level NSGs provide granular, per-VM control
  • Rule priorities determine evaluation order (lower number = higher priority)
  • Default rules allow VNet-to-VNet and outbound internet traffic
  • Service tags simplify rule creation for Azure services
  • Flow logs enable traffic analysis and troubleshooting

πŸ“Š NSG Rule Best Practices

Security Principles

  • Principle of Least Privilege: Only allow necessary traffic
  • Defense in Depth: Use multiple security layers
  • Regular Review: Audit rules periodically

Rule Organization

  • Use descriptive names for easy identification
  • Set appropriate priorities to avoid conflicts
  • Document rule purposes in descriptions
  • Group related rules logically

Common Patterns

  • Management Access: Restrict admin ports to specific IPs
  • Application Traffic: Allow only required application ports
  • Inter-Tier Communication: Control traffic between application tiers
  • Internet Access: Limit outbound internet connectivity

πŸ“š Additional Resources