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

4

10

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)?

Brochure answered 17/8, 2020 at 10:27 Comment(7)
How are you setting the static IP address for the vswitch? To be clear, when you assign a static IP address to the vswitch, you are assigning this static address on the host partition, not the VMs themselves.Foregut
The path: control panel->network and internet->network connection. There is an adapter called "vEthernet (Default Switch)". I rightclicked this adapter and open the properties UI. In the "network" tab I selected "...IPV4" and open its property, then the classical IP address setting UI is shown. What is shown in the UI is that the adapter "using the following IP address".Brochure
Why I need the information from the virtual switch? The cause is that I need gateway information when I cnofigure VM ubuntu with static IP address. Remember that I have installed another VM on which a windows 10 copy runs. The windows 10 VM is configured as using dhcp. That's to say, all IP configurations are created by Hyper-V. I observed that the IP of virtual switch is used as default gateway in the windows 10 VM. So I configured similarly the ubuntu VM with same gateway.Brochure
However, Now I know the IP of the virtual switch changes after every rebooting. It requires that I have to modify ubuntu's IP and gateway every time or just use dhcp. Both situations break the remote connection saved in VS Code.Brochure
How was the internal v-switch made? Or was the default v-switch already there when Hyper-V was installed?Foregut
You can have a try. Internal network is the default network mode. The default switch is automatically created if you have no virtual switch yet while you creat VM. I think that shouldn't be problem.Brochure
The default switch is made by HNS, and there may be settings that causes it to have a DHCP address on reboot. Try creating the vswitch through Hyper-V, as this does not go through HNSForegut
L
14

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.

Leatherwood answered 20/4, 2021 at 2:52 Comment(1)
In order to ping the Windows 11 host, I have to add rules in the host firewall to allow ICMP.Menderes
B
2
  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.
Brochure answered 19/8, 2020 at 13:11 Comment(2)
I tried this and it did not work and broke the existing switch until my new one was deleted, "Note: Currently, you are limited to one NAT network per host. "Tonisha
But why do we need to create NAT to make static IP for our Hyper V VM?Ununa
K
0

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'

Kingfisher answered 31/3, 2021 at 4:50 Comment(1)
VMs' must be connected to "external" switch and not "default". Sorry for the error.Kingfisher
T
-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

Tola answered 1/10, 2023 at 17:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.