Tutorial: Filter network traffic with a network security group using the Azure Portal
You can filter network traffic inbound to and outbound from a virtual network subnet with a network security group. Network security groups contain security rules that filter network traffic by IP address, port, and protocol. Security rules are applied to resources deployed in a subnet. In this tutorial, you learn how to:
- Create a network security group and security rules
- Create a virtual network and associate a network security group to a subnet
- Deploy virtual machines (VM) into a subnet
- Test traffic filters
If you prefer, you can complete this tutorial using the Azure CLI or PowerShell.
If you don't have an Azure subscription, create a free account before you begin.
Log in to Azure
Log in to the Azure portal at https://portal.azure.com.
Create a virtual network
- Select + Create a resource on the upper, left corner of the Azure portal.
- Select Networking, and then select Virtual network.
Enter, or select, the following information, accept the defaults for the remaining settings, and then select Create:
Setting Value Name myVirtualNetwork Address space 10.0.0.0/16 Subscription Select your subscription. Resource group Select Create new and enter myResourceGroup. Location Select East US. Subnet- Name mySubnet Subnet - Address range 10.0.0.0/24
Create application security groups
An application security group enables you to group together servers with similar functions, such as web servers.
- Select + Create a resource on the upper, left corner of the Azure portal.
- In the Search the Marketplace box, enter Application security group. When Application security group appears in the search results, select it, select Application security group again under Everything, and then select Create.
Enter, or select, the following information, and then select Create:
Setting Value Name myAsgWebServers Subscription Select your subscription. Resource group Select Use existing and then select myResourceGroup. Location East US Complete step 3 again, specifying the following values:
Setting Value Name myAsgMgmtServers Subscription Select your subscription. Resource group Select Use existing and then select myResourceGroup. Location East US
Create a network security group
- Select + Create a resource on the upper, left corner of the Azure portal.
- Select Networking, and then select Network security group.
Enter, or select, the following information, and then select Create:
Setting Value Name myNsg Subscription Select your subscription. Resource group Select Use existing and then select myResourceGroup. Location East US
Associate network security group to subnet
- In the Search resources, services, and docs box at the top of the portal, begin typing myNsg. When myNsg appears in the search results, select it.
Under SETTINGS, select Subnets and then select + Associate, as shown in the following picture:

Under Associate subnet, select Virtual network and then select myVirtualNetwork. Select Subnet, select mySubnet, and then select OK.
Create security rules
Under SETTINGS, select Inbound security rules and then select + Add, as shown in the following picture:

Create a security rule that allows ports 80 and 443 to the myAsgWebServers application security group. Under Add inbound security rule, enter, or select the following values, accept the remaining defaults, and then select Add:
Setting Value Destination Select Application security group, and then select myAsgWebServers for Application security group. Destination port ranges Enter 80,443 Protocol Select TCP Name Allow-Web-All Complete step 2 again, using the following values:
Setting Value Destination Select Application security group, and then select myAsgMgmtServers for Application security group. Destination port ranges Enter 3389 Protocol Select TCP Priority Enter 110 Name Allow-RDP-All In this tutorial, RDP (port 3389) is exposed to the internet for the VM that is assigned to the myAsgMgmtServers application security group. For production environments, instead of exposing port 3389 to the internet, it's recommended that you connect to Azure resources that you want to manage using a VPN or private network connection.
Once you've completed steps 1-3, review the rules you created. Your list should look like the list in the following picture:

