Seth Keiper

VirtualBox Port Forwarding with Linux Host

by on Jan.05, 2008, under VirtualBox, Virtualization

Make sure to read the VirtualBox User Manual for the latest methods as this post is 99% outdated.

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


2 Comments for this entry

  • JonathonP

    Is this post relevant to VirtualBox 4.0.4? I am using Fedora 14 as the host and Windows XP in VirtualBox. I have added two ports (TCP and UDP) to the firewall settings in 'other ports' in fedora? Do i need to do this?

    I noticed the port forwarding option in the firewall setup which asks for either the ports to be forwarded to either local or an ip address. Do i need to do anything here?

    In the settings under VirtualBox under Network adapter 1 (which is enabled) and attached to NAT, there is a port forwarding option in the advanced setings. Can the host and the guest port be the same be the same number? If i use 'open port checker' to check if the tcp port is open in firefox under windows xp in virtualbox will it work as it does in Fedora? I have added the 2 ports to windows firewall.

    • Seth Keiper

      Is this post relevant to VirtualBox 4.0.4?

      Yes and No. Depends on your taste but always read the VirtualBox User Manual.

      I am using Fedora 14 as the host and Windows XP in VirtualBox. I have added two ports (TCP and UDP) to the firewall settings in 'other ports' in fedora? Do i need to do this?

      You can use the GUI just as well. It was added to remove ambiguity.

      I noticed the port forwarding option in the firewall setup which asks for either the ports to be forwarded to either local or an ip address. Do i need to do anything here?

      You probably would want a local IP.

      In the settings under VirtualBox under Network adapter 1 (which is enabled) and attached to NAT, there is a port forwarding option in the advanced setings. Can the host and the guest port be the same be the same number?

      No

      If i use 'open port checker' to check if the tcp port is open in firefox under windows xp in virtualbox will it work as it does in Fedora? I have added the 2 ports to windows firewall.

      with any GNU/Linux distro (which fedora is included in), I would suggest to either use netstat or nmap. Your choice.

4 Trackbacks / Pingbacks for this entry

Leave a Reply

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!