Skip to content

Instantly share code, notes, and snippets.

@mzaglia
Last active March 7, 2024 09:02
Show Gist options
  • Save mzaglia/d51893ec08937d15c83660e028c1edee to your computer and use it in GitHub Desktop.
Save mzaglia/d51893ec08937d15c83660e028c1edee to your computer and use it in GitHub Desktop.
Static IP Hyper-V Ubuntu VM

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

Source

@arafatx
Copy link

arafatx commented Aug 1, 2022

What makes me confused is the IP address of 192.168.0.1 as stated here: https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/user-guide/setup-nat-network (this is similar to your example).

It says, that the example IP "192.168.0.1" acts like a gateway to the host (eg: my computer that hosting a virtual machine). They came out with a confusing example of an IP address, as that IP is too common for a physical router gateway.

Instead of creating an internal switch, I end up creating an external switch that works perfectly fine.

@Mason117
Copy link

Mason117 commented Jul 26, 2023

gateway4 has been deprecated, can use below example:

network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses:
- 192.168.0.2/24
routes:
- to: default
via: 192.168.0.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
dhcp4: no

@sergromanenko1
Copy link

sergromanenko1 commented Dec 27, 2023

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

@juvuorin
Copy link

juvuorin commented Mar 7, 2024

Does dns work also with this setup? Assuming that host is utilizing DCHP to get DNS and IP?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment