Aug 3, 2009

Windows 7 Virtual Machines and Host Networking

Let’s assume you’ve got a Windows 7 Virtual Machine and you want to do networking between the Host and the Guest operating systems with it.

I’m going to give you a run through of how I set this up on my laptop using the loopback adapter.  There may be other ways, but this works quite well for my needs.

1. Install the Loopback Adapter

Go to device manager (in Control Panel) and select the Add Legacy Hardware option

image

In the ensuing wizard select: Install manually –> Network Adapters –> Microsoft Loopback Adapter

image

2. Setup the Loopback Adapter

Once the installation is complete we need to configure the loopback adapter.  My preference is to use fixed IP addresses for this so that I can use machine names to identify the VM easily, just make sure you pick an address range that isn’t used :-)

So go to the Networking and Sharing Center and select “Change Adapter Settings” on the left. You should see something like this:

image

Now go to the Loopback Adapters properties and then go to the properties of the IPv4 TCP/IP settings:

image

Simply set the IP address you wish to use for communicating with the VM.  I normally use a 10.x.x.x address range with the Class C subnet to minimise conflicts with other networks I may be connected to.

Not that I normally disable the IPv6 settings on the loopback adapter to make the configuration simpler.

3. Configure the Virtual Machine Networking

Assuming you already have a virtual machine configured you now need to go to your virtual machines settings and set up the networking as follows:

image

Set adapter 1 to be the loopback adapter and adapter 2 to be your physical network adapter so that I can access the internet and other networks from the VM should I need to.

4. Configure the VM’s Operating System

Now we need to start the VM and change some settings in the networking to use the fixed IP for the loopback adapter in much the same way as we did for the Host Operating System.

image

You may notice that in the network adapters it’s hard to tell which adapter is which since they are all the same type (i.e. the virtualised network).  The trick is to remember which network adapter is which in the VM settings – Adapter 1 is “Local Area Connection”, Adapter 2 is “Local Area Connection 2”, etc.

5. Don’t Forget Your Firewalls

So if we were to try pinging the Host OS from the VM we would probably find we don’t get a response, and the same would happen if we did it the other way around.  If that’s the case you probably forgot to adjust your firewall settings.

Here’s how it’s done in Windows 7 on the Host:

Go to Control Panel –> Windows Firewall then choose “Advanced Settings”

Select the Inbound Rules from the left side and then choose the New Rule action from the right.

image

Choose “Custom” in the dialog and then in the scope step add the IP address for the VM in the Remote settings area (as shown).  For extra security you should also ensure that the local address is set to the IP address of the loopback adapter on the host (10.2.1.1 in my case).

image

Allow all connections through on that rule and don’t forget to give it a meaningful name such as “Windows VM Loopback” or something like that.

You’ll also need to do that same in your guest operating system (assuming it also has a firewall enabled).

Once that’s done you should then be able to ping the Host from the VM as shown and do the same in the reverse direction.

image

6. One Last Step

Trying to use IP addresses can be somewhat of a pain, especially if something you’re accessing is wanting you to use host names instead.  The way to improve this situation is to edit the HOSTS file and add entries for the remote machines in both the Host OS and also in the Guest OS (the VM).

On Windows 7 start Notepad as Administrator since you need Administrator privileges to edit the HOSTS file.  The Hosts file can be found in C:\Windows\System32\drivers\etc.  Note that there is no extension on the Hosts file so you may have to change the filter in the Open File dialog to show all files before you’ll see it.

Once you have it open add an entry at the end of the file on a new line in the format of <ip address> <machine name>.  For example I use:
10.2.1.2    tfs2008-vm

Save the file and check that things are working by opening up a command prompt and pinging the virtual machine using the name you just gave it:

image

Don’t forget to do the same on the Guest OS in the Virtual Machine.

Once that’s done you should be all set to go and your Windows 7 Host and you Virtual Machine should now be able to talk to each other without a problem.

Enjoy!

2 comments:

  1. Thanks, this was really useful. Being new to this, I did struggle to work out where you access the Virtual Machine Settings from. If anyone else has this problem, you do Start/All Programs/Windows Virtual PC/Windows Virtual PC then right click on the "Windows XP Mode.vcmx" entry and choose "Settings"

    ReplyDelete
  2. Hmm. So you don't create a loopback adapter on the VM as well? I've seen other examples that do. What's the difference?

    ReplyDelete