Accessing LAN devices from a Hyper-V host while running Ubuntu virtual machines requires careful network adapter configuration. By properly configuring the external virtual switch and ensuring routing rules are in place, you can enable seamless communication between your Linux web service, the Windows host, and other devices on your local network.
Understanding Network Adapter Types
Hyper-V provides several types of virtual network adapters that determine how your virtual machine connects to the physical network. The most common options are External, Internal, and Private switches. For accessing LAN devices, the External switch is typically the required choice because it binds directly to a physical network interface card (NIC) on the host machine. This allows the virtual machine to appear as a distinct device on the same subnet as the host. When you create an External switch, Hyper-V creates a virtual switch that bridges the traffic between the VM and the physical NIC. It is crucial to select the correct physical adapter, especially if your host has multiple network interfaces, such as Wi-Fi and Ethernet. If you choose the wrong adapter, your VM may not receive an IP address from your router, or it may be isolated from the rest of the LAN. Always verify that the physical adapter you select is connected to the network segment containing the devices you wish to access.Configuring the Virtual Switch
To set up the connection, open Hyper-V Manager and navigate to Virtual Switch Manager. Select New virtual network switch and choose External. In the subsequent wizard, select the physical network adapter that provides internet access or LAN connectivity. You will see an option to allow management operating system to share this network adapter. Checking this box is essential for the host to retain its own network connectivity while the VM uses the adapter. Without this setting, the host may lose its network connection entirely when the VM starts. Once the switch is created, assign it to your Ubuntu VM. Go to the VM settings, locate the Network Adapter section, and change the connection type from Default Switch to the newly created External switch. Restart the VM to apply the changes. Upon booting, Ubuntu should automatically request an IP address via DHCP from your local router. You can verify this by runningip addr show in the terminal. Look for an IP address in the same range as your host computer, typically something like 192.168.1.x or 10.0.0.x.
NIC Change VM (Ubuntu Hyper-V) Default Switch - External Switch Lost Internet Connection
This step often resolves issues where VMs lose connectivity after switching from the default NAT-based switch. The default switch isolates the VM from the LAN, making direct access to other devices impossible without complex port forwarding or proxy configurations.
Firewall and Service Configuration
Even with the correct network switch, firewalls can block access to services running on the Ubuntu VM. Ubuntu uses UFW (Uncomplicated Firewall) by default. If your web service is listening on a specific port, ensure that UFW allows inbound traffic on that port. You can check the status of the firewall usingsudo ufw status. If the status is active, you must add a rule to allow incoming connections, such as sudo ufw allow 80/tcp for HTTP traffic.
On the Windows host side, the Windows Defender Firewall might also interfere, although it rarely blocks outbound connections initiated by the host to the VM. However, if you are trying to access the VM from another device on the LAN, that device's firewall must also permit the connection. Ensure that your web service is bound to the correct interface. Binding to 0.0.0.0 ensures the service listens on all available network interfaces, including the one connected to the virtual switch. Binding to 127.0.0.1 would restrict access to localhost only, preventing remote LAN access.
Testing Connectivity
After configuring the switch and firewalls, test the connectivity from the Windows host. Open a command prompt and useping followed by the VM's IP address. A successful ping confirms basic network layer connectivity. Next, try accessing the web service using curl http://<VM_IP>:<port>. If the curl command returns the expected response, your configuration is successful. If it times out, re-check the UFW rules and ensure the service is actually running and listening on the correct port using ss -tlnp.
Routing Windows Traffic through Linux using Socks Proxy
While a SOCKS proxy is useful for routing general traffic, it is not necessary for simple LAN access to a web service. Direct IP access is more efficient and simpler to debug. However, if you need to route specific application traffic through the Linux VM, consider using SSH tunneling instead of a full proxy setup.