I'm pretty sure I've done this before
Skip the story
Hosting Ubuntu in a VM makes it incredibly easy to setup a Linux box that you can use to host a Git server, WordPress blog, your Rails apps, Node.js, Apache, mail server, MySQL RDBMS, TeamCity, etc... Really, there's no reason not to setup an Ubuntu VM.
This isn't the first time I've attempted blogging, nor is this the first time I've put together a post about using VirtualBox to host an Ubuntu VM. Each time I write this post I discover that the process has gotten easier and eaiser... mostly.
You no longer need to use "VBoxManage setextradata" to setup port forwarding, Ubuntu has gotten easier and easier to use, and most major software package have some sort of setup / installation package you can download and easily install.
However, I continue to write this post every few years because I continue to have a problems trying to install the VirtualBox Guest Additions...
Getting a basic installation of Ubuntu setup is incredibly straight forward. You pretty much need just two things:
- The latest stable distribution of Ubuntu for desktops (if you're reading this you should probably avoid Ubuntu Server)
Helpful hint: if you have an old install of VirtualBox laying around from the last time you messed around with virtual machines, delete it and install the newest release. This will ensure you have the newest version of the Guest Additions.
Install the Guest Additions
I'm going to assume you can figure out how to install VirtualBox and get a basic instance of Ubuntu up and running. For the most part the options are pretty straight forward. Give up as much memory as you can and allocate a virtual disk drive that matches your intended use for the VM.
Make sure that you go into the VM's settings (in VirtualBox) and enable 3D acceleration. With that taken care of, open a terminal and execute the following:
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential module-assistant
$ sudo m-a prepare
$ sudo apt-get install xserver-xorg xserver-xorg-core
With that taken care of, use the VirtualBox menu to mount the Guest Additions CD in the VM. Ubuntu should automatically prompt you to begin installation. If it does not, you can either eject / unmount the CD or run the following command:
sudo sh /media/cdrom/VBoxLinuxAdditions.run
Once the install completes you should reboot the virtual machine. Hopefully when Ubuntu finishes rebooting you'll be all set.
As nifty as it is to have Ubuntu running in a VM on your Windows box, it doesn't do you a lot of good unless you can get to it from the outside world. Regardless of the port you want to open up, you basically have two ways to accomplish your goal:
- Configure your VM to use a Bridged Adapter
- Setup port forwarding
With bridged networking your VM essentially appears to be on your network. There is no need to setup port forwarding. You can simply configure network router to forward traffic to the IP address of the VM. Unless you have a compelling reason not to, I'd recommend using this configuration if you want to open something running on your VM to the outside world.
With port forwarding your host OS has the opportunity to buffer / forward packets as it sees fit. I'll let you explore why you would use port forwarding over a bridged adapter on your own. Here's how you do it:
- Open the settings for your VM in Virtual Box
- Go to the Network tab
- Ensure Attached to is set to NAT
- Click on Port Forwarding
- In the dialog that appears enter whatever Host and Guest port are appropriate for your use case. You should leave the two IP columns blank.
With that you should be set to access your VM from anywhere in the world. Please keep in mind that this probably isn't the most secure thing to do. I wouldn't really recommend leaving any port open to the world unless you really know what you're doing.
Random useful links: