Virtualbox: Forwarding ports to your Virtual Machine

VirtualBox is a great program to use to test out different operating systems. However there may come a time where you may want to use the guest OS as a server. In that case, there are two ways to setup the virtual machine. You can either use a bridged connection so the guest OS can have its own IP address on your network or you can forward the ports on your computer to the guest OS. The latter comes in handy if you do not have any control over what computers are connected in the network.

Let’s say for instance you want to run a web server on the guest OS. The port you usually would use for that is port 80. So we now know we want to forward port 80 of the host OS to the guest OS so any request on that port will go to the guest OS instead. This means that the Host OS will not be able to use that port, so it’s good to keep that in mind.

To do this you have to run the VboxManage command line tool in the VirtualBox directory. If you are running windows then the directory is most likely C:\Program Files\Oracle\VirtualBox.

Here is an example of how to use this command (make sure the guest OS is not running):
VBoxManage modifyvm “VM name” –natpf1 “servicename,tcp,,hostPortNumber,,GuestPortNumber”

Replace VM name with the name of the virtual machine you want to forward your ports to. Change servicename to what kind of service you plan on running. In this case we are running a web server. You user “http,” “web server,” or whatever name you feel is most descriptive. For tcp, replace it with the protocol used (TCP or UDP). For hostPortNumber type the port that you want to forward to the guest. Finally GuestPortNumber must be replaced with the port that the guest is actually using for the service.

This is what I’m going to on the command line to forward port 80 on the host to port 80 on the guest:
Vboxmanage modifyvm “Windows Xp” –natpf1 “http,tcp,,80,,80”

To remove the forwarding you can run this
VBoxManage modifyvm “VM name” –natpf1 delete “servicename”

For my service I typed
Vboxmanage modifyvm “Windows Xp” –natpf1 delete “http”

Under windows, the next time you run the guest OS you might be asked by the windows firewall if you want to give VirtualBox certain permissions. You want to allow this access or else the windows firewall will block the VM’s access to the port you just specified.

If the guest OS also has a firewall on you will want to open up the port you just forwarded. In this case I had to open up port 80 on the Guest OS firewall.

Now that everything is setup, anytime a computer on my network opens up a web browser and types in my host OS ip address they will be forwarded to my guest OS that has a web server running.

This entry was posted in Software. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s