• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
OpenTechTips

OpenTechTips

Comprehensive IT Guides for Pros and Enthusiasts

MENUMENU
  • HOME
  • ALL TOPICS
    • Exchange
    • InfoSec
    • Linux
    • Networking
    • Scripting
      • PowerShell
    • SSL
    • Tools
    • Virtualization
    • Web
    • Windows
  • ABOUT
  • SUBSCRIBE
Home » How to create NAT-ed subnets in Hyper-V

How to create NAT-ed subnets in Hyper-V

May 17, 2021 - by Zsolt Agoston - last edited on May 17, 2021

In this exercise we go through the process of setting up a Hyper-V environment on a regular computer, with multiple virtual switches and subnets configured on them.

How to create NAT-ed subnets in Hyper-V

  1. Install Hyper-V
  2. Add as many Internal vSwitches as you want
  3. Configure the management IP address on the virtual switches
  4. Set up NAT rules to allow the subnets to reach the Internet

1. The Goal

In this example we use a home PC with Windows 10 running on.

Our goal is to set up three new subnets in Hyper-V for our virtual environment as follows:

Subnet1: 10.0.0.0/24
Subnet2: 10.0.1.0/24
Subnet3: 10.0.2.0/24

Then install new servers connected to each subnet and test internet connectivity.

2. Preparation: Install Hyper-V

The first step is to install Hyper-V on the computer.

a. Right-click on the Windows button on the taskbar, and select “Run”. Type in “appwiz.cpl” and hit ENTER.

How to create NAT-ed subnets in Hyper-V

b. Click on the “Turn Windows features on or off” option in the left-hand side menu. Make sure the “Hyper-V” feature is checked, click OK.

How to create NAT-ed subnets in Hyper-V

c. After installation restart the computer

3. Add subnet(s) to Hyper-V

Now we are ready to install new virtual computers and utilize our Hyper-V host. However, at the moment we only have a single virtual switch called “Default Switch” automatically set up for Hyper-V. All virtual machines that we deploy will be using that switch by default, which has a random IP subnet assigned and NAT-ed for external network access. For instance, we install a new virtual machine and see that it has an IP of 172.28.100.10 assigned that we cannot modify.

We want more control. We want to use the subnet of our choice. In fact, we want to have multiple subnets to simulate a complex network, all running on our single physical machine. For example to build an Active Directory lab with multiple sites that require multiple subnets.
To achieve that we add new internal switches to the mix, configure their IP settings (using the subnets 10.0.0.0/24, 10.0.1.0/24, 10.0.2.0/24 in this example but they can be whatever you want).

We can repeat this process with as many subnets as needed.

a. Open the Hyper-V Management console from the Start menu.

b. Right-click on the computer name (here it’s TC21, but you might have a different name on your computer)

How to create NAT-ed subnets in Hyper-V

c. Select the “New virtual network switch”, highlight the “Internal” type and hit the “Create Virtual Switch” button.

How to create NAT-ed subnets in Hyper-V

d. Put in the name: “Subnet1”, leave all setting as they are and hit “Apply”

How to create NAT-ed subnets in Hyper-V

e. Repeat the process as many times as you want. We need three subnets thus three new switches.

How to create NAT-ed subnets in Hyper-V

4. Configure IP addresses on the virtual switch interfaces

After creating the new switches all of them will have a management interface created automatically. We can check that opening the “Network and Sharing Center” window, and browsing for adapter settings.

a. The easiest way is to right-click on the Windows button and select “Run” (or alternatively hit the [Windows] + [R] keys simultaneously on the keyboard), and type in: ncpa.cpl, then hit ENTER.

How to create NAT-ed subnets in Hyper-V

b. We can see all of our management interfaces there. They are all called "vEthernet ([virtual switch name])". To configure the IP settings for Subnet1, right-click on “vEthernet (Subnet1)” and choose “Properties”

How to create NAT-ed subnets in Hyper-V

c. There highlight “Internet Protocol Version 4”, and click on “Properties”

How to create NAT-ed subnets in Hyper-V

d. Select the “Use the following IP address” radio button, and put in the desired IP address for the first virtual switch. We want the subnet 10.0.0.0/24 set. This covers the 10.0.0.1 – 10.0.0.254 IP range, we use the last usable host for the switch, but again: it can be any address in the range we like.

