Skip to content

Instantly share code, notes, and snippets.

@Pristavkin
Forked from mzaglia/hyper-v-static-ip.md
Last active July 26, 2022 11:41
Show Gist options
  • Save Pristavkin/0b579549c85520cf78f666406fb20972 to your computer and use it in GitHub Desktop.
Save Pristavkin/0b579549c85520cf78f666406fb20972 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 "Hyper-V [VM NAT]" -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 HyperV-NATnetwork -InternalIPInterfaceAddressPrefix 192.168.0.0/24

In your ubuntu server open your network netplan

cd /etc/netplan
sudo vi <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

To have an external access to vm, add dstNAT on the host machine

netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=3022 connectaddress=192.168.0.2 connectport=22

Source

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