Running a virtual server using virtual box, nat and a wifi network connection
I used to install apache, mysql, etc on my laptop for local testing. However having all these extra services caused my computer to start slowly. I disabled them from starting automatically but that became tedious because I needed to remember to start them (generally 3 or so) before I used them. Here’s a better idea - use Virtual Box (recently renamed to Sun xVM) to run an instance of Ubuntu Server (or whatever) locally. When you’re ready to do some dev work, fire up the vm (it boots in about 30s).
The problem is, if you’re on a laptop then you probably use wifi, and that means you’re probably going to need to use a NAT connection for your Ubuntu Server to have network access. Unfortunately, with a NAT connection no one, not even your laptop, can see the guest services running on the Ubuntu Server. Fortunately the Virtual Box developers provided a solution, if you know where to look.
Using a command line tool you can configure port forwarding. The documentation explains this quite well if you know to look there. :-) (I asked in IRC and was politely directed to the fine manual page) It’s in chapter 6.4.1 titled, “Configuring port forwarding.” You should read it, but in case you don’t want to, here’s what you do.
- Know the name of your VM. Mine is called “Ubuntu Server”
- Shut down the VM - it cannot be running for this to work (according to the manual)
- Know the ports of the services you want to forward, for example 80 and 443 for web, 22 for ssh and 3303 for MySQL
- Open a terminal and run these commands (I’m assuming you’re running Linux as your host and using one of the “PCnet” emulated network adapters - replace pcnet with e1000 if you’re using an “Intel” emulated network adapter) - you run these on your host pc, not the guest - remember you shut the guest down in step2:
VBoxManage setextradata “Ubuntu Server” “VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesthttp/Protocol” TCP
VBoxManage setextradata “Ubuntu Server” “VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesthttp/GuestPort” 80
VBoxManage setextradata “Ubuntu Server” “VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesthttp/HostPort” 2080
These three commands set up one rule that forwards incoming TCP traffic on port 2080 to the http port 80 on the virtual machine called “Ubuntu Server.” So above, change the text “Ubuntu Server” to be your virtual machine’s name in ”all three lines”. Change TCP in the first line to UDP if necessary (ICMP won’t work). In the second line change 80 to something else if you’re not working with the http protocol. For example, maybe you want a mailserver in which case you’d want port 25. And in the third line, change 2080 to the port you want to use to access this service. I used port 2080 which means to view the site I’d have to go to http://localhost:2080/. (I have this thing against running development servers on standard ports on my PC)
One special note above, each port I’m forwarding (called a “rule”) has to have a unique name. In the example above the rule is called “guesthttp.” All three lines that set the rule use that same phrase. If I were going to forward another port, say 443 for https, then I’d use a different name for the rule, maybe “guesthttps.” Make sure that all three lines that make up the rule use the same name though.
If you want to delete a rule just repeat those three lines with the last option blank. Using my example above, exclude TCP, 80 and 2080 from the commands.
I believe you can run multiple servers and forward different ports. Just make sure that you use different ports on the host. So for example, the first guest may use 2080 for http, the second guest you may want to use 3080, and the next 4080. That way you can run three different web servers at the same time, each in their own guest.
Good luck, I hope this helps.
Edit: Thanks to Nathan Stein for pointing out that the rules need to be modified if you’re using an emulated Intel PRO/1000 network card. See the note above for details.
Bearfruit
Comments
running in headless mode
P.S. Here’s a nice tip for running your VM in headless mode so you don’t see it’s window.
Post new comment