4 Feb 2013

Hyper-V Virtual Switch 101: How to create and use Virtual Switches to connect Virtual Machines to Network

Understanding Virtual Switches

To connect a virtual machine (hereafter vm) to network it needs a network controller (hereafter NIC) the same way as any physical computer would need. In Hyper-V virtualization this NIC is of course virtual, it does not exists physically.

As we cannot connect a virtual NIC to a physical router we need a Virtual Switch (hereafter VS) to act like a network bridge between host NIC and vm virtual NIC. See the image below:



A Virtual Switch can be external, internal or private.
  • External Virtual Switch: will connect a vm to host NIC. If host NIC is connected to Internet through router, the vm is also connected to Internet. If host NIC (LAN) is not connected to router or if the router has no Internet access, the internal network will still work allowing computers (physical and vm) belonging to the same subnet and workgroup or domain to communicate with each other (file and media sharing etc.). This is not true when connecting external switch to host WLAN NIC, in which case the vm loses all connectivity when WLAN is disabled or not connected on host.
  • Internal Virtual Switch: When a virtual NIC is connected to Internal VS it can communicate with other computers and vms on the same subnet but has no Internet access.
  • Private Virtual Switch: When a virtual NIC is connected to Private VS it can communicate with other vms on same Hyper-V server but cannot communicate with host PC nor has it Internet access. A Private vs is often needed when setting up a Linux or pre-XP Windows vm.
Create a Virtual Switch

To create a VS open Hyper-V Virtual Switch Manager

Select New virtual network switch, select the switch type and click Create Virtual Switch:

Name the switch as you wish, select which of host NICs it will be connected to. Please check that the option Allow management operating system to share this network adapter is selected, otherwise you might lose all connectivity:

  • What this means, to put it very simple, is that when sharing is not enabled the virtual machines using a non-shared external switch can communicate with each other and outside world, but they will not share the connection with host. In this case you need to use another NIC and connection to connect host to the Internet
  • Notice that this is also a practical way to use two totally different Internet connections with two independent routers and subscriber lines thus getting two different public IPs. An external virtual switch in this case takes total control of NIC it is connected to leaving host without connection if it can not use another NIC
  • Also important to notice that to get two separate public IPs the external switch must not be shared, and host needs to be connected not only with another NIC but with another router and subscriber line. If both NICS belong to the same subnet and are using the same router / line they always share the public IP
Before clicking OK to save new switch you just created check that you have no active network connections used to anything important at the moment, for instance unfinished downloads or backups to a network location; when you click Apply or OK the network connection of your host is disconnected shortly before a new connection is created.

By default all virtual switches get the name vEthernet followed by the name you selected when you created it within parenthesis. Here's how system named for instance my external switch connected to LAN NIC on my host: vEthernet (AGMnet External LAN).

Screenshot below shows typical available network connections when all types of virtual switches have been created:



Notice that external virtual switch connected to host WLAN (vEthernet (AGMnet External WLAN) in this example) is not connected, showing red X. This is because of the WLAN NIC of host is disconnected (greyed out in screenshot); a virtual machine using this virtual switch would have no connectivity at the moment.

You might have also noticed that although the virtual switch vEthernet (AGMnet External WLAN) is connected to (is using) host's WLAN NIC it is however showing an Ethernet LAN symbol (connector of a LAN cable) and presented under LAN connections. This has a simple explanation: all Hyper-V network connections are Ethernet (LAN) connections; Hyper-V does not support WiFi so even when a virtual switch is connected to a wireless NIC, Hyper-V presents it as Ethernet connection to the virtual machine i.e. the virtual NIC on vm "thinks" it is connected with an Ethernet cable.

This also means that when we are using an external virtual switch connected to a WLAN NIC, we need an extra network bridge on the host. The virtual NIC connects to a virtual switch, virtual switch in its turn connects to a host network bridge which creates (as its name says) a bridge between our virtual Ethernet connection and host WLAN NIC's connection to the router:



A "real life" situation from my computer. Virtual machine sees the connection as an Ethernet connection, connecting its Virtual NIC to External Virtual Switch, which in its turn is connected to a Network Bridge on host and further to WLAN NIC on host. Sounds complicated, is simple:



Don't worry be happy, Hyper-V and your Windows 8 host are together taking care of creating bridges when needed. See the screenshot below, I have just enabled and connected my host's WiFi NIC. When enabled it noticed that there's a Hyper-V external virtual switch which wants to use it so it created a Network Bridge, which then told external virtual WLAN switch.

Red X disappeared, a vm using this connection would be back in business:



Connect a Virtual Machine to a Virtual Switch (vm with Windows XP SP2 or later OS )

When creating a new vm select the correct virtual switch for your purposes. To connect vm to other computers and to Internet select an external switch which is connected to a host NIC currently enabled, connected, and online:

You can always change the virtual switch from vm Settings:

You are now ready to start networking.

Connect a Virtual Machine to a Virtual Switch (vm with pre Windows XP or Linux / Unix OS )

As you already know the expression "Not supported" does not always mean it wont work. Several Linux distros as well as early versions of Windows run very nicely on Hyper-V although officially it only supports XP and later Windows versions. Read for instance this tutorial to see how you can install Open SUSE or Ubuntu Linux on a Hyper-V vm: Hyper-V – Create Linux Virtual Machine in Windows 8.

When creating a new vm for Linux or older Windows versions it is important to follow these steps in setting up network functionality:

1.Select Not connected when creating vm:


2.Before booting vm first time go to Settings and delete (remove) default network driver:


3.Create a new virtual Legacy Network Adapter:


4.Create a Private Virtual Switch, attach it to newly created Legacy Network Adapter:


5. Boot and install OS
6. When on desktop, shut down vm before starting any other apps or programs or changing vm settings
7. When vm is shut down, go to virtual machine's Settings and connect the Legacy Network Adapter to an External Virtual Switch with network connectivity
8. Boot the vm and you are ready to go online

After you have created an external virtual switch, your Windows host computer starts using it, too. No reason to change this, as long as your external virtual switch is online everything will be OK.



Remember, Hyper-V and windows are taking care of the bridges when needed. Host Network Bridge does exist only when host WLAN is enabled, and is appearing offline when WLAN is not connected:



If a vm cannot connect to network, check first that it is using a correct external virtual switch and that the said switch is connected to a NIC which is enabled and connected:





No comments:

Post a Comment