Network Security Group Fundamentals
Complete the full lesson to earn 25 points — 50 with Pro
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks, the wait, and see fewer ads — read each lesson on a single page with Pro
Network Security Group Fundamentals
Introduction: The Digital Perimeter
In the modern landscape of cloud computing and interconnected data centers, the concept of a "hard shell" perimeter—where everything inside the firewall is trusted and everything outside is hostile—has become obsolete. As organizations migrate their workloads to the cloud, the responsibility for securing these assets shifts toward a model of defense-in-depth. At the heart of this strategy lies the Network Security Group (NSG). An NSG acts as a virtual firewall for your cloud resources, allowing you to filter network traffic to and from virtual machines, subnets, and other network interfaces.
Understanding Network Security Groups is not just a technical requirement for passing certification exams; it is a fundamental skill for anyone responsible for protecting sensitive data. Without properly configured NSGs, your cloud environment is effectively "wide open," exposing your internal services to unauthorized access, brute-force attacks, and lateral movement by malicious actors. By mastering the logic of inbound and outbound rules, priority levels, and service tags, you gain granular control over how your infrastructure communicates. This lesson will guide you through the mechanics of NSGs, providing the knowledge needed to build secure, resilient, and well-monitored network environments.
What is a Network Security Group?
A Network Security Group is essentially a set of access control rules that determine whether network traffic is permitted or denied. Think of an NSG as a security guard stationed at the door of a specific resource. This guard has a list of instructions—the rules—that they must follow religiously. When a packet arrives, the guard checks the "Source IP," "Destination IP," "Protocol," and "Port" against their list to decide if the packet should be allowed to enter or leave.
In most cloud platforms, NSGs operate at the packet level. They are stateless in nature, meaning that for every request, the return traffic must also be explicitly allowed or handled by the inherent stateful nature of the platform's return-path logic. However, it is crucial to remember that NSGs process these rules in a specific order based on a priority number. The lower the number, the higher the priority. Once a rule matches a packet, processing stops, and the action (Allow or Deny) is applied.
Callout: Stateless vs. Stateful Security While NSGs are often described as firewalls, it is important to understand that they function differently than traditional hardware appliances. In many cloud environments, NSGs are "stateful." This means if you allow an inbound request from an external IP, the response traffic is automatically allowed back out, regardless of the outbound rules. This simplifies configuration significantly, as you do not need to manually create return rules for every single inbound connection you permit.
The Anatomy of an NSG Rule
To configure an NSG effectively, you must understand the individual components that make up a rule. Every rule is a combination of parameters that define the traffic flow. If even one parameter is misconfigured, it could lead to security gaps or service disruptions.
1. Priority
Priority is defined by an integer, typically ranging from 100 to 4096. Rules are processed in ascending order of priority. If you have a rule with priority 100 that denies all traffic and a rule with priority 200 that allows web traffic, the "deny" rule will always win because it is processed first. Always leave gaps between your priority numbers (e.g., use 100, 200, 300) so you can insert new rules later without renumbering everything.
2. Source and Destination
You can define traffic flow based on IP addresses, ranges (CIDR blocks), or service tags. Service tags are a powerful feature that represents a group of IP addresses for specific cloud services (like "Storage," "SQL," or "AzureLoadBalancer"). Using service tags is significantly safer and easier to maintain than hardcoding individual IP addresses, which change frequently.
3. Protocol
You specify the transport protocol, such as TCP, UDP, ICMP, or "Any." For most web traffic, you will use TCP. For DNS or streaming services, you might require UDP. Restricting the protocol is a key security practice; for example, if you only need web access, there is no reason to allow UDP traffic to your web server.
4. Port Range
This defines the specific port or range of ports for the traffic. You can specify a single port (e.g., 80), a range (e.g., 8000-8080), or use an asterisk (*) to represent all ports. Always follow the principle of least privilege: only open the ports that are absolutely necessary for the application to function.
5. Action
The action is binary: "Allow" or "Deny." By default, all NSGs come with a set of "Default Rules" that allow communication within a virtual network and between load balancers, while denying all other inbound traffic. You cannot delete these default rules, but you can override them with your own higher-priority rules.
Implementing NSG Rules: Practical Examples
Let’s look at how to implement these rules in a real-world scenario. Imagine you are hosting a web application on a virtual machine. You want to allow users to visit your website via HTTPS, but you want to restrict SSH access to only your office network.
Example 1: Allowing Web Traffic
To allow public access to your web server, you must create an inbound rule:
- Name: AllowHTTPSInbound
- Priority: 100
- Source: Any
- Source Port Ranges: *
- Destination: Any
- Destination Port Ranges: 443
- Protocol: TCP
- Action: Allow
Example 2: Restricting SSH Access
To secure your management interface, you should only allow access from your known office IP address:
- Name: AllowSSHFromOffice
- Priority: 110
- Source: 203.0.113.5/32 (Your static office IP)
- Source Port Ranges: *
- Destination: Any
- Destination Port Ranges: 22
- Protocol: TCP
- Action: Allow
Warning: The "Deny All" Pitfall A common mistake is creating a rule that accidentally blocks all traffic, including the traffic you need to manage the server. Always ensure your "Allow" rules for management (like SSH or RDP) have a higher priority (a lower number) than any broad "Deny" rules. If you lock yourself out, you may need to use a serial console or a cloud-specific portal emergency access feature to regain entry.
Advanced Configuration: Leveraging Service Tags
Managing lists of IP addresses is a nightmare. If your cloud provider updates their IP range for a service like "AzureStorage," your hardcoded rules will become stale, leading to broken applications. Service tags solve this by abstracting the IP ranges.
When you use a service tag, the cloud provider automatically manages the IP addresses associated with that tag. If the provider adds new IP ranges to their storage service, your NSG rule updates automatically.
Common Service Tags:
- Internet: Represents all traffic outside the virtual network.
- VirtualNetwork: Represents all IP addresses within your virtual network.
- AzureLoadBalancer: Represents the health probe IP address for the load balancer.
- Storage/Sql/KeyVault: Specific service tags that allow communication only to the platform's managed services.
Example: Securing a Database
If your application server needs to talk to a SQL database, you should not allow access from the entire virtual network. Instead, use the SQL service tag:
- Name: AllowSQLTraffic
- Source: [Your App Server Subnet]
- Destination: Sql (Service Tag)
- Port: 1433
- Protocol: TCP
- Action: Allow
Step-by-Step: Creating an NSG and Associating it
Implementing an NSG is a multi-step process. You must first create the NSG container, define the rules, and then associate that NSG with either a specific network interface (NIC) or an entire subnet.
Step 1: Create the Network Security Group
- Navigate to the networking section of your cloud dashboard.
- Select "Create Network Security Group."
- Provide a name, region, and resource group.
- Once created, open the NSG and navigate to "Inbound Security Rules."
Step 2: Add Rules
- Click "Add" to create a new rule.
- Fill in the priority (e.g., 100).
- Select the source and destination parameters based on the requirements discussed above.
- Click "Add" to save the rule.
Step 3: Associate the NSG
- Navigate to the "Subnets" or "Network Interfaces" tab within the NSG.
- Click "Associate."
- Select the virtual network and the specific subnet you wish to protect.
- Save the configuration.
Note: Associating an NSG with a subnet is generally preferred over associating it with individual network interfaces. Subnet-level association ensures that every device added to that subnet is automatically protected by the same security posture, reducing the risk of human error when deploying new resources.
Best Practices for Network Security Groups
Security is a moving target. As your infrastructure grows, your NSG configurations can become cluttered and difficult to manage. Adopting a standardized approach from the beginning will save you significant effort in the long run.
1. Principle of Least Privilege
Only open the ports that are strictly necessary. If an application only needs to communicate on port 8080, do not open a range of ports. If a service doesn't need to talk to the internet, ensure there is no outbound path allowed.
2. Use Descriptive Names
Instead of naming rules "Rule1" or "AllowWeb," use descriptive names like "Allow-HTTPS-From-Internet-To-WebServer." This makes auditing and troubleshooting significantly easier for team members who may not be familiar with the original configuration.
3. Maintain an Audit Trail
Regularly review your NSG rules. Over time, developers may add "temporary" rules for testing that are never removed. These "temporary" rules are a primary vector for security breaches. Implement a periodic review cycle (e.g., every 30 days) to prune unused or overly permissive rules.
4. Log and Monitor
Most cloud providers offer flow logging for NSGs. These logs capture information about the traffic that hit your rules—both allowed and denied. Analyzing these logs can help you identify blocked traffic that should be allowed or, more importantly, malicious traffic patterns that suggest an ongoing attack.
5. Avoid "Allow All" Rules
Never use "Any" as a source or destination unless absolutely required. If you must allow traffic from the internet, always specify the destination IP or service tag. Using "Any" for both source and destination creates a massive security hole that is easily exploited.
Comparison Table: NSG vs. Application Gateway vs. Firewall
It is common to confuse NSGs with other network security appliances. Here is a quick reference to distinguish them:
| Feature | Network Security Group (NSG) | Application Gateway | Cloud Firewall |
|---|---|---|---|
| Layer | Layer 3 & 4 (IP/Port) | Layer 7 (HTTP/HTTPS) | Layer 3-7 (Full Stack) |
| Primary Use | Subnet/NIC filtering | Web traffic routing/WAF | Centralized egress/ingress |
| Complexity | Low | Medium | High |
| Cost | Usually Free | Higher (Hourly) | Higher (Hourly) |
| Stateful | Yes | Yes | Yes |
- NSGs are your first line of defense at the network layer.
- Application Gateways are used when you need to inspect the content of the traffic (e.g., looking for SQL injection in a URL).
- Cloud Firewalls are used for complex, enterprise-grade traffic routing and centralized policy management across multiple virtual networks.
Common Pitfalls and Troubleshooting
Even experienced engineers run into issues with NSGs. If your application is failing to connect, the first instinct is often to blame the code or the database. However, the network configuration is frequently the culprit.
Pitfall 1: The "Hidden" Default Deny
If you have a complex setup with multiple NSGs (one on the subnet and one on the NIC), remember that the traffic must pass both layers. If either NSG denies the traffic, the request will fail. Always check both levels of association.
Pitfall 2: Forgetting Outbound Rules
While NSGs are stateful, there are scenarios where you might explicitly deny outbound traffic for security reasons. If you have an outbound rule that blocks all traffic to the internet, your server may not be able to reach external APIs or package repositories for updates. Ensure your outbound rules align with your server's operational requirements.
Pitfall 3: Over-reliance on "Any"
Using "Any" as a source for SSH/RDP is a recipe for disaster. Bots scan the internet 24/7 for open management ports. Within minutes of opening port 22 to the public, your logs will show thousands of failed login attempts. Always restrict management ports to a specific VPN or static IP address.
Troubleshooting Workflow:
- Check the Flow Logs: Are the packets being denied? If yes, which rule is responsible?
- Review Priority: Is a higher-priority "Deny" rule overriding your "Allow" rule?
- Verify Association: Is the NSG actually attached to the correct subnet or interface?
- Test Connectivity: Use tools like
telnetornc(netcat) to test specific port connectivity (e.g.,nc -zv <IP> 443). - Check Routing: Sometimes the traffic is allowed by the NSG, but the routing table is sending the traffic to the wrong destination.
Implementing NSG Rules via Infrastructure as Code (IaC)
In modern environments, you should never configure NSGs manually through the portal. Manual configuration is error-prone, hard to track, and impossible to replicate. Instead, use Infrastructure as Code (IaC) tools like Terraform or Bicep.
Here is an example of a simple NSG rule defined in Terraform. This approach ensures that your security configuration is version-controlled and reproducible.
# Example Terraform snippet for an NSG Rule
resource "azurerm_network_security_rule" "web_access" {
name = "AllowHTTPS"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "443"
source_address_prefix = "*"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.example.name
network_security_group_name = azurerm_network_security_group.example.name
}
By defining your rules in code, you can run automated tests against your infrastructure to ensure no "Allow All" rules are accidentally committed. This is the gold standard for enterprise security.
Key Takeaways for Network Security
As we conclude this lesson, remember that security is a continuous process, not a "set it and forget it" task. The following points summarize the core principles of effective NSG management:
- Hierarchy Matters: Rules are processed based on priority. Always keep your most specific rules at the top (lowest number) and broad rules at the bottom.
- Use Service Tags: Replace hardcoded IP ranges with service tags to ensure your rules remain accurate as cloud provider infrastructure evolves.
- Subnet Association: Prefer applying NSGs at the subnet level to provide a consistent security boundary for all resources within that segment.
- Principle of Least Privilege: Never open a port unless it is required for an application to function, and never use "Any" as a source or destination if a more specific option exists.
- Audit and Prune: Establish a routine for auditing your NSG rules. Delete old, unused, or "temporary" rules that have outlived their purpose.
- Use Infrastructure as Code: Manage your NSGs through version-controlled files. This provides an audit trail and prevents accidental misconfigurations caused by manual changes.
- Monitor with Flow Logs: Enable network flow logging to gain visibility into the traffic patterns hitting your security groups; this is essential for both troubleshooting and incident response.
By applying these principles, you move beyond basic connectivity and begin to build a robust, defensible network architecture. Securing your network is one of the most impactful things you can do to protect your organization's digital assets. As you move forward, continue to practice these techniques in your own lab environments, testing how different rule combinations affect traffic flow. The more you experiment with these configurations, the more natural the logic of network security will become.
Reach the last section to complete this lesson and earn points — you're on section 1 of 10.
- Introduction to Azure Networking
- Introduction to Azure Networking Quiz5q
- Virtual Network Address Spaces
- Virtual Network Address Spaces Quiz5q
- Subnet Design and Configuration
- Subnet Design and Configuration Quiz5q
- Public and Private IP Addressing
- Public and Private IP Addressing Quiz5q
- Network Interface Configuration
- Network Interface Configuration Quiz5q
- Azure DNS Configuration
- Azure DNS Configuration Quiz5q
- Virtual Network Peering
- Virtual Network Peering Quiz5q
- Global VNet Peering
- Global VNet Peering Quiz5q
- Azure Virtual WAN
- Azure Virtual WAN Quiz5q
- Virtual WAN Hub Configuration
- Virtual WAN Hub Configuration Quiz5q
- Service Chaining and UDR
- Service Chaining and UDR Quiz5q
- Network Virtual Appliances
- Network Virtual Appliances Quiz5q
- Azure VPN Gateway Overview
- Azure VPN Gateway Overview Quiz5q
- Site-to-Site VPN Configuration
- Site-to-Site VPN Configuration Quiz5q
- Point-to-Site VPN Configuration
- Point-to-Site VPN Configuration Quiz5q
- VPN Gateway SKUs and Sizing
- VPN Gateway SKUs and Sizing Quiz5q
- VPN Gateway High Availability
- VPN Gateway High Availability Quiz5q
- VPN Gateway Troubleshooting
- VPN Gateway Troubleshooting Quiz5q
- ExpressRoute Overview
- ExpressRoute Overview Quiz5q
- ExpressRoute Circuit Configuration
- ExpressRoute Circuit Configuration Quiz5q
- ExpressRoute Peering Types
- ExpressRoute Peering Types Quiz5q
- ExpressRoute Global Reach
- ExpressRoute Global Reach Quiz5q
- ExpressRoute FastPath
- ExpressRoute FastPath Quiz5q
- ExpressRoute High Availability
- ExpressRoute High Availability Quiz5q
- Azure Load Balancer Overview
- Azure Load Balancer Overview Quiz5q
- Internal Load Balancer Configuration
- Internal Load Balancer Configuration Quiz5q
- Public Load Balancer Configuration
- Public Load Balancer Configuration Quiz5q
- Load Balancer Health Probes
- Load Balancer Health Probes Quiz5q
- Cross-Region Load Balancer
- Cross-Region Load Balancer Quiz5q
- Application Gateway Overview
- Application Gateway Overview Quiz5q
- Application Gateway Components
- Application Gateway Components Quiz5q
- URL Path-Based Routing
- URL Path-Based Routing Quiz5q
- Multi-Site Hosting
- Multi-Site Hosting Quiz5q
- SSL Termination and End-to-End SSL
- SSL Termination and End-to-End SSL Quiz5q
- Web Application Firewall Integration
- Web Application Firewall Integration Quiz5q
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles the points you earn on every lesson and quiz so you progress twice as fast, unlocks half of every practice exam — plus full case studies — with the Learn & Exam study modes, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× points per lesson & quiz
- ✓ 50% of every exam unlocked
- ✓ Learn & Exam modes
- ✓ Distraction-free lessons