Set up network connection in Hyper-V
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: trueip 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 foreverPinging 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).
112 Answers
The sole purpose of the Default Switch is to provide WAN access and won't provide a static IP by design:
- 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, and provides access to the host’s network regardless of connection type
Depending on why a static IP is needed, create in Virtual Switch Manager or PowerShell:
Traffic managed by router:
- External switch: router assigns static IP (@John's answer)
- Create one for both ethernet and WiFi, as VMs do not auto-switch between wired/wireless
Traffic managed by host:
- Internal switch: manually assign IP subnet on host and client
- Allows communication between VM ⟷ host and/or VM ⟷ VM
- Lacks DHCP server and Gateway, so only the IP and subnet mask would be set
Avoid using subnets normally used by routers [192.168.1.0/24,2.0/24,100.0/24, etc.], else issues will occur when connecting to a router using the same subnet
- Private switch: manually assign IP subnet on clients
- Allows VM ⟷ VM communication that is isolated from host's traffic
- Hyper-V Manager → Actions → Virtual Switch Manager
- New virtual network switch → Create virtual switch → <type> → Create Virtual Switch
- Create a Name → Select Connection type → OK/Apply
+R →
powershell→ Ctrl+Shift+OK- List existing network adapters and note the
Nameof the adapter to use:PS $ Get-NetAdapter Name InterfaceDescription ifIndex Status MacAddress LinkSpeed ---- -------------------- ------- ------ ---------- --------- vWiFi Hyper-V Virtual Ethernet Adapter #3 62 Up A0-11-22-33-44-55 10 Gbps LAN Killer E2200 Gigabit Ethernet Controll… 25 Disconnected B0-11-22-33-44-55 0 bps BT Bluetooth Device (Personal Area Networ… 23 Disconnected B1-11-22-33-44-55 3 Mbps vLAN Hyper-V Virtual Ethernet Adapter 54 Up A1-11-22-33-44-55 10 Gbps vDefault Hyper-V Virtual Ethernet Adapter #2 58 Up A2-11-22-33-44-55 10 Gbps OpenVPN TAP6 TAP-Windows Adapter V9 12 Disconnected A3-11-22-33-44-55 1 Gbps OpenVPN WinTun Wintun Userspace Tunnel 11 Disconnected 100 Gbps WiFi Intel(R) Dual Band Wireless-AC 7260 5 Up B2-11-22-33-44-55 866.7 Mbps vShare Hyper-V Virtual Ethernet Adapter #4 2 Up A4-11-22-33-44-55 10 Gbps - Create virtual switch:
- External:
New-VMSwitch -Name vWiFi -NetAdapterName WiFi -AllowManagementOS $true - Internal:
New-VMSwitch -Name vShare -SwitchType Internal - Private:
New-VMSwitch -Mame vPrivate -SwitchType Private
- External:
If you need guest machines to have the same IP address using Hyper-V, then set up the guest machines to have bridged networking. That will work, and usually applies to VMware as well. This is for one network.
Consider the environment you are using.
One:
A desktop computer with a classic wired Ethernet adapter is ideal for Bridged Networking (External Switch) where the Guest gets the IP address from the Router the host machine is attached to.
You would do this if you need to access a Guest machine. Bridged Network (External Switch) is ideal for this.
Here is a complete post for accessing a Guest machine using a Bridged (External) Networking approach.
Accessing a Guest machine with Static IP
This will work on a Laptop in a fixed physical location where the Laptop has an Ethernet connection, a mainboard wireless card, and most USB wireless adapters installed into the machine.
You can never use a cellular USB wireless adapter for bridging.
Two:
Typical roaming laptops use NAT networking but then the IP address is not fixed (static). It can work, and I use it on one machine, just not guaranteed.
This is useful if you are moving a laptop around to multiple locations, say Home and Work.
The trick here is to make sure you check your Guest IP before accessing it.
To have bridged networking, you need to add that networking type to the Hyper-V Manager.
I have done this here using a Linux Guest machine (Kali) and it worked fine.
Open Hyper-V Manager and then Virtual Switch Manager and add an External (Bridged Network) Switch type. Give it a Name you will use in the Guest setup.
Once done, go to the Guest area in Hyper-V Manager, Settings and go to the Network Adapter. Add the External adapter from the prior step.
Restart any Guests you change.
24