This guide explains how to create a NAT network in Hyper-V on Windows Server 2022, and how to configure an Ubuntu VM and a Windows VM with static IP addresses on this network.
-
Open a PowerShell session as an administrator.
-
Create an internal virtual switch named "NAT":
New-VMSwitch -SwitchName "NAT" -SwitchType Internal
-
Identify the interface index of the NAT switch:
Get-NetAdapter
Look for "vEthernet (NAT)" in the output and note the corresponding interface index.
-
Assign an IP address to the NAT switch:
New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex <InterfaceIndex>
Replace
<InterfaceIndex>
with the interface index you identified in the previous step. -
Create a NAT object:
New-NetNat -Name MyNATnetwork -InternalIPInterfaceAddressPrefix 192.168.0.0/24
When the installer asks for network details, fill in the fields as follows:
- Subnet:
192.168.0.0/24
. This is the IP address range for your network. - Address: Choose an address within the range
192.168.0.2
to192.168.0.254
that is not in use by another device. This will be the IP address of your Ubuntu VM. - Gateway:
192.168.0.1
. This is the IP address of the NAT switch in your Hyper-V setup, which acts as the default gateway. - Name servers:
8.8.8.8, 8.8.4.4
. These are the DNS servers that your VM will use to resolve domain names. - Search domains: Leave this field empty unless you have a specific domain that you want to use for DNS searches.
Please note that these values are based on the network configuration described in this guide. Adjust them as necessary if your network is set up differently.
-
Open a terminal.
-
Edit the Netplan configuration file. The exact filename can vary, but it's typically
01-netcfg.yaml
or50-cloud-init.yaml
:sudo nano /etc/netplan/01-netcfg.yaml
-
Modify the file to set a static IP address. For example:
network: version: 2 renderer: networkd ethernets: ens3: dhcp4: no addresses: - 192.168.0.2/24 gateway4: 192.168.0.1 nameservers: addresses: [8.8.8.8, 8.8.4.4]
Replace
ens3
with the name of your network interface, if it's different. -
Save and close the file. In nano, you can do this by pressing
Ctrl+X
, thenY
, thenEnter
. -
Apply the changes:
sudo netplan apply
-
Open Network & Internet settings.
-
Open the Ethernet settings, then click on the network adapter that you want to configure.
-
In the settings for the network adapter, scroll down to the "IP settings" section and click on "Edit".
-
Change to a static IP configuration and enter the IP address and DNS details:
- IP address: 192.168.0.3
- Subnet prefix length: 24
- Gateway: 192.168.0.1
- Preferred DNS: 8.8.8.8
- Alternate DNS: 8.8.4.4
-
Save the changes.