Create virtual machines
Create two VMs in the virtual network.
Create the first VM
- Select + Create a resource found on the upper, left corner of the Azure portal.
- Select Compute, and then select Windows Server 2016 Datacenter.
Enter, or select, the following information, accept the defaults for the remaining settings, and then select OK:
Setting Value Name myVmWeb User name Enter a user name of your choosing. Password Enter a password of your choosing. The password must be at least 12 characters long and meet the defined complexity requirements. Subscription Select your subscription. Resource group Select Use existing and select myResourceGroup. Location Select East US Select a size for the VM and then select Select.
Under Settings, select the following values, accept the remaining defaults, and then select OK:
Setting Value Virtual network Select myVirtualNetwork Network Security Group Select Advanced. Network security group (firewall) Select (new) myVmWeb-nsg, and then under Choose network security group, select None. Under Create of the Summary, select Create to start VM deployment.
Create the second VM
Complete steps 1-6 again, but in step 3, name the VM myVmMgmt. The VM takes a few minutes to deploy. Do not continue to the next step until the VM is deployed.
Associate network interfaces to an ASG
When the portal created the VMs, it created a network interface for each VM, and attached the network interface to the VM. Add the network interface for each VM to one of the application security groups you created previously:
- In the Search resources, services, and docs box at the top of the portal, begin typing myVmWeb. When the myVmWeb VM appears in the search results, select it.
Under SETTINGS, select Networking. Select Configure the application security groups, select myAsgWebServers for Application security groups, and then select Save, as shown in the following picture:

Complete steps 1 and 2 again, searching for the myVmMgmt VM and selecting the myAsgMgmtServers ASG.
Test traffic filters
- Connect to the myVmMgmt VM. Enter myVmMgmt in the search box at the top of the portal. When myVmMgmt appears in the search results, select it. Select the Connect button.
- Select Download RDP file.
- Open the downloaded rdp file and select Connect. Enter the user name and password you specified when creating the VM. You may need to select More choices, then Use a different account, to specify the credentials you entered when you created the VM.
- Select OK.
You may receive a certificate warning during the sign-in process. If you receive the warning, select Yes or Continue, to proceed with the connection.
The connection succeeds, because port 3389 is allowed inbound from the internet to the myAsgMgmtServers application security group that the network interface attached to the myVmMgmt VM is in.
Connect to the myVmWeb VM from the myVmMgmt VM by entering the following command in a PowerShell session:
mstsc /v:myVmWebYou are able to connect to the myVmWeb VM from the myVmMgmt VM because VMs in the same virtual network can communicate with each other over any port, by default. You can't however, create a remote desktop connection to the myVmWeb VM from the internet, because the security rule for the myAsgWebServers doesn't allow port 3389 inbound from the internet and inbound traffic from the Internet is denied to all resources, by default.
To install Microsoft IIS on the myVmWeb VM, enter the following command from a PowerShell session on the myVmWeb VM:
Install-WindowsFeature -name Web-Server -IncludeManagementToolsAfter the IIS installation is complete, disconnect from the myVmWeb VM, which leaves you in the myVmMgmt VM remote desktop connection.
- Disconnect from the myVmMgmt VM.
In the Search resources, services, and docs box at the top of the Azure portal, begin typing myVmWeb from your computer. When myVmWeb appears in the search results, select it. Note the Public IP address for your VM. The address shown in the following picture is 137.135.84.74, but your address is different:

To confirm that you can access the myVmWeb web server from the internet, open an internet browser on your computer and browse to
http://<public-ip-address-from-previous-step>. You see the IIS welcome screen, because port 80 is allowed inbound from the internet to the myAsgWebServers application security group that the network interface attached to the myVmWeb VM is in.
Clean up resources
When no longer needed, delete the resource group and all of the resources it contains:
- Enter myResourceGroup in the Search box at the top of the portal. When you see myResourceGroup in the search results, select it.
- Select Delete resource group.
- Enter myResourceGroup for TYPE THE RESOURCE GROUP NAME: and select Delete.
Next steps
In this tutorial, you created a network security group and associated it to a virtual network subnet. To learn more about network security groups, see Network security group overview and Manage a network security group.
Azure routes traffic between subnets by default. You may instead, choose to route traffic between subnets through a VM, serving as a firewall, for example. To learn how to create a route table, advance to the next tutorial.

