about me about me
photography photography
software projects software projects
contact me contact me

This month I’ve got a couple of trips to the US, one work, one leisure. I’d like to be able to use some of the dead time during the flight to do some development work. Instead of running PHP, Apache and MySQL from my Mac, I run it from a Ubuntu virtual machine via VirtualBox.

My previous configuration was to use a bridged network so the host could see the guest and access services running on it. Perfect … except when you don’t have a network connection, like when you’re on a flight. Initially I struggled with OS X setting the wired interface inactive when a cable is not connected. I asked around and found I needed to change the adapter type from bridged to host only.

Host only networking creates a new virtual adapter on the host machine using the loopback interface, VirtualBox names this vboxnet0. This adapter enables the host to see services on the guest but with no requirement for a physical NIC to be present, so it’ll work offline too.

Setting up Host Only networking

Host only adapters cannot provide internet access, therefore we’ll need two devices on the guest for when we’re online. The first device should be configured as NAT (the default), the second is the host only adapter.

VirtualBox network adapter settings

Once you’ve added those adapters, go to VirtualBox » Preferences » Network and specify an IP (if the default doesn’t suit your network, otherwise go with the default). This is the IP you’ll use in Ubuntu (the guest) to connect to OS X. If you have mounted directories on the host, this is the IP address to include in your /etc/fstab on the guest.

Now we can boot our VM. Once the VM starts, if you run ifconfig in Terminal, you should see a virtual adapter has been added to OS X:

$ ifconfig vboxnet0
vboxnet0: flags=8843 mtu 1500
	ether 0a:00:27:00:00:00 
	inet 192.168.56.1 netmask 0xffffff00 broadcast 192.168.56.255

When Ubuntu has booted, configure the network interfaces:

$ sudo vim /etc/network/interfaces

If you chose to enable VirtualBox’s built in DHCP server, you can leave the NAT interface eth0 as dynamically assigned:

auto eth0
iface eth0 inet dhcp

Assign the second host only adapter with a static IP address, this address must be on the same subnet as the vboxnet0 adapter on the host. For example, if our IP was 192.168.56.1, the guest must use an IP within the range 192.168.56.2 – 255.

auto eth1
iface eth1 inet static
    address 192.168.56.5
    netmask 255.255.255.0
    network 192.168.56.0
    broadcast 192.168.56.255

When you’re done, restart networking on the guest:

$ sudo /etc/init.d/networking restart

You should be able to SSH to Ubuntu from OS X now:

$ ssh 192.168.56.5
Linux 2.6.32-21-generic-pae #32-Ubuntu SMP Fri Apr 16 09:39:35 UTC 2010 i686 GNU/Linux
Ubuntu 10.04 LTS

Welcome to Ubuntu!
....
$

comments

Ben // 06.Nov.2011 // 06:40

Thanks for this. I’m running in Win7 but this got me up and running just fine. I appreciate it!

Larry Zoumas // 25.Oct.2012 // 14:37

This is the best tutorial I have seen to configure a virtual machine for development purposes on Mac. I thank you for that and owe you a beer.

Dave C // 16.Mar.2013 // 12:55

I came to this page because I want to be able to take my Mac off-grid and work on a webserver app on Ubuntu guest and run the browser on my Mac.

So far so good, the host-only network works nicely. My only issue is that after doing the setup, my shared folder stopped working. I’m trying to sort it out now, will report back here if/when I figure it out.

Manny // 17.Apr.2013 // 19:51

Hi, thanks for an easy to follow tutorial.

I am having some trouble with my setup though. My guest is not seeing my host and vice versa. Pinging my host returns “Destination host unreachable”.

I followed all your instructions. I left my VBs built-in dhcp server enabled, but changed the ip range to 192.168.0.0.

My ubuntu guest servers interfaces file looks like this:

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp

auto eth1
iface eth1 inet static
address 192.168.0.131
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255

When I run ifconfig on my guest, my eth0 returns an ip range of 10.0.2.*. My eth1 is set correctly to 192.168.0.131. I am not sure if this is what’s affecting communication between my guest and host. I am also not sure what I need to configure for my host which is a MAC.

Any help would be much appreciated.

David Hall // 05.Sep.2013 // 12:13

Thanks for your blog post! I have been trying to figure out how to use virtual hosts offline because I always used Bridged-Adaptor which relied on DHCP of my wifi network. This is great.

To make it simpler, I found you can have VirtualBox run a dhcp server in the virtual network. In virtualbox main menue, select preferences (Cmd-,) then Network. There you will see “Host Only” window. If nothing is there, click little icon on right to create. Select the little screw driver icon and you can then turn on dhcp server.
That is of course far simpler than the manual configuration.

kolybasov // 31.Jan.2014 // 11:05

Thank you for a great tutorial!