How to open ports and endpoints to a VM using PowerShell

Applies to: ✔️ Linux VMs ✔️ Windows VMs ✔️ Flexible scale sets

You open a port, or create an endpoint, to a virtual machine (VM) in Azure by creating a network filter on a subnet or a VM network interface. You place these filters, which control both inbound and outbound traffic, on a network security group attached to the resource that receives the traffic.

The example in this article demonstrates how to create a network filter that uses the standard TCP port 80 (it's assumed you've already started the appropriate services and opened any OS firewall rules on the VM).

After you've created a VM that's configured to serve web requests on the standard TCP port 80, you can:

  1. Create a network security group.

  2. Create an inbound security rule allowing traffic and assign values to the following settings:

    • Destination port ranges: 80

    • Source port ranges: * (allows any source port)

    • Priority value: Enter a value that is less than 65,500 and higher in priority than the default catch-all deny inbound rule.

  3. Associate the network security group with the VM network interface or subnet.

Although this example uses a simple rule to allow HTTP traffic, you can also use network security groups and rules to create more complex network configurations.

Quick commands

To create a Network Security Group and ACL rules you need the latest version of Azure PowerShell installed. You can also perform these steps using the Azure portal.

Log in to your Azure account:


In the following examples, replace parameter names with your own values. Example parameter names included myResourceGroup, myNetworkSecurityGroup, and myVnet.

Create a rule with New-AzNetworkSecurityRuleConfig. The following example creates a rule named myNetworkSecurityGroupRule to allow tcp traffic on port 80:

$httprule = New-AzNetworkSecurityRuleConfig `
    -Name "myNetworkSecurityGroupRule" `
    -Description "Allow HTTP" `
    -Access "Allow" `
    -Protocol "Tcp" `
    -Direction "Inbound" `
    -Priority "100" `
    -SourceAddressPrefix "Internet" `
    -SourcePortRange * `
    -DestinationAddressPrefix * `
    -DestinationPortRange 80

Next, create your Network Security group with New-AzNetworkSecurityGroup and assign the HTTP rule you just created as follows. The following example creates a Network Security Group named myNetworkSecurityGroup:

$nsg = New-AzNetworkSecurityGroup `
    -ResourceGroupName "myResourceGroup" `
    -Location "EastUS" `
    -Name "myNetworkSecurityGroup" `
    -SecurityRules $httprule

Now let's assign your Network Security Group to a subnet. The following example assigns an existing virtual network named myVnet to the variable $vnet with Get-AzVirtualNetwork:

$vnet = Get-AzVirtualNetwork `
    -ResourceGroupName "myResourceGroup" `
    -Name "myVnet"

Associate your Network Security Group with your subnet with Set-AzVirtualNetworkSubnetConfig. The following example associates the subnet named mySubnet with your Network Security Group:

$subnetPrefix = $vnet.Subnets|?{$_.Name -eq 'mySubnet'}

Set-AzVirtualNetworkSubnetConfig `
    -VirtualNetwork $vnet `
    -Name "mySubnet" `
    -AddressPrefix $subnetPrefix.AddressPrefix `
    -NetworkSecurityGroup $nsg

Finally, update your virtual network with Set-AzVirtualNetwork in order for your changes to take effect:

Set-AzVirtualNetwork -VirtualNetwork $vnet

More information on Network Security Groups

The quick commands here allow you to get up and running with traffic flowing to your VM. Network Security Groups provide many great features and granularity for controlling access to your resources. You can read more about creating a Network Security Group and ACL rules here.

For highly available web applications, you should place your VMs behind an Azure Load Balancer. The load balancer distributes traffic to VMs, with a Network Security Group that provides traffic filtering. For more information, see How to load balance Linux virtual machines in Azure to create a highly available application.

Next steps

In this example, you created a simple rule to allow HTTP traffic. You can find information on creating more detailed environments in the following articles: