5

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

11
  • The sole purpose of the 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]
    – JW0914
    Commented Jul 11, 2021 at 12:39
  • @JW0914 you are correct, I do have a specific need here, which is to establish an SSH connection between host and VM (using the same IP) and I want the VM to have access to the internet.
    – Albin
    Commented Jul 11, 2021 at 13:43
  • You can go about that two ways, depending on whether you want the traffic to go through the router [external switch only], or whether you want Hyper-V to manage all traffic locally on the host [Default switch and Internal Switch], with the latter being how I have mine setup.
    – JW0914
    Commented Jul 11, 2021 at 13:46
  • @JW0914 I tried the internal switch solution, unfortunately it doesn't work (see update to the question)
    – Albin
    Commented Jul 11, 2021 at 14:46
  • 1
    (Example) You'll have two adapters [switches] with two different IPs - the Default switch will have a dynamically allocated 172.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)
    – JW0914
    Commented Jul 11, 2021 at 16:21

2 Answers 2

3

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
      Example
      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


Virtual Switch Manager:

  1. Hyper-V ManagerActionsVirtual Switch Manager
  2. New virtual network switchCreate virtual switch → <type> → Create Virtual Switch
  3. Create a Name → Select Connection typeOK/Apply

PowerShell:

  1. WinKey+RpowershellCtrl+Shift+OK
  2. List existing network adapters and note the Name of 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
    
  3. 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
      
3

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.

enter image description here

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.

enter image description here

24
  • Sorry, I should have wrote that into my question as well: I tried to create an external switch while I was playing around with the settings but I can not bind it to the wired network card. The host is a Dell laptop with a docking station attached via USB C, the network adapter is integrated into the docking station. The only option I have is to bind it to the wireless card that is integrated directly on the motherboard. I'll try your solution anyway, but is there a way to bind it to the wired network adapter?
    – Albin
    Commented Jul 11, 2021 at 12:03
  • Even if the machine has wireless internet, you can still use it for External Network Switch. The adapter in the screenshots is wireless.
    – anon
    Commented Jul 11, 2021 at 12:20
  • Also if you wish to use the Wired adapter, you would need to make this adapter default. Bridged networking looks to the router the host is attached to in order to get an IP. This works, and I have done. NAT is better for roaming laptops, but then the IP is not static.
    – anon
    Commented Jul 11, 2021 at 12:34
  • @Albin AFAIK, the only way to pass through a USB network card if it doesn't show up in Switch Manager is via RDP (Show Options → Local Resources → Local devices and resources → More...), and even this may not work (I've never tried it)
    – JW0914
    Commented Jul 11, 2021 at 12:46
  • A cellular USB card will never work as bridged. A USB wireless card should work. A wireless card on the mainboard (same thing) will work. An Ethernet card will work. So the environment needs to be worked out. What I posted above is working here. My post says Add an External switch. Yes, you cannot modify the default.
    – anon
    Commented Jul 11, 2021 at 12:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .