First in a powershell create a new network switch
New-VMSwitch -SwitchName "SwitchName" -SwitchType Internal
Get-NetAdapter // (note down ifIndex of the newly created switch as INDEX)
New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex <INDEX>
New-NetNat -Name MyNATnetwork -InternalIPInterfaceAddressPrefix 192.168.0.0/24
In your ubuntu server open your network netplan
cd /etc/netplan
sudo nano <your-net-plan>
Type the following into your netplan
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses:
- 192.168.0.2/24
gateway4: 192.168.0.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
dhcp4: no
Then
sudo netplan apply
IP addresses like 192.168.0.x can be used on a local WiFi network, so it's better to replace them with 192.168.10.x so in a powershell:
New-VMSwitch -SwitchName "SwitchName" -SwitchType Internal
Get-NetAdapter // (note down ifIndex of the newly created switch as INDEX)
New-NetIPAddress -IPAddress 192.168.10.1 -PrefixLength 24 -InterfaceIndex
New-NetNat -Name MyNATnetwork -InternalIPInterfaceAddressPrefix 192.168.10.0/24
After creating a new network switch, you must add it to the VM in Hyper V Manager, without that static IP won't be working.
All you need in your netplan Ubuntu:
network:
ethernets:
eth0:
addresses:
- 192.168.10.2/24
version: 2
If you had another switch in your VM before adding a new one, for example "Default Switch" then set eth1 instead of eth0, you need in your netplan Ubuntu:
network:
ethernets:
eth0:
dhcp4: true
eth1:
addresses:
- 192.168.10.2/24
version: 2