VirtualBox Port Forwarding with Linux Host

The first of many ways to handle VirtualBox's virtualization is to Port Forward. Make sure you have the latest of VirtualBox. This allows a person to connect to the virtual machine directly through SSH or view the web server through a browser. Basically, we have the host machine (your actual computer's Operating System) and your guest machine (your virtual machine). So far, this is only for GNU/Linux host how to. Later the windows host how to will be added. Here is the Microsoft Windows how to.

In your GNU/Linux host's terminal application, run the following commands (Warning: make sure the quotes are proper quotes due to this CMS' protection from quotes Update: this quotation issue has been fixed the quotes you see should work):

VBoxManage setextradata "name of vm" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 2222

VBoxManage setextradata "name of vm" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" 22

VBoxManage setextradata "name of vm" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" TCP

Before running those commands, replace the following with your personal settings:

  • Replace "name of vm" with your virtual machine
  • Replace ssh with the preferred name you want. This is _required_ to be unique among the three commands.
  • Replace 2222, 22, TCP respectively with your desired host port, guest port, and protocol.

I use port 2222 for the host in this example so it will not conflict on most configurations. This can be replaced with any port setup you desire:

  • Apache (port 80)
  • MYSQL (port 3306 [Update: requires both TCP and UDP])
  • Any many more possibilities

One important note is to make sure you restart all VirtualBox applications (ie. the virtual machine, VirtualBox GUI, all of it). This allows the settings to be used properly.

Once this is done and your virtual machine is restarted, run these commands to verify:

VBoxManage getextradata "name of vm" enumerate

And/or

ssh -l user_name -p 2222 localhost

If you want to clear the virtual machine settings, run this ( Only use this command if you want to clear the settings for this! ):

VBoxManage setextradata "name of vm" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort"

VBoxManage setextradata "name of vm" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort"

VBoxManage setextradata "name of vm" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol"

Enjoy


4 Responses to “VirtualBox Port Forwarding with Linux Host”

Leave a Reply

You must be logged in to post a comment.