XF

NetNat for Hyper-V Virtual Machines

As stated on the Arch Linux Wiki,

As of version 1803 (“Fall Creators Update”), Windows 10 has a NAT Switch built in (named “Default Switch”) that gives you instant internet access after installing your virtual machine if you set it up to use DHCP, see this Technet blog post, so you do not need the instructions below to get a working NAT switch.

However, the Default Switch in Hyper-V will assign a random IP address to each Virtual Machine when rebooted, which can be inconvenient. This behavior is not easily configurable. If you want to assign a static IP address to a Virtual Machine for a specific purpose like SSH connectivity, you can create a new Internal Switch with NAT capability.

Create Internal Switch with NetNat

Follow the tutorials from ArchWiki to create the Internal Switch with NAT:

# Create the internal switch with a name of VM-Internal-Switch
PS C:\WINDOWS\system32> New-VMSwitch -Name "VM-Internal-Switch" -SwitchType Internal

# Verify that the internal switch was created
PS C:\WINDOWS\system32> Get-VMSwitch

# Get the ifIndex of the newly created internal switch, usually named 'vEthernet (name)'
PS C:\WINDOWS\system32> Get-NetAdapter

# Set the IP address of the internal switch, noting the ifIndex retrieved from the previous command.
# In this example, the network address of the internal switch is 192.168.3.0/24, and the ifIndex is 50.
PS C:\WINDOWS\system32> New-NetIPAddress -IPAddress 192.168.3.1 -PrefixLength 24 -InterfaceIndex 50

# The virtual machines using the internal switch must use static IP addresses, such as 192.168.3.2/24.
# Support for DHCP in internal switches is not included in current Windows versions (as of Version 1703, OS Build 15063).
# Create the NAT with IP address of the internal switch
PS C:\WINDOWS\system32> New-NetNat -Name "VM-NAT-Network" -InternalIPInterfaceAddressPrefix 192.168.3.1/24

# Verify that the NAT was created
PS C:\WINDOWS\system32> Get-NetNat

# Enable SSH port forwarding on port 2222 of any interface on the host, to a virtual machine with an IP address of 192.168.3.2/24
PS C:\WINDOWS\system32> Add-NetNatStaticMapping -NatName "VM-NAT-Network" -Protocol TCP -ExternalIPAddress 0.0.0.0 -ExternalPort 2222 -InternalIPAddress 192.168.3.2 -InternalPort 22

# Verify that the port forward is active
PS C:\WINDOWS\system32> Get-NetNatStaticMapping

Microsoft has provided more detailed documentation on setting up a NAT network in Hyper-V.

Network Settings in Virtual Machines

By default, the Internal Switch does not automatically assign IP addresses like a DHCP server to Virtual Machines. We need to manually configure static IPs inside each VM. Setting static IPs is appropriate in this case since they are used for remote connection purposes.

We can attach two Network Adapters to each Virtual Machine: One linked to the Default Switch for external network access, and the other connected to the newly created Internal Switch for remote connections using the static IP addresses.

FreeBSD

Edit /etc/rc.conf file. Set the first network adapter to use DHCP for automatic IP assignment. For the second adapter, specify a static IP configuration:

# /etc/rc.conf
ifconfig_hn0="DHCP"
ifconfig_hn1="inet 192.168.3.2 netmask 255.255.255.0"
# other configurations ...

You may find the detailed documentation in FreeBSD Handbook.

Arch Linux

When using systemd-networkd, create the following configuration files:

#/etc/systemd/network/20-default.network
[Match]
Name=eth0

[Network]
DHCP=yes
#/etc/systemd/network/25-internal.network
[Match]
Name=eth1

[Network]
Address=192.168.3.3/24
Gateway=192.168.3.1
DNS=192.168.3.1