Network Security Groups in Azure: The Traffic Rulebook for Your Cloud Network
A beginner-friendly guide to Azure Network Security Groups, the simple but powerful rule lists that decide which network traffic may enter or leave your servers, explained step by step with everyday analogies.
Imagine a large office building with several floors. At the main gate stands a guard with a clipboard. The clipboard has a list of instructions: “let delivery vans into the loading bay”, “let staff into floors two and three”, “never let strangers into the server room”. Every visitor is checked against the list from top to bottom, and the first instruction that fits the visitor decides what happens. A Network Security Group is that clipboard for your cloud network.
In this guide we begin with the simplest question, “what is network traffic?”, and slowly build up to rule design, default rules, service tags, troubleshooting and managing hundreds of rules across a company. No earlier networking knowledge is needed. Every new word is explained when it first appears.
1What Is a Network Security Group?
A Network Security Group, usually shortened to NSG, is a list of allow and deny rules for network traffic.
Computers on a network talk by sending small packages of data called packets. Network traffic is simply the flow of these packets from one place to another. When you open a website, packets travel from your computer to a server and back. When an application reads from a database, packets travel between two servers.
In Azure, resources such as virtual machines live inside a virtual network, which is your private network in the cloud. By default, many of these resources can talk to each other freely. An NSG lets you say exactly which conversations are allowed and which are blocked. It filters traffic coming into a resource, called inbound traffic, and traffic going out of it, called outbound traffic.
An NSG is not a physical box and it is not a server. It is a set of rules stored in Azure and applied automatically by the platform. It costs nothing to create. You can attach the same NSG to many subnets or network cards, and you can change a rule at any time, with the change taking effect within seconds.
Think of a housing society with a security guard at each building entrance. The guard has a rulebook that says who may enter and who may leave. Residents of the society can visit each other, couriers may go to the front desk, and strangers are turned away. The NSG is the rulebook, and Azure’s network is the guard that follows it faithfully.
Words you will meet again and again
Security Rule
One instruction in the list. It says allow or deny, for which traffic, and in which direction.
Priority
A number that sets the order in which rules are checked. Lower numbers are checked first.
Port
A numbered door on a computer. Different services listen on different doors.
Service Tag
A ready-made name for a group of addresses that belong to an Azure service.
Application Security Group
Your own named group of servers, used in rules instead of raw addresses.
Association
The act of attaching an NSG to a subnet or to a network card so that its rules start to apply.
2Why Do We Need NSGs?
A network with no rules is like a building with no doors.
Imagine a company that places a web server, an application server and a database inside the same virtual network. If nothing limits the traffic, a problem in one machine can spread to all the others. An attacker who breaks into the web server could try to reach the database directly. A mistake by a developer could expose a management port to the entire internet.
The security idea behind NSGs is called least privilege. It means every resource should be allowed to talk only to the exact peers it needs, using only the exact ports that are needed, and nothing more. NSGs are the everyday tool for putting this idea into practice at the network level.
Blocking unwanted visitors
Stop the open internet from reaching servers that should be private, such as databases and internal tools.
Separating tiers
Make sure that web servers can talk to application servers, and application servers can talk to databases, but never the other way around or skipping a level.
Limiting damage
If one machine is compromised, tight rules stop the attacker from freely moving to other machines. This is called limiting the blast radius.
Meeting rules and audits
Many industry standards demand proof that access is restricted. A clear rule list is easy to show to an auditor.
Controlling outgoing traffic
Prevent servers from calling places they should never talk to, which also makes data theft harder.
NSGs are only one layer of defence. They filter by address, port and protocol, but they cannot read the content of a web request. Real-world designs combine NSGs with firewalls, identity controls and application security.
3Anatomy of a Security Rule
Every rule answers the same set of questions, so once you learn them you can read any NSG.
A security rule describes one kind of traffic and says what to do with it. To describe the traffic, it uses five pieces of information, often called the five-tuple. Together with a direction, a priority and an action, these make up a complete rule.
| Part of the rule | What it means | Example in plain words |
|---|---|---|
| Direction | Is the traffic coming in or going out? | Inbound |
| Source | Where does the traffic come from? | The internet, a range of addresses, a service tag or a group of servers |
| Source port range | Which door does the sender use? | Usually any, shown as a star |
| Destination | Where is the traffic going? | One address, a subnet or a group of servers |
| Destination port range | Which door is the traffic knocking on? | The secure web door |
| Protocol | What kind of conversation is it? | TCP, UDP, ICMP or any |
| Action | What should happen? | Allow or deny |
| Priority | When is this rule checked? | A number from 100 to 4096 |
A gate rule might read: “Visitors coming in, from any city, going to the loading bay, using a delivery van, are allowed, rule number 200”. Each piece of the sentence is one column in the table above.
Understanding ports and protocols
A port is like a numbered door on a computer. Each service waits at a well-known door. The secure web service waits at one door, plain web at another, remote login for Linux at another, and remote desktop for Windows at yet another. Databases wait at their own doors. When you write a rule, you choose which doors it covers.
The protocol describes the style of conversation. TCP is careful and reliable, like a phone call where each side confirms what was heard, and is used by websites and databases. UDP is quick and light, like shouting a message across a room, and is used for some streaming and name lookups. ICMP is used for simple checks, such as the ping test.
Priority and the first match
Rules are checked in order of priority, from the lowest number to the highest. As soon as one rule matches the traffic, Azure applies its action and stops looking at the other rules. This means the order matters a lot. A broad rule with a low number can hide a narrower rule with a higher number.
Creating a broad “deny” rule with a low number, then adding a more specific “allow” rule with a higher number, and wondering why the allow never works. The deny matches first and the allow is never reached. Put specific rules at lower numbers than broad ones.
Leave gaps between priority numbers, such as 100, 110, 120. Later you can slip a new rule between two old ones without renumbering everything.
4The Default Rules You Always Get
A brand-new NSG is not empty, because Azure places safe starting rules in it.
Every NSG carries a set of default rules. They have very high priority numbers, which means they are checked last. You cannot delete or edit them, but you can override them by adding your own rules with lower numbers. They give a sensible baseline: resources in the same network may talk to each other, the internet may not start conversations with your servers, and your servers may reach out to the internet.
| Direction | Priority | Name | What it does |
|---|---|---|---|
| Inbound | 65000 | AllowVnetInBound | Allows traffic coming from inside the virtual network and connected networks |
| Inbound | 65001 | AllowAzureLoadBalancerInBound | Allows Azure’s load balancer to check the health of your servers |
| Inbound | 65500 | DenyAllInBound | Blocks all other incoming traffic |
| Outbound | 65000 | AllowVnetOutBound | Allows traffic going to the virtual network and connected networks |
| Outbound | 65001 | AllowInternetOutBound | Allows traffic going out to the internet |
| Outbound | 65500 | DenyAllOutBound | Blocks all other outgoing traffic |
Imagine a new building that comes with a default gate policy: residents may visit each other, outsiders may not enter, and residents may go out. You can add stricter or looser instructions on top, but the default policy stays printed at the bottom of the list.
What the defaults really mean
The first inbound default says that anything inside your virtual network may talk to anything else inside it. This is friendly but also dangerous, because a compromised machine can reach all its neighbours. That is why serious designs add their own deny rules between tiers, with lower numbers than 65000.
The last two defaults deny everything else. For inbound traffic this means that unless you add an allow rule, the internet cannot reach your servers. For outbound traffic, the internet is allowed by the earlier default, so servers can download updates unless you block them.
Because the “allow virtual network” default is so broad, an NSG that has only the default rules does very little to separate your internal tiers. You must add explicit rules to get real segmentation.
5Stateful Rules: Replies Take Care of Themselves
NSGs remember conversations, which makes rules much simpler to write.
NSGs are stateful. This means that when a conversation is allowed in one direction, the replies belonging to that same conversation are allowed back automatically. Azure keeps a short-lived record of each allowed conversation, called a flow. You do not need to write a second rule for the answer.
For example, if you allow visitors to reach your web server on the secure web port, the web server’s replies to those visitors leave freely, even if an outbound rule would otherwise block them. The reverse is also true. If your server starts a conversation to a database and the outbound rule allows it, the database’s replies come back in without an extra inbound rule.
sequenceDiagram
participant V as Visitor
participant N as NSG
participant W as Web Server
V->>N: Request to web door
N->>N: Inbound rule allows, flow recorded
N->>W: Request delivered
W-->>N: Reply
N-->>V: Reply passes automatically
When you phone a friend, the phone company does not need a second permission to let your friend’s voice reach you. Once the call is connected, speech flows both ways. A stateful NSG treats a conversation the same way.
If you change a rule while a conversation is running, existing flows are not always cut immediately. New connections follow the new rule, while old ones may continue until they end. Keep this in mind when you block something urgently.
6Where to Attach an NSG: Subnet or Network Card
An NSG does nothing until you attach it to something.
You can associate an NSG with a subnet, with a network interface card, or with both. A subnet is a slice of your virtual network, and a network interface card, called a NIC, is the virtual plug that connects one machine to the network. A rule set on a subnet applies to every resource in that subnet. A rule set on a NIC applies only to that one machine.
Attach to a Subnet
One rule list protects every machine in the subnet. Easy to read and audit. This is the usual best choice.
Attach to a NIC
Gives special rules to a single machine. Useful for exceptions, but many NSGs become hard to track.
Attach to Both
Traffic must pass both lists. Powerful but easy to confuse, so use it only when you understand the order.
The order of checking
When both a subnet NSG and a NIC NSG exist, traffic must be allowed by both. For incoming traffic, the subnet NSG is checked first and then the NIC NSG. For outgoing traffic, the NIC NSG is checked first and then the subnet NSG. A deny at any step stops the traffic.
flowchart LR
I["Inbound Traffic"] --> S1["Subnet NSG"]
S1 --> N1["NIC NSG"]
N1 --> VM["Virtual Machine"]
VM --> N2["NIC NSG"]
N2 --> S2["Subnet NSG"]
S2 --> O["Outbound Traffic"]
A visitor to an office first passes the building gate guard, who represents the subnet NSG, and then the guard at the door of the specific office, who represents the NIC NSG. Both must say yes. On the way out, the order is reversed.
Opening a port in the subnet NSG and forgetting that a second NSG on the NIC still blocks it. The result is a puzzling “everything looks open but nothing connects” situation. When troubleshooting, always check both places.
7Service Tags: Names for Groups of Addresses
Typing long lists of addresses is boring and error-prone, so Azure provides ready-made labels.
Azure services use many internet addresses, and these addresses change over time. A service tag is a name that stands for a whole group of addresses belonging to one Azure service or category. Microsoft keeps each tag up to date automatically. You simply use the name in a rule, and the group of addresses behind it stays correct without any effort from you.
Internet
Every address outside your virtual network and outside Azure’s own ranges. Used to allow or block the public internet.
VirtualNetwork
Your own virtual network, networks peered with it and connected office networks.
AzureLoadBalancer
The address Azure uses for health checks from its load balancer.
Storage and Sql
Addresses of the storage and database services. Useful for allowing outgoing traffic to just those services.
AzureCloud
All public addresses used by Azure datacentres. Many tags also exist for a single region.
AzureActiveDirectory
Addresses used for sign-in with Microsoft Entra ID.
Instead of listing the home address of every employee of a delivery company on your gate list, you write “the delivery company”. The security office keeps the actual staff list updated for you.
A good pattern is to deny all outbound internet traffic and then allow only the service tags your application truly needs, such as storage and a database. The rule list stays short and always correct.
A broad tag such as one covering a whole cloud service includes addresses used by other customers of that service. Allowing it opens the door to more than just your own resources, so combine tags with other controls where the data is sensitive.
8Application Security Groups: Naming Your Own Servers
Rules that mention addresses become hard to read, so name your servers by their role instead.
An Application Security Group, called an ASG, is a label that you create and attach to the network cards of machines that share a role. You might create one called “web-servers”, one called “app-servers” and one called “db-servers”. Then you write rules using those names instead of numbers.
Create the groups
Make an ASG for each role in your application, such as web, application and database.
Assign machines
Attach each machine’s network card to the group that matches its role.
Write rules using names
For example, allow traffic from the web group to the application group on the application’s port.
Grow without editing rules
When you add a new web server, attach it to the web group. All existing rules apply to it immediately.
A gate list that says “Rahul in flat 12, Meena in flat 14, Arun in flat 15” must be rewritten whenever someone moves. A list that says “all members of the cleaning team may enter the store room” keeps working when new cleaners join.
Benefit: readability
Anyone reading the rules can understand them at a glance without decoding numbers.
Benefit: automatic scaling
New machines inherit the right access the moment they join the group, even when addresses change.
Benefit: fewer mistakes
No more forgotten addresses or typing errors in long lists.
The machines in the source and destination groups of one rule must live in the same virtual network. ASGs help organise a single network, and they do not link separate networks.
9Inside the Machine: How Evaluation Really Works
Understanding what happens to each packet makes troubleshooting far easier.
Azure applies NSG rules on the host machine that runs your virtual machine, inside a piece of software called the virtual switch. Because the check happens on the host, before a packet reaches your machine, nothing inside your server needs to change. The operating system of the server never even sees the traffic that was denied.
Packet arrives
The packet reaches the host that runs the destination virtual machine.
Existing flow check
The virtual switch first asks whether this packet belongs to a conversation that was already allowed. If so, it passes straight through.
Rule search
For a new conversation, rules are examined from the lowest priority number upwards, using the subnet NSG first and then the NIC NSG for inbound traffic.
First match wins
The first rule that matches decides. If it allows the traffic, a flow record is created. If it denies, the packet is dropped quietly.
Delivery or drop
Allowed packets are handed to the server. Denied packets simply vanish, so the sender usually sees a timeout rather than a friendly refusal.
flowchart TD
P["Packet arrives at host"] --> F{"Part of an allowed flow?"}
F -->|"Yes"| D["Deliver to server"]
F -->|"No"| R["Check rules by priority"]
R --> M{"First matching rule"}
M -->|"Allow"| C["Record flow and deliver"]
M -->|"Deny"| X["Drop packet silently"]
The guard first looks at his notebook of people already let in today. If your name is there, you walk in. If not, he reads the instruction list from the top and follows the first line that fits you.
A denied packet is dropped without any reply. A tool trying to connect keeps waiting until it gives up. If you see a connection that hangs for a long time and then fails, a network rule is a prime suspect. A quick “connection refused” message usually means the network was fine but nothing was listening on that port.
10Designing Rules for a Three-Tier Application
Let us walk through a classic example: a web tier, an application tier and a database tier.
Suppose your system has three groups of servers. The web servers receive visitors, the application servers run business logic, and the database servers store data. Each group lives in its own subnet with its own NSG. The goal is that traffic flows only in one direction down the chain.
flowchart TD
I["Internet"] -->|"Secure web door only"| W["Web Subnet"]
W -->|"Application door only"| A["Application Subnet"]
A -->|"Database door only"| D["Data Subnet"]
M["Management Subnet"] -.->|"Admin access"| W
M -.->|"Admin access"| A
M -.->|"Admin access"| D
| Subnet NSG | Inbound rule: allow | Inbound rule: deny | Outbound rule |
|---|---|---|---|
| Web | Internet to web servers on the secure web door; management subnet for administration | Everything else from the virtual network | Allow to the application tier on its door only |
| Application | Web tier to application servers on the application door; management subnet | Everything else, including direct internet and other subnets | Allow to the database tier on its door only |
| Data | Application tier to database servers on the database door; management subnet | Everything else, including the web tier and the internet | Deny internet; allow only what backups and updates need |
A bank has a public hall, a staff area and a vault. Customers may enter the hall. Hall staff may call the back office. Only back office staff may open the vault. A customer cannot walk straight to the vault, and the vault door does not open to the street.
The shape of a good rule list
Allow what is needed
Write precise allow rules with low priority numbers for each legitimate flow.
Add an explicit deny for the rest
Place a deny rule for all other virtual network traffic before the default allow at 65000, so internal tiers stay separated.
Control the outbound side
Limit outgoing traffic to the services each tier really uses, and deny the internet where it is not needed.
Document each rule
Give every rule a clear name and a description that explains the business reason.
Writing a deny-everything outbound rule without allowing Azure’s essential services, then wondering why the server cannot activate, update or talk to monitoring. Test outbound restrictions on a non-critical subnet first and use service tags to allow what is essential.
11NSG, Azure Firewall and Other Tools Compared
Azure has several security tools, and each one fits a different job.
| Tool | What it filters | Where it works | Best for |
|---|---|---|---|
| Network Security Group | Address, port and protocol | Subnets and network cards | Simple, free, per-subnet segmentation |
| Azure Firewall | Address, port, domain names, threat intelligence, application rules | Central point in the network | Central control and inspection, including by website name |
| Web Application Firewall | Content of web requests | Application Gateway or Front Door | Blocking web attacks such as injection and scripting |
| Private Endpoints and Firewalls on services | Who may reach a specific Azure service | On the service itself | Protecting storage, databases and similar services |
NSGs are the door locks on each room. Azure Firewall is the central security desk of the building that checks IDs and logs everyone. A web application firewall is the bag scanner that looks inside what people carry.
Use NSGs for
Basic and fast segmentation between subnets, allowing management access from known places, and limiting what each tier may talk to.
Add Azure Firewall for
Filtering outgoing traffic by website name, central logging for the whole company, and inspection that NSGs cannot do.
Add a WAF for
Public web applications that need protection from common web attacks.
These tools are partners, not rivals. A mature design uses NSGs on every subnet for fine-grained separation, a central firewall for company-wide rules, and a WAF for public web traffic.
12NSGs with PaaS Services and Special Subnets
Not every Azure service lives inside your subnet, and some subnets have special needs.
An NSG filters traffic for resources that have a network card in your virtual network, such as virtual machines. Many platform services, like storage accounts and databases, do not sit in your subnet. They are reached through their own addresses. NSG rules can control traffic going out to them with service tags, but the service itself is protected through its own firewall, private endpoints and identity settings.
Private Endpoints
A private endpoint places a network card for a service in your subnet. NSG rules can apply to it when the subnet setting for network policies is enabled.
Application Gateway Subnet
Requires certain management ports and health check traffic to stay open. Blocking them breaks the gateway.
Azure Bastion Subnet
Needs specific inbound and outbound rules for the service to work. Microsoft documents the exact list.
Delegated Subnets
Some managed services take over a subnet and may add or require their own rules. Read the service’s network guidance before attaching an NSG.
Traffic the platform treats specially
A few pieces of basic infrastructure traffic, such as the address-giving service of Azure and the health checks from the platform, use special platform addresses. Some of this traffic is handled by the platform and is not controlled the same way as ordinary traffic. Rely on the official documentation for the exact list instead of guessing.
Attaching a tight NSG to a subnet used by a managed service without reading its requirements. The service may fail to deploy or become unhealthy. Always check the service’s networking documentation for mandatory rules.
13Safe Administration: Closing the Dangerous Doors
The most attacked doors on any server are the ones used for remote administration.
To manage a server, administrators connect remotely. Windows servers use a remote desktop door and Linux servers use a secure shell door. Because these doors give full control of a machine, attackers constantly scan the internet for them and try common passwords. A server with an open management door on a public address is usually attacked within minutes.
Option: Azure Bastion
A managed service that lets administrators open a remote session in the browser, through the Azure portal. The servers need no public address and no management door open to the internet.
Option: Just-in-Time access
A feature of Microsoft Defender for Cloud that keeps management doors closed by default. An administrator asks for access, and a temporary NSG rule is created for a limited time and only for their address, and then it disappears.
Option: VPN or private link
Administrators first connect to the company network through a VPN, and management doors are opened only to private addresses.
Option: Restricted source addresses
If direct access is unavoidable, allow management doors only from a small list of trusted addresses, never from the whole internet.
Instead of leaving the key to the server room under the doormat, you keep it in a locked cabinet. A manager issues a key for one hour when a technician has a valid job, and takes it back afterwards. Just-in-Time access works exactly like that.
A rule that allows any source on a management door, or the “Internet” tag, is the single most common cause of cloud server break-ins. Search your NSGs for such rules regularly and remove them.
14Monitoring and Logging
A rule list is only trustworthy if you can see what it is really doing.
Azure offers several ways of watching NSG behaviour. Together they let you answer three questions: “who changed the rules?”, “what traffic was allowed or blocked?” and “does this specific traffic pass?”
Activity Log
Records every change to NSGs and rules, including who made it and when. Your first stop after a surprise.
Flow Logs
Record information about the traffic passing through, such as source, destination, port and whether it was allowed. Microsoft is moving customers from NSG flow logs to virtual network flow logs, so check which one your environment should use today.
Traffic Analytics
Turns flow log data into charts and maps that show busy flows, unusual destinations and blocked traffic.
Network Watcher
A toolbox that includes tests to verify rules and to list the effective rules on a machine.
IP Flow Verify
You describe a packet, such as a source, destination, port and direction, and Azure tells you whether it would be allowed or denied and names the exact rule responsible.
Effective Security Rules
Shows the combined result of all NSGs that apply to a network card, including the default rules. This removes the guesswork when both subnet and NIC NSGs exist.
Alerts on changes
Create alerts that notify you when a rule is added that opens a dangerous door to the internet, so you can react quickly.
Enable flow logging before you need it. When a security incident happens, history that was never recorded cannot be recovered, and it is the first thing investigators ask for.
15Troubleshooting: When Something Cannot Connect
Follow a calm checklist instead of changing rules at random.
Confirm the basics
Is the target machine running, does it have the expected address, and is the application actually listening on the expected port?
Check the effective rules
View the effective security rules of the target’s network card. They show the real combined picture of subnet and NIC rules.
Test with IP Flow Verify
Enter the exact source, destination, port and direction, and read which rule allows or denies it.
Check both ends
Remember that outbound rules at the source and inbound rules at the destination both apply. Check each side.
Look beyond NSGs
If the NSGs are fine, check routes, a firewall in the path, the operating system’s own firewall on the server, and name resolution.
| Symptom | Likely cause | What to check |
|---|---|---|
| Connection hangs, then times out | Traffic dropped by a rule or route | Effective rules, IP Flow Verify, routes |
| Connection refused quickly | Network is open but nothing listens | Application status and port on the server |
| Works from one machine, not another | Rule limits the source address | Source ranges and groups in the rules |
| Worked yesterday, fails today | A rule or association changed | Activity log for recent changes |
| Managed service unhealthy after adding NSG | A mandatory rule was blocked | Service documentation for required rules |
Fixing a connection problem by adding an “allow any” rule at the top and forgetting to remove it. Temporary fixes become permanent holes. Use the diagnostic tools to find the real cause and add only the precise rule needed.
16Managing NSGs at Scale
A company with hundreds of subnets cannot manage rules by clicking in a portal.
As an environment grows, the number of NSGs and rules grows with it. Azure offers generous limits on how many NSGs and rules you can have, but human attention is the real limit. Without discipline, rule lists fill up with forgotten exceptions and nobody dares to remove anything.
Infrastructure as Code
Define NSGs in templates or scripts stored in version control, so every change is reviewed, repeatable and traceable.
Azure Policy
Use policy to forbid dangerous settings, such as management doors open to the internet, or to require an NSG on every subnet.
Naming Standards
Use clear names that say the direction, action and purpose, so rules read like sentences.
Reusable Templates
Build standard NSG designs for common tiers and reuse them, so every web subnet in the company looks the same.
Regular Reviews
Schedule periodic reviews to remove rules that no longer have a reason.
Central Visibility
Collect flow logs and activity logs in one place so security teams can search across the whole company.
A large university does not let each department invent its own gate rules and write them on scraps of paper. It keeps a central rulebook, with a standard format, a review process and a person who is responsible for each page.
Add a short description to every rule that says why it exists and who asked for it. Years later, this single habit is what makes it safe to clean up old rules.
17Design Patterns That Work Well
These approaches appear again and again in well-run Azure environments.
Pattern: One NSG per Subnet Role
Each type of subnet, such as web, application, data and management, has its own NSG design, reused for every environment. Rules stay simple and predictable.
Pattern: Deny by Default, Allow by Exception
Begin with a deny for everything not needed, then open only specific, documented flows. Every open door must have a reason.
Pattern: ASG per Role
Application security groups name the roles, and rules mention roles instead of addresses, so scaling out needs no rule changes.
Pattern: Egress Allow-List
Outbound traffic is denied to the internet by default, and only specific service tags or addresses are allowed, which reduces the chance of data theft.
Pattern: Central Firewall plus Local NSGs
A hub firewall handles inspection and website-name filtering for the whole company, while NSGs in each spoke provide local separation between tiers.
flowchart TD
INT["Internet"] --> FW["Central Firewall in Hub"]
FW --> SP1["Spoke 1 with NSGs per subnet"]
FW --> SP2["Spoke 2 with NSGs per subnet"]
SP1 --> T1["Web Tier"]
SP1 --> T2["Application Tier"]
SP1 --> T3["Data Tier"]
18Real-World Examples
NSGs quietly guard almost every serious Azure deployment.
Online retail
Web servers accept visitors on the secure web door only, application servers accept connections from web servers only, and the database accepts connections from application servers only. A breach of a web server cannot reach the database directly.
Banks and insurers
Strict NSG rule sets and flow logs provide evidence to auditors that production systems are separated from development, and that administration happens only through approved paths.
Hospitals
Patient record systems sit in subnets where inbound access is allowed only from clinical application servers, while outbound internet is denied, protecting sensitive data from leaks.
Software vendors
Each customer environment lives in its own network with its own NSGs, so that a problem in one customer’s setup cannot affect any other customer.
Data and analytics teams
Compute clusters may talk to storage and databases only through approved service tags and private endpoints, with the rest of the internet blocked.
Development and test labs
Test networks are isolated from production by deny rules, so a careless experiment cannot touch live systems.
Advantages
- Free to create and fast to apply
- Simple, readable allow and deny rules
- Stateful, so replies need no extra rules
- Service tags and ASGs make rules short and self-updating
- Works on subnets and network cards with no agent on the server
Disadvantages / Trade-offs
- Filters only by address, port and protocol, not content or website name
- Large rule lists become hard to read without discipline
- Two NSGs on one machine can confuse troubleshooting
- Denied traffic simply times out, so problems feel mysterious
- Needs careful design for managed services with mandatory rules
19Anti-Patterns to Avoid
These shortcuts regularly lead to break-ins, outages and confusing audits.
Problem
A rule allows any source to reach the remote administration door of servers, often created “just for a quick test” and then forgotten.
Why It’s Harmful
Automated attackers find open management doors within minutes and try endless password guesses. One weak password can lead to full control of the machine.
Correct Approach
Use Bastion, Just-in-Time access or a VPN. If a rule is unavoidable, allow only specific trusted addresses and set a clear review date.
Problem
Teams rely on the default “allow virtual network” rule and never add rules between tiers.
Why It’s Harmful
Every machine can talk to every other machine. A single compromised server can scan and attack all its neighbours, including databases.
Correct Approach
Add explicit allow rules for required flows and a deny rule for the rest of the virtual network traffic, with a lower priority number than the default.
Problem
Rules are written with long lists of raw addresses that nobody remembers or updates.
Why It’s Harmful
Addresses change, machines move, and rules silently become wrong. A forgotten address may keep access it should have lost.
Correct Approach
Use application security groups for your servers and service tags for Azure services, so rules describe roles instead of numbers.
Problem
Every virtual machine has its own NSG on its network card, in addition to a subnet NSG, and nobody knows the combined effect.
Why It’s Harmful
Troubleshooting becomes a puzzle, since traffic must pass both lists. Audits struggle to prove what is really allowed.
Correct Approach
Prefer a single NSG on each subnet. Use NIC-level NSGs only for rare, documented exceptions, and check effective rules when you do.
Problem
Priority numbers are packed tightly, rules have no descriptions, and old rules are never removed.
Why It’s Harmful
Nobody can safely add or remove anything. The list grows until it is a maze, and risky rules hide inside it for years.
Correct Approach
Leave gaps in priority numbers, describe every rule, manage NSGs in version-controlled templates and review them on a schedule.
20Best Practices and a Starter Checklist
Walk through these steps whenever you design NSGs for a new network.
Draw the traffic first
Sketch which tiers must talk to which, and on what doors. Rules should follow the picture.
Attach NSGs to subnets
Use one NSG per subnet role, and treat NIC-level NSGs as exceptions.
Use groups and tags
Write rules with application security groups and service tags instead of raw addresses.
Deny what is not needed
Add explicit deny rules between tiers and restrict outbound internet access with an allow-list.
Protect administration
Use Bastion or Just-in-Time access, and never expose management doors to the whole internet.
Name, number and describe
Leave gaps in priority numbers, use clear names and write a reason on each rule.
Log and alert
Enable flow logs, analyse them, and alert on changes that open risky doors.
Automate and review
Define NSGs in code, enforce guardrails with Azure Policy, and review the rules on a regular schedule.
Believing the default rules provide tier separation, writing allow rules with higher numbers than broad denies, forgetting the second NSG on a NIC, opening management doors to the internet, blocking mandatory rules for managed services, and fixing problems with temporary “allow any” rules that are never removed.
21Frequently Asked Questions
An NSG is a basic, free packet filter that works with addresses, ports and protocols. A full firewall such as Azure Firewall adds central management, filtering by website name, threat intelligence and richer logging. Many designs use both.
Creating and using NSGs is free. Costs can appear from related features such as storing flow logs and analysing traffic.
The rule with the lower priority number is checked first, and its action applies. Later rules are ignored for that traffic, so ordering is important.
No. Default rules cannot be removed or changed, but you can override them by adding your own rules with lower priority numbers.
No. NSGs are stateful, so once a conversation is allowed, its replies are allowed automatically in the opposite direction.
In most cases attach it to the subnet, which keeps the design simple and easy to audit. Use network card NSGs only for special exceptions, and remember that when both exist, traffic must be allowed by both.
Denied packets are dropped silently, so the sender waits until it times out. A hanging connection often points to a network rule, a route or a firewall, while an instant refusal usually means nothing is listening on the port.
No. NSGs work with network addresses and ports. To filter by website name, use Azure Firewall or another service that understands domain names.
Use a service tag for storage in your outbound rule, or better, connect through a private endpoint and restrict the storage account’s own firewall. This keeps traffic off the open internet.
Use the IP Flow Verify tool in Network Watcher. Enter the source, destination, port, protocol and direction, and it names the rule that allows or denies the traffic. Flow logs can also show blocked connections over time.
22Summary and Key Takeaways
A Network Security Group is the everyday rulebook of an Azure network. Each rule describes a kind of traffic and says allow or deny, rules are checked by priority from the lowest number up, and the first match wins. Default rules provide a safe but loose baseline, so serious designs add their own rules to separate tiers and limit outgoing traffic. With service tags, application security groups, careful placement on subnets, safe administration paths, good logging and management through code, NSGs turn a flat cloud network into a set of well-guarded rooms.
Key Takeaways
- NSGs are rule lists for traffic — each rule uses source, destination, port, protocol, direction, priority and an allow or deny action.
- Order matters — rules are checked from the lowest priority number, the first match wins, and gaps between numbers keep life easy.
- Defaults are only a baseline — the default rules allow all traffic inside the virtual network, so add explicit rules to separate your tiers.
- NSGs are stateful — replies to allowed conversations pass automatically and need no second rule.
- Attach to subnets by preference — when both a subnet and a network card have an NSG, traffic must pass both, which complicates troubleshooting.
- Use names, not numbers — service tags and application security groups keep rules short, readable and automatically up to date.
- Protect administration and keep watching — avoid open management doors, use Bastion or Just-in-Time access, enable flow logs, and manage rules through code and policy.