How to configure static IP address for a Hyper-V VM (Ubuntu 19.10 Quick Create) to avoid updating ssh config after each reboot?
I'm using VS-Code remote development plugin to edit code on a hyper-v VM (which is a Ubuntu 19.10 - quick create) which is running on Windows 10 on my Laptop. The VM uses hyper-v's default switch for networking. The VS-Code remote development plugin allows to edit a ssh config file (C:\Users\username\ssh\config) which makes it easy to connect to the VM. Here' how my ssh config looks like:
Host hypervubuntu
HostName 172.18.10.76
User my_UbuntuVM_username
Settings of my hyper-v default switch (it looks like it's set to use a static IP but actually this settings change after each reboot of Windows):
The issue is that on every Windows reboot the IP address of the VM (and hyper-v's default switch) change requiring to edit ssh config to allow VS-Code to connect to the VM again. The change of IP also leads to further issues like the need to restart the VM and to confirm the authenticity of "new" host on every new ssh connect.
I've tried setting a static IP in VM's network settings but this seems not to persist since they change back to "Automatic (DHCP)" after each reboot of the VM.
As suggested in another post I've tried to create a new virtual switch w/ a static IP cause it's seems that Hyper-V's default switch is not meant to have a static IP. But this is what I could not get to work at all.
Which parts have to be configured to allow VS-Code reconnect smoothly even after reboot of Windows or VM?
DISCLAIMER: my network skill level == noob :\
EDIT:
Steps I needed to perform after great answer from @AlexKrauss to set static IP in my Hyper-V Ubuntu 19.10 Desktop VM:
I. located and opened network config file
cd /etc/netplan/
sudo nano 01-network-manager-all.yaml
II. replaced it's content as follows
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
III. applied & checked changes
sudo netplan apply
ifconfig -a
IV. adjusted IP address in ssh config used by VSCode
Host hypervubuntu
HostName 192.168.0.2
User my_UbuntuVM_username
I had the same problem, and found a step-by-step guide from Microsoft for setting up an internal switch with NAT, and connecting VMs to it.
It consists of the following steps in PowerShell
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
This uses 192.168.0.0/24 as the subnet for the virtual switch, where 192.168.0.1 is the IP of the host, which acts as a gateway.
Now, the VM can be connected to the new switch in Hyper-V Manager.
Note that unlike the Default Switch, there is no automatic network configuration via DHCP, so inside the VM, you will have to configure a static IP (e.g., 192.168.0.2) in the VM.
All you have to do now is
ssh <MULTPASS_VM_NAME>.mshome.net
and you are done!