If you’re like me sometimes I just find it easier to use a Virtual Machine for doing development work, especially when it is a complex system with many moving parts. Recently I started to work on a project that I had inherited from another developer. The project was partially setup on a remote development server. I wanted to under stand how the pieces went together and be able to pass it along to other co-workers, so I decided to build a VM.
I have been a heavy VMware Fusion user for many years yet not everyone in my office has the luxury of a license for it. I decided to give Virtualbox a go for this project.
At first I was really happy with it, I got my machine up and running in no time and was working away until I came to setting up the server and realized for the sake of sane hostfile management I wanted it to have a static ip. I decided to switch the network adapter over to NAT which is where the pain began.
I enjoy just shelling into my machines and working that way, once I switched to NAT I could not just shell to the ip address of the vm. This seemed odd to me, VMware creates a virtual network for you which apparently Virtualbox does not. So I went diving and here is what I found:
You’ll need to enable port forwarding to the vm’s NIC. To start open up the network configuration section of your vm.
After opening the network tab, click on the “Port Forwarding” button under the “Advanced” section.
Then fill out the sections with the relative information. I found that if you try and put in a Host IP the setup doesn’t work quite right. Here I’m forwarding my localhost port 2222 to port 22 on the guest vm along with port 8080 to port 80.
Now from my host machine I can use the following shell command to ssh in $ ssh aip@localhost -p 2222 which will use the port-forwarding and let me access my machine.
The same logic applies to trying to view the site running on the vm, in my browser I just have to access http://localhost:8080.
Now this is a bit of work but it is the trade off for a free tool for virtualizing your environments. It’s working for me right now but your mileage may very.