Like many other developers, I prefer to do all of my development within a virtual machine so I can mimic my production server as closely as possible. I recently made the switch from Windows to Mac, and as such no longer have VMware Workstation to use for virtualization. I decided to use the free and open source Oracle VM VirtualBox and installed the latest version of Ubuntu Server on it.
Unlike VMware Workstation for Windows, setting up a host-only adapter is not easy to do. A host-only adapter is a virtual network interface which allows the Host (the physical machine) connect directly with the Guest (Virtual Machine). This also allows you to access the VM while offline (a bridged or NAT adapter won’t allow this). After doing a lot of searching around the net, I wasn’t able to easily find a guide of how to do this. Below are the steps that need to be taken to set this up properly.
Step 1: Configure Host-Working
This is to configure host-only networking on OS X which can be accessed through VirtualBox > Preferences. Click on the Network tab.
There may or may not be a “vboxnet0” network there. If there isn’t click the “+” button and one will be added for you. Click the screwdriver icon and go the the DHCP tab and uncheck the enable box. There is no need for DHCP with a host-only adapter.
To confirm this works go to mac’s terminal and type:
$ ifconfig vboxnet0
vboxnet0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
inet 192.168.56.1 netmask 0xffffff00 broadcast 192.168.56.255
If you get a response similar to this, you’re up and running and it is time to set up the host only adapter on your VM.
Configure Host-Only Network Adapter:
Configuring a host-only adapter on the virtual machine is a pretty easy thing to do. Unfortunately, most of the guides I found were misleading or incomplete. You now need to go into your virtual machine and open /etc/network/interfaces in your favorite editor (I prefer vim).
[bash]$ sudo vim /etc/network/interfaces[/bash]
You now need to assign the second adapter a static IP address. It should look similar to what I did below:
iface eth1 inet static
address 192.168.56.2 # This number needs to be larger than the inet address on your Mac (when we did ifconfig vboxnet0)
Save the file and restart networking:
[bash]$ sudo /etc/init.d/networking restart[/bash]
Et voilà! You will now be able to SSH into your Linux guest through Mac’s native terminal.