In this post, I use the following definitions
- Host: The laptop/desktop/system that hosts one or many VMs (Guests). This is typically the main system where the Virtual Machine software itself is installed that allows a VM to access and share the Host infrastructure.
- Guest/VM: The Guest or Virtual Machine that "runs on" the Host and which feeds off the Host computing infrastructure (CPU, RAM, HDD etc.).
Once I get the everything installed and humming in the VM, it's sometimes inconvenient to keep switching between the Host and various Guests when I need to access applications or get other things done.
VirtualBox does provide an option to make things easier using a feature called "Seamless Mode". However, I primarily use my VMs to host server applications, most of which can be accessed, administered and configured using a web browser and development tools like SQLDeveloper, Eclipse etc.
Wouldn't it be nice to stay within the confines of the Host - including using the Host web browser, development tools and utilities without switching to the Guest?
A good way to do this is to configure the network in a way that it allows the Host and Guests to communicate seamlessly.
So how do we do this? In my example, the Host is a Windows 7 (Enterprise Edition, SP1) OS which has VirtualBox 4.2.18 installed and I have the following setup on a Guest/VM:
- Windows 2008 Server (R2 SP1)
- Oracle Database installed and configured on port 1521
- Oracle Enterprise Manager (accessible via a web browser) configured on port 1158
First, gather the following information
IP address of the Guest/VM: Access your Guest/VM OS command line and execute ipconfig.
Find a section similar to:
C:\Users\GuestUser>ipconfig
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . : abc.def.ghi.jkl
Link-local IPv6 Address . . . . . : fe30::f373:46d7:d443:915711
IPv4 Address. . . . . . . . . . . : 10.0.2.15 This is the address you are looking for
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 10.0.2.2
Now, find the IP address of the Guest as seen by the Host: Access your Host OS command line and execute ipconfig. You will see information about all your network adapters, but you want to specifically find the VirtualBox adapter:
C:\Users\HostUser>ipconfig
Ethernet adapter VirtualBox Host-Only Network: This is the adapter you are looking for
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::da97:4432:gf05:785a%28
Autoconfiguration IPv4 Address. . : 169.254.110.92 This is the IP you are looking for
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :
So now, we've got to make sure when we access the IP that the Host can "see", it should tell the Guest/VM that you want to access services that it hosts. To do this, we need to configure "Port Forwarding" in VirtualBox.
To configure this in VirtualBox, launch Settings > Network > Port Forwarding
Port Forwarding simply bridges the gap between the two IP addresses above. It receives the request on the address the Host can "see" (169.*), and patches it through the Guest IP address (10.*) and vice versa. I've used the port of 1158 since that's the port Oracle Enterprise Manager listens on within the Guest/VM. You can use 80 for example if you want to access a website hosted on your Guest/VM or 1521 if you want to access an Oracle Database etc.
Now, if you access this IP:Port using a web browser on your Host, you should be able to access the application hosted on your Guest/VM and you do not need to switch to your Guest/VM each time.
This is what you would see you your Host now:
If this doesn't work, it may be that you have an active firewall setup on your Guest/VM that is rejecting inbound requests. Remember - your Guest/VM is a full OS that behaves as if your Host is just another computer trying to access it and so if it has an active firewall, will protect itself against the Host.
To get through this, access your Guest/VM firewall
Now, create a firewall "allow" rule for this port (I called this rule "Oracle Database Enterprise Manager"). The rule should simply allow inbound connections requesting access. (You can start with allowing all IPs, but a recommended practice is to restrict it to the IP of the Host)
That's it!
I don't like accessing applications via IP that much (too many numbers to remember!). So, on my system I also configure a "forced DNS" on my Host. You don't need to do this if you are ok accessing via IP.
On your Host, Access your hosts file located at C:\Windows\System32\drivers\etc
Add something like this line and save the file:
169.254.110.92 guest.virtualmachine.com
What you've essentially done now is this:
- Host browser tries to access guest.virtualmachine.com
- The hosts file resolves this name to 169.254.110.92
- The request goes to the Virtual Host Adapter on 169.254.110.92
- VirtualBox looks at its "Port Forwarding" setting and patches the request to 10.0.2.15
- The Guest/VM firewall looks at the inbound connection and allows it through
- Your Host browser is now connected to application on Guest/VM