Willi
Willi

Reputation: 407

How to prevent the ip address of hyper-v virtual switch from being changed?

I have a laptop with Windows 10 2004 installed. I configureded Hyper-V and created two VMs: On one VM runs windows 10 to enclose serveral malware IM softwares which I have to use for working contacts. On the other VM runs ubuntu server 20.04 for development.

I configured Hyper-V with a virtual switch of type internal network and Hyper-V automatically specified the virtual switch with a static IP. I want to connect to ubuntu with machine name. So I specified ubuntu VM with a static IP and added an item in the hosts file of the host windows 10. Then I can use the remote over SSH feature of VS Code to develop node.js app in windows 10 host. Everything went ok till I had restarted my laptop.

After I have restarted the laptop, the IP address of the hyper-v virtual switch was changed. I couldn't reconnect to VM ubuntu any more because the VM ubuntu was configured with a static IP and default gateway based on the old IP of the virtual switch.

I checked the IPv4 properties of the virtual switch via UI, It's configured as "using the following IP", so I thought of the IP shouldn't change. But I was wrong. Each time after my laptop being restarted, the "static IP" of virtual switch always changes. This change breaks the connection to VM ubuntu.

So, is there any way to prevent the IP address of the virtual switch from being changed? Or some way to add name resolving mechanism to Hyper-V virtual switch (then I can configure using dynamic IP address in VMs)?

Upvotes: 13

Views: 37428

Answers (4)

AlexPaskhin
AlexPaskhin

Reputation: 1

It is very simple Add second IP address Example

Linux:

sudo ip address add 172.22.88.73/20 dev eth0

Windows:

route ADD 172.22.88.73 MASK 255.255.255.255 172.30.224.1 METRIC 3

Upvotes: -1

Godfrey Tan
Godfrey Tan

Reputation: 89

Setting Static IP address in Ubuntu in Hyperv

Note: VM(s) are connected on "External Switch" in Hyperv

  1. Check DNS IP address VM uses

gtan@master:~$ systemd-resolve --status
...

Link 2 (eth0)
      Current Scopes: DNS
       LLMNR setting: yes
MulticastDNS setting: no
      DNSSEC setting: no
    DNSSEC supported: no
         DNS Servers: 192.168.141.81
          DNS Domain: mshome.net
  1. Check Gateway and Subnet Mask

gtan@master:/etc/netplan$ netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         _gateway        0.0.0.0         UG        0 0          0 eth0
10.44.0.0       0.0.0.0         255.255.255.0   U         0 0          0 cni0
10.44.1.0       10.44.1.0       255.255.255.0   UG        0 0          0 flannel.1
10.44.2.0       10.44.2.0       255.255.255.0   UG        0 0          0 flannel.1
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 br-894a4759cb12
172.18.0.0      0.0.0.0         255.255.0.0     U         0 0          0 docker0
192.168.141.80  0.0.0.0         255.255.255.240 U         0 0          0 eth0
_gateway        0.0.0.0         255.255.255.255 UH        0 0          0 eth0

eth0 Gateway = 192.168.141.81 mask 255.255.255.240 (192.168.141.80 is for broadcast)

  1. Check eth0 IP address and subnet mask

gtan@master: ifconfig
...
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.141.85  netmask 255.255.255.240  broadcast 192.168.141.95
        inet6 fe80::215:5dff:fe3a:1100  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:3a:11:00  txqueuelen 1000  (Ethernet)
        RX packets 82792  bytes 103449317 (103.4 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 40353  bytes 10745111 (10.7 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
...
  1. Edit "/etc/netplan/01-netcfg.yaml" to this (make a backup of the orginal 01-netcfg.yaml first: "sudo cp ./01-netcfg.yaml ./01-netcfg.yaml.bak"):
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses:
        - 192.168.141.85/28
      gateway4: 192.168.141.81
      nameservers:
          addresses: [8.8.8.8]
  1. Shutdown the VM

  1. In Hyperv, select the VM(s):

  • Go to Network Adaptor --> Advanced Features --> Set MAC address to "static"
  1. All good to go!!!

  2. After work, use "saved" instead of "paused" for the VMs'

Upvotes: 0

Muz
Muz

Reputation: 359

I just added an additional Internal switch via Hyper-V "Virtual Switch Manager" and gave it a name of "Static Switch". Then changed it's settings via Control Panel->Network and Internet->Network and Sharing Center->Change adapter settings. Right click on the new adapter and select Properties->Internet Protocol Version 4 (TCP/IPv4) and set a static IP that is not currently used on your network, for example IP address: 192.168.199.1 Subnet mask: 255.255.255.0. No other settings are required. Click Ok and close all the parent windows.

Add the new nic to the Hyper-V machine via the settings for the VM, Add Hardware->Network Adapter-Add. Select the 'Static Switch' and click OK.

Edit your client virtual machine network settings, in my case Ubuntu 20.10 so it's Wired Settings for eth1 (the new 'Wired Connection'). I disabled IPv6 and edited the IPv4 settings using a manual configuration (static IP) setting the IP address to 192.168.199.2 and a net mask of 255.255.255.0 and a gateway of 192.168.199.1 (The 'Static Switch'). No other settings are required. Click Apply and check that the network eth1 is connected.

You should be able to ping 192.168.199.1 from the client VM and ping 192.168.199.2 from the host machine.

This configuration will persist after a reboot and you can edit your hosts file on the host machine with whatever name you want, eg 192.168.199.2 my.vm.machine

Note: You will need to edit the hosts file as an administrator.

Upvotes: 17

Willi
Willi

Reputation: 407

  1. Don't use the default switch created by Hyper-V. As Miket25 said: "The default switch is made by HNS, and there may be settings that causes it to have a DHCP address on reboot".
  2. Create a internal virtual switch using NAT annually. Here is the guide. And then connect VMs to this switch. The static IP address assigned to this switch will not be changed through rebooting.

Upvotes: 2

Related Questions