Okay. There are actually a plethora of similar blog posts and guides on how to run a box in a segregated virtual machine using VirtualBox. However, this seemingly easy task gave me a bit of challenge the last time I did it because those posts and guides I found on Google had some “holes” that were easy to miss, and I had to pull my hair for a few hours before I eventually made everything work. So I decided to write this shit up myself.

I. Download VirtualBox and Ubuntu.

Easy. VB here and Ubuntu here. I used VirtualBox 4.3.8 and Ubuntu 12.04 64-bit. As of now (March 2014), that’s pretty much the de facto option for an Ubuntu server.

II. Create a new guest box.

This step is also pretty straight forward and it’s guided by the GUI wizard. The default ram size (512mb) and storage size (8gb) are usually fine, but I do encounter lack of space from time to time, and if you want to have a lot of room for large log files or asset cache or whatever, it’s safe to increase the storage size a bit. The actual physical size of the image file in the file system is dynamic (if you choose this option) meaning that the size of the image file will grow as you put more files in the box’s file system. Thus pre-defining this to be large does not penalize you in anyways either.

Screen Shot 2014-03-22 at 5.00.14 PM

III. Install Ubuntu server

You will now see the created box in the left most column of the VirtualBox GUI window. You will need to mount the ubuntu image to install it. Just open up its settings and go to the storage tab. It’s all there.

Screen Shot 2014-03-22 at 5.09.22 PM

Boot and install Ubuntu server. Until you see the prompt for the username, password, and time zone you will most likely just hit enter for all the prompts accepting the defaults. You will also see a prompt to install LVM. I don’t use LVM, but it might be helpful later. LVM is a logical volume manager (like gparted). Install GRUB (boot loader) or it won’t boot.

IV. Setting up ssh and host-guest connectivity

At this point, NAT should be automatically set up, so if your host is connected to the internet, your ubuntu box will have the internet connection as well. However, your host won’t be able to access the guest (and vice verse). NAT is really awesome for abstracting the external network adaptor that connects to the internet, but it does not provide a way for internal connectivity.

Some people suggest using port forwarding with NAT, but I don’t understand why anyone would do that when we have a better method – using a second host-only network adaptor for dedicated internal communication. It’s easier to setup, and you will be able to use standard ports (22 for ssh).

First install ssh server (and why not install build-essential while at it).

sudo apt-get update
sudo apt-get -y install build-essential openssh-server

Now shutdown the box, and set up the host-only adaptor for the box. This is actually where I had to pull my hair for a while… my host-only adaptor weren’t connecting to the host. The guest boxes could ping each other, but my host couldn’t ping the guest. I found out that you actually have to configure the host’s ip address to match the network address of the guest’s ip addresses. It’s quite obvious if you understand how these all work, but I just couldn’t thought of this because I didn’t know this configuration existed in VirtualBox GUI. For me, this was defaulted to 33.33.33.1… wtf…

Go to VirtualBox -> Preferences -> Network tab -> Host-only sub-tab. There, select the adaptor you will use (VirtualBox seems to have created on for me as default, and I’m just using this), then click on the driver icon. You can set it up like this if you want to use 192.162.56.x ip addresses. Also note that there’s a DHCP tab. You can disable this since we’re going to use static IP addresses.

Screen Shot 2014-03-22 at 6.49.04 PM

Now attach the host-only adaptor from the guest in Settings -> Network.

Screen Shot 2014-03-23 at 9.32.40 AM

Now we should set up this adaptor from the guest box. Boot it. Then edit the interface file.

sudo vim /etc/network/interfaces

At the end of the file, add these.

auto eth1
iface eth1 inet static
        address 192.168.56.101
        netmask 255.255.255.0

The last octet of the static address can be anything other that 1 and 255 and any existing ip addresses.

Now you should be able to ssh into your box from your host by typing something like this.

ssh user@192.168.56.101

V. Getting rid of having to type password

(Okay I just lost a good chunk of writing because of this weird wordpress bug… fuck this shit… starting over…)

This is my personal VM that isn’t exposed to the public, so for me, it doesn’t make sense having to keep typing password over and over. You can get rid of password for any sudo command by adding a little line to visudo.

sudo visudo

At the end of the file. Add these then save/exit

user ALL=(ALL) NOPASSWD: ALL

I would also add my host’s public key to guest so I don’t have to type password when I ssh.

From the host:

ssh-keygen -t rsa
scp ~/.ssh/id_rsa.pub user@192.168.56.101:
# Now cat this to authorized_keys in guest
ssh user@192.168.56.101
mkdir ~/.ssh
touch ~/.ssh/authorized_keys
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
rm id_rsa.pub
exit

You should be able to ssh into the guest without having to password now.

VI. Installing guest addition and configure shared folders

VBoxGuestAddition.iso comes with the VirtualBox binaries. On OS X, it’s located at /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAddition.iso. Just copy it to some other location because I found it impossible to mount it from this location. After copying, mount it in the same manner as how you mounted the ubuntu installation image.

Then from the guest, mount the image and run the installation script.

sudo su
mkdir /media/cdrom
mount /dev/cdrom /media/cdrom
/media/cdrom/VBoxLinuxAdditions.run 

It will take a few minutes to install. After the installation is complete, we can set up the shared folders from VirtualBox GUI.

Screen Shot 2014-03-23 at 10.41.37 AM

Create the folder where this volume will be mounted, and you can use this command to mount it there. Obviously, edit the folder names and path if they are different.

sudo su
mkdir /mnt/downloads
mount -t vboxsf Downloads /mnt/downloads

If you want this to be mounted permanently, you can just add this to /etc/rc.local.

sudo vim /etc/rc.local

At the end of the file but before exit 0 add this line.

mount -t vboxsf Downloads /mnt/downloads

Now it should be mounted automatically.

I guess that’s it. Your ultimate ubuntu server is ready.