For more details on how subnetting works click here. The /24 subnet has the 255.255.255.0 subnet mask, we don’t worry about the default gateway, not the DNS servers. Those can stay empty.

How to create NAT-ed subnets in Hyper-V

e. Do the same for the other virtual switches. Switch2 will have the IP: 10.0.1.254 and subnet mask 255.255.255.0, and Switch3 the IP: 10.0.2.254 and subnet mask 255.255.255.0

5. Set up NAT for the subnet(s)

At this point we have multiple virtual switches, with multiple subnets. However, if virtual machines are connected to those switches, they won’t have access neither to the internet, nor to each other.

To make it work we have to set up NAT (Network Address Translation) for each new subnet separately. Hosts in that particular subnet will use the IP address of the virtual switch as their default gateway (10.0.0.254, 10.0.1.254 or 10.0.2.254 respectively), and the switch will be acting on behalf of the hosts when accessing external network addresses. For more information on how Network Address Translation, or more precisely PAT (Port Address Translation) works, please check out this article.

Open up an elevated PowerShell window and use these commands to create the appropriate NAT rules:

New-NetNat -name "Subnet1 NAT" -InternalIPInterfaceAddressPrefix 10.0.0.0/24
New-NetNat -name "Subnet2 NAT" -InternalIPInterfaceAddressPrefix 10.0.1.0/24
New-NetNat -name "Subnet3 NAT" -InternalIPInterfaceAddressPrefix 10.0.2.0/24

6. Testing

Lastly, we add a virtual machine to the new subnet, and test it.

We install a new virtual machine in our Hyper-V environment, connecting it to the Subnet0 virtual switch.

The new virtual server will look like this:

How to create NAT-ed subnets in Hyper-V

Note, that DHCP service is NOT running to assign automatic IP addresses for hosts. Later we can use this same server to configure the DHCP service for the subnet 🙂

For now we assign an IP address manually for the new host: 10.0.0.1/24.

How to create NAT-ed subnets in Hyper-V

All that left is testing network connectivity. We get response back from both the virtual switch (10.0.0.254), and a public Google server (8.8.8.8) so now we confirmed that external network access is working!

How to create NAT-ed subnets in Hyper-V

Note: by default, Windows computers are blocking incoming ping requests, so you might not get a response back from your virtual switch. That doesn’t mean it is broken, only that you have not enabled incoming ping (ICMP echo) packets. That doesn’t affect network connectivity 🙂

7. Routing between Subnets on the same Hyper-V host

Note that the basic Hyper-V system only has NAT functionality, it has no other routing capabilities. That means subnets on the same Hyper-V machine can communicate with the host machine and with the external network, but not with each other. If we want to allow the configured subnets to communicate with each other, we need a layer 3 (router) server to provide routing. Detailed guide is coming

Reader Interactions

Comments

  1. Aamir says

    February 10, 2022 at 05:34

    Excited to learn more on it. Awesome!!!!

    Reply
  2. success says

    March 25, 2022 at 00:14

    Great guide, but I found a small part that is a bit confusing.

    The sentence “For now we assign an IP address manually for the new host… should read “For now we assign an IP address manually for the new guest…”

    Using the word host is a bit confusing because it makes it seem like you are setting up a new Hyper-V host.

    I had struggled for the last day or so and managed to get the internet connection working with a hack from this post https://superuser.com/a/949806, but I didn’t like how much of a jerryrigged solution it was, and the guide here is a much more proper and correct way to provide internet for a Hyper-V guest.

    After figuring this out I managed to get the internet connection working for Ubuntu Server within Hyper-V on Windows Server 2019.

    Reply

Comments Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tools

Secondary Sidebar

CONTENTS

  • How to create NAT-ed subnets in Hyper-V
  • 1. The Goal
  • 2. Preparation: Install Hyper-V
  • 3. Add subnet(s) to Hyper-V
  • 4. Configure IP addresses on the virtual switch interfaces
  • 5. Set up NAT for the subnet(s)
  • 6. Testing
  • 7. Routing between Subnets on the same Hyper-V host

  • Terms of Use
  • Disclaimer
  • Privacy Policy
Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
Manage your privacy
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}