DEV Community

Cover image for How to use Network Security Group (NSG) to allow and deny access to a virtual machine.
Olalekan Oladiran
Olalekan Oladiran

Posted on

How to use Network Security Group (NSG) to allow and deny access to a virtual machine.

Introduction

You can restrict network traffic to resources in your virtual network by using network security groups. A list of security rules that permit or prohibit inbound or outgoing network traffic is contained in network security groups.

Steps involved in creating Network Security Group

The first thing to do is to create a virtual machine to use in testing the network security.

  • Click create a resource Image description
  • Search for virtual machine in the market place and click create Image description
  • Fill the project details which comprises of subscription and resource group Image description
  • Fill in the instance details Image description
  • Fill the Administrator account Image description
  • In the inbound port rules section, select none for Public inbound ports Image description
  • Select the Networking tab and choose none for NIC network security group. Image description
  • Select the monitoring tab, disable Boot diagnostics and leave other settings as default, click create + review after Image description
  • Click create once validation is passed Image description
  • Wait for it to deploy and click go to resource once deployment is complete Image description
  • In the overview page, Click Networking in the settings. You will notice that there is no network security group in the inbound port rules. Also take note of the Network Interface Card name Image description

How to create Network Security Group

  • Click create a resource Image description
  • Search for virtual machine in the market place and click create Image description
  • Fill the project details Image description
  • Fill the instance details Image description
  • Click preview + create Image description
  • Click create once validation passed Image description
  • Click go to resource once deployment is complete Image description
  • Click Network interfaces in the settings section and click associate Image description
  • Click Network Interface association and select the name of the interface noted earlier from the drop down, click Okay after. Image description Image description

How to configure Inbound security port rule to give access to RDP.

  • Go back to the virtual machine created earlier and click connect in the overview page Image description
  • We need to connect to the virtual machine by downloading the RDP. First click connect under Native RDP Image description
  • Click download RDP Image description
  • After downloading the RDP file click on the file to launch it and the launch will fail because the Network Security Group created will not allow RDP Image description
  • Click Networking under settings in the virtual machine and click Add inbound port rule Image description
  • Configure the inbound rule with the following settings Destination port ranges: 3389 Protocol: TCP Priority: 300 Name: AllowRDP Leave other settings as default and click add Image description Image description
  • We need to try to connect the virtual machine again to check if the inbound rule is working. Download the RDP file again
  • After downloading the RDP file click on the file to launch it and click connect Image description
  • Enter your username and password and click Ok Image description
  • Click yes Image description

How to configure Outbound port rule to deny access to internet

  • First check if the virtual machine connected to can access internet by opening internet explorer and search bing.com Image description This shows that outbound port rule is allowed
  • Click networking in the settings, click outbound port rule and select add outbound port rule Image description
  • Enter the following settings Destination: Service Tag Destination port ranges: * protocol: TCP Action: Deny Priority: 4000 Name: DenyInternet Leave other settings as default and click save Image description Image description
  • To check if the outbound rule is working, go back to the connected virtual machine and search bing.com in internet explorer. The access will be denied Image description

Top comments (0)