Open Ports to a Virtual Machine in Microsoft Azure

Open Ports to a VM in Microsoft Azure
This tutorial focuses on the steps to open ports to a virtual machine (VM) with the Azure portal and PowerShell.

 

Windows Azure, lately renamed Microsoft Azure in 2014, is a public cloud computing platform offered by Microsoft to build, manage, and deploy services and applications through a global network of data centers. Azure provides a range of cloud services, such as analytics, compute, networking, storage, and more. Azure is a public cloud provider with solutions including Software as a Service (SaaS), Infrastructure as a Service (IaaS), and Platform as a Service (PaaS). Azure is a flexible, fast, affordable, and reliable cloud computing platform. The capabilities and pricing of this platform make it one of the best public cloud offerings in today’s market.

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

This tutorial demonstrates the steps to create a network filter that uses the standard TCP port 80. We assume that the user has already started the appropriate services and opened any operating system firewall rules on the virtual machine (VM). After the user has created a virtual machine that’s configured to serve web requests on the standard TCP port 80, they can perform the following:

 

  1. Create a network security group.
  2. Create an inbound security rule allowing traffic and assign the settings values:
  • Destination port ranges: 80
  • Source port ranges: * (allows any source port)
  • Priority value: Enter a value less than 65,500 and higher in priority than the default catch-all deny inbound rule.
  1. Associate the network security group with the subnet or VM network interface.

 

Using the Azure Portal

In order to open a port to a virtual machine (VM) with the Azure portal, follow the below steps:

Step 1: Sign in to Azure Portal

First, sign in to the Azure portal using the below URL:

https://portal.azure.com

 

Step 2: Create a Network Security Group
  1. Search and select the resource group for the virtual machine (VM), choose Add, then search for and select the Network security group option.
  2. Then, click the Create button.
  3. After that, the Create network security group window opens, as shown below.

 

Open Ports to a VM in Microsoft Azure

 

  1. Enter a name for the user’s network security group and select or create a resource group, then choose a location.
  2. Finally, select the Review + create button to create the network security group.

 

Step 3: Create an Inbound Security Rule
  1. Select the newly created network security group.
  2. Then, choose the Inbound security rules option from the left menu and click the Add button.
  3. Users can limit the Source, and Source port ranges as needed or choose the default (Any) setting.
  4. Users can also limit the Destination details as required or leave the default (Any) settings.
  5. After that, select a standard Service from the drop-down menu, such as HTTP. Users can also choose Custom settings if they want to provide a specific port to use.
  6. (Optional) Then, change the Name or Priority. The priority will affect the order in which the rules get applied: the lower the numerical value, the earlier the rule is applied.

 

Open Ports to a VM in Microsoft Azure

 

  1. Finally, click the Add button to create the rule.

 

Step 4: Associate the Network Security Group with a Subnet

The final step is associating the user’s network security group with a specific network interface or a subnet. For this tutorial, we will associate the network security group with a subnet. For that, follow the below steps:

  1. First, select the Subnets option from the left menu, and then select the Associate option.
  2. Select the user’s virtual network, and choose the appropriate subnet from the drop-down.

 

Open Ports to a VM in Microsoft Azure

 

  1. Finally, click the OK button.

 

Using the PowerShell

In order to create a Network Security Group and ACL rules, users need the latest version of Azure PowerShell installed. Follow the below steps to open ports and endpoints to a VM using PowerShell.

First, log in to the Azure account by executing the following command:

 

Connect-AzAccount

 

In the following examples, replace the parameter names with the user’s own values. Example parameter names include the Resource Group, Network Security Group, and Virtual Network details.

Then, create a rule with New-AzNetworkSecurityRuleConfig. In this tutorial, we will create a rule named myNetworkSecurityGroupRule to allow TCP traffic on port 80, as shown below:

 

$httprule = New-AzNetworkSecurityRuleConfig `

    -Name "myNetworkSecurityGroupRule" `

    -Description "Allow HTTP" `

    -Access "Allow" `

    -Protocol "Tcp" `

    -Direction "Inbound" `

    -Priority "100" `

    -SourceAddressPrefix "Internet" `

    -SourcePortRange * `

    -DestinationAddressPrefix * `

    -DestinationPortRange 80

 

Next, create the Network Security group with New-AzNetworkSecurityGroup and assign the HTTP rule that the user just created as follows. The following will create a Network Security Group named myNetworkSecurityGroup:

 

$nsg = New-AzNetworkSecurityGroup `

    -ResourceGroupName "myResourceGroup" `

    -Location "EastUS" `

    -Name "myNetworkSecurityGroup" `

    -SecurityRules $httprule

 

Now, assign the created Network Security Group to a subnet. The following example will assign an existing virtual network named myVnet to the variable $vnet with Get-AzVirtualNetwork, as shown below:

 

$vnet = Get-AzVirtualNetwork `

    -ResourceGroupName "myResourceGroup" `

    -Name "myVnet"

 

After that, associate the created Network Security Group with the subnet with Set-AzVirtualNetworkSubnetConfig. The following example will associate the subnet named mySubnet with the Network Security Group, as shown below:

 

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

 Set-AzVirtualNetworkSubnetConfig `

    -VirtualNetwork $vnet `

    -Name "mySubnet" `

    -AddressPrefix $subnetPrefix.AddressPrefix `

    -NetworkSecurityGroup $nsg

 

Finally, update the virtual network with Set-AzVirtualNetwork. In order for the changes to take effect, execute the following command:

 

Set-AzVirtualNetwork -VirtualNetwork $vnet

 

Conclusion

This tutorial presents the steps to open ports to a virtual machine (VM) with the Azure portal and PowerShell. Hope this tutorial was helpful, and do reach out to us if you have any queries or suggestions.

Share this post

Services to Explore

Stay up to date!

Stay up to date with the Web Hosting, Cloud and Server Management Industry News and Tutorials!

We will send you only the relevant emails, and we respect your privacy. Please review our privacy policy for more info.

Managed Microsoft Azure Services

Focus on your business, and let us take care of your Azure Cloud Infrastructure!
From what you are reading, it seems you are interested in Azure Cloud and related technologies. If you have a moment to spare, please take a look at our Managed Azure Services, which might interest you even more!
Managed Azure Cloud

Value-Added Services

We have services that can help you run a successful business. With us, you don't have to worry about these areas because our experts will take care of it for you.

ServerHealers uses cookies.