Wednesday, 4 July 2012

How-to: VirtualBox Networking Part Part Four - Accessing my Virtual Server

How-to: VirtualBox Networking Part Part Four - Accessing my Virtual Server Fiberoptics Guides, Library of University of Alberta Canada, International Business school.jpgThis all started when I needed to run a sandboxed instance of Wordpress and thought a virtual Ubuntu 11.10 server would the simple answer under VirtualBox.

Deciding how to network my virtual server so that I could practically use it, I'm going to run two network adapters on my virtual server to give me the access I need; a standard NAT adapter and a Host-only adapter.


My host is Ubuntu 11.10 Desktop and my guest is 11.10 Server, which keeps things within the Linux way of life.

Guest Access to the Internet
First, I need my guest machine to be able to access the Internet for installing updates, downloading packages and the like. I need to access it from my host machine, but I don’t want the server to be accessible from anywhere else on the network.

Selecting my guest machine from the VirtualBox Manager window, I then select Settings > Network.

I begin by keeping the default networking NAT adapter. This allows the guest machine to access the Internet through my host’s connection. The guest isn't visible to anyone else on my network; I cannot access any guest resources from my host machine either, nor can any guest machines access each other.

VirtualBox vboxnet0 set-upConfigure Host-only Adapter in VirtualBox Manager
The easy method is to use the default Host-only adapter provided by VirtualBox, this is the one named vboxnet0, but you can add more if you want several separate Host-only networks. If you go to the VirtualBox Manager main window and select File > Preferences > Network, you can configure vboxnet0 or add new adapters.

Click on the “Edit” button for your Host-only network, that's the screwdriver icon titled "Edit host only network" and you'll find the default settings. The IP address 192.168.56.1 is the address at which the guests can access the host. I'll leave this as-is.

By default, vboxnet0 has a DHCP server set up to dynamically allocate IP addresses on a per session basis. I want to my Wordpress virtual server to keep static IP addresses, so on the DCHP tab, I uncheck the “Enable Server” box for DHCP

VirtualBox Add Host-Only Network Adapter Add Host-only Adapter to Guest I'll add another network adapter to my guest machine, a Host-Only Adapter, that enables a self-contained, private, virtual network consisting solely of my host and any guests using host-only. Any of the member machines can access each other, but nothing outside it can get in.

 So on my Adapter 2 tab for my guest, I set the Attached to option to Host-only, and the name is the default vboxnet0. Promiscuous mode doesn't matter, but I do need the Cable Connected checkbox enabled.

 Configure the Guest I need my guest's virtual server to have a static IP address on the host-only network, otherwise the address changes for every session and I have to work out what it is before I can access it from the host. Logging into the guest, I open a terminal and issue the following command:
# ifconfig eth1 192.168.56.101 netmask 255.255.255.0 up

 This sets the IP address on my host-only adapter for this guest and brings up the network interface. It uses the 192 network prefix for domestic networks, followed by .168 for the start of the host ID, .56 as the host-only sub-net range, and I could use use any final identifier above .1 (my host) on my host-only network. The NAT adapter is sitting on eth0, so logically the Host-only adapter is bound to eth1. I can now secure shell (SSH) or browse to the guest from the host using this IP address as a test.

 Howver, this is just temporary; if I reboot, this configuration will disappear. To make it permanent, I need to add it to the /etc/network/interfaces file (as root) in the form:

 # The host-only network interface
auto eth1
iface eth1 inet static
address 192.168.56.101
netmask 255.255.255.0
network 192.168.56.0
broadcast 192.168.56.255


Once I reboot, this interface will be raised automatically. I can check that it's present using the ifconfig command in a terminal on the guest.

Naming of parts
Since I'm not so good with numbers and IP addresses, I'd rather use the guest machine name over the IP address. To do this I can add an alias to the /etc/hosts file on my Host machine, so that I can browse by name to my running guest.

I edit the file (as root) adding the line:
192.168.56.101 ocelotsvr

If I add more guests to the host-only network, I can do the same so that I can address any of them from any of them by name.

Any Port in a Storm? The networking experts out there will have spotted an alternate route to accessing my virtual server.

Using my default NAT adapter, I could run some extra configuration there to let me access my virtual server from the host - without using Host-only networking.

VirtualBox NAT set up with  port forwardingI can set up port forwarding in VirtualBox, using the guest's Network Adapter settings panel. Going to the NAT adapter, the big button at the bottom is for Port forwarding. This opens a further configuration dialog box so I can set the port forwarding rules for this network adapter for this guest only.

I need two rules; one to access the Apache web-server on my guest, the other to handle all the other TCP traffic (http requests, mostly).

Keeping it simple, I'll name them Apache and TCP; both use the TCP protocol. If I stick to the common port numbers, 8888 on the host forwarding to port 80 on the guest for Apache; then 2222 on the host forwarding to port 22 on the guest; this gives me specific access on the guest for those service requests. Anything else will be rejected by VirtualBox as there's no viable route.

This also means any other guests I wish to run can't access this virtual server, as no network route exists under NAT. RC

Image credit: Fiberoptics Guides, Creative Commons from Library of University of Alberta Canada, International Business school