I created several VMs on Hyper-V on a Windows 10 Pro Client with a wired USB Ethernet adapter. The Windows machine keeps its IP address after a restart, the Linux machine (Ubuntu Server) doesn't. Both are using the Default Switch (note: the external switch does not work on USB Ethernet adapters).
I noticed the Windows VM kept its MAC address, so I tried to change the MAC address for the Linux VM from dynamic to static. Unfortunately that did not help.
The goal is to establish an SSH connection between the host and the Linux VM (always via the same IP address). In addition to that I want the VM to have access to the Internet. Note: the host is a laptop therefore the Internet connection will be established through different LANs and through different interfaces (build in WLAN, different wired USB-LAN adapter on different docking stations, etc.)
SOLUTION:
Currently I'm looking at configuring an internal switch. This setup suggestion relies on giving the internal switch static settings. Unfortunately it does not allow to set up a default gateway, so the VM can't use the Hosts Internet connection (at least I didn't find a solution to do that with the internal switch).
Note: I use 192.168.192.1/24 for the switch and 192.168.192.2/24 for the VM. I can ping the switch and the VM from the host and vice versa (ones I set the Windows firewall on the host appropriately).
To get simultaneous access the Internet (while using the internal switch to connect from the host to the VM via a non-changing IP address) I added a second network interface to the VM and set it to the default switch. I configured the second NIC to get its configuration via DHCP (via /etc/netplan/00-installer-config.yaml
):
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses: [192.168.192.2/24]
eth1:
dhcp4: true
ip a
shows that it's up and running:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:15:5d:97:34:01 brd ff:ff:ff:ff:ff:ff
inet 192.168.192.2/24 brd 192.168.192.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::215:5dff:fe97:3401/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:15:5d:97:34:03 brd ff:ff:ff:ff:ff:ff
inet 192.168.204.131/28 brd 192.168.204.143 scope global dynamic eth1
valid_lft 86333sec preferred_lft 86333sec
inet6 fe80::215:5dff:fe97:3403/64 scope link
valid_lft forever preferred_lft forever
Pinging the internal switch (192.168.204.129
) works (mind the Windows firewall!), pinging an outside server, e.g., 8.8.8.8 works as well. Thanks to JW0914 for his suggestions in the comments!
Please also note John's answer. It wasn't appropriate for my case, but it gives you the nice bonus that the VM is reachable from the routers LAN (and not only from the host).
Default Switch
is to provide WAN access only and won't provide a static IP (Microsoft made this change a few years ago to make Hyper-V VM setup simpler, as previously an External switch had to be created in Switch Manager). Depending on why you need a static IP: (1) to have the router assign the static IP, create an External switch [@John's answer]; (2) to allow client and host communication for RDP, file sharing, etc., create an Internal switch [Switch Manager]; (3) to allow VMs to communicate with one another, create a Private switch [Switch Manager]Default
switch will have a dynamically allocated172.x.x.x
IP for providing WAN access and the Internal switch will have whatever IP and Subnet Mask you set on each OS (must be manually configured on the host and the client OSes since it doesn't have a Gateway/DHCP server)