Feb 23 2009

Port forwarding from Host to Guest in VirtualBox using NAT

VirtualBox’s GUI doesn’t expose this functionality, but the VBoxManage command line utility does. Before starting, remember to close your virtual machine before changing it’s configuration.

You need five pieces of information:

  • your virtual machine’s name in VirtualBox
  • a descriptive name for your port forward
  • the guest port
  • the host port
  • the protocol to use (TCP or UDP).

These three terminal commands are used to set up the port forward (replace bold sections with your information):
VBoxManage setextradata GuestName “VBoxInternal/Devices/pcnet/0/LUN#0/Config/DescriptiveName/HostPort� HostPort

VBoxManage setextradata GuestName “VBoxInternal/Devices/pcnet/0/LUN#0/Config/DescriptiveName/GuestPort� GuestPort

VBoxManage setextradata GuestName “VBoxInternal/Devices/pcnet/0/LUN#0/Config/DescriptiveName/Protocol� Protocol

For Example:

fenris@thinkbuntu:~/Documents$ VBoxManage setextradata OpenSolaris “VBoxInternal/Devices/e1000/0/LUN#0/Config/SSH/HostPort” 2222
VirtualBox Command Line Management Interface Version 2.1.4
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.

fenris@thinkbuntu:~/Documents$ VBoxManage setextradata OpenSolaris “VBoxInternal/Devices/e1000/0/LUN#0/Config/SSH/GuestPort” 22
VirtualBox Command Line Management Interface Version 2.1.4
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.

fenris@thinkbuntu:~/Documents$ VBoxManage setextradata OpenSolaris “VBoxInternal/Devices/e1000/0/LUN#0/Config/SSH/Protocol” TCP
VirtualBox Command Line Management Interface Version 2.1.4
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.

In this case, you could now connect via SSH to the guest though port 2222 on the host:
ssh -p 2222 localhost

The guest is available from other machines on the network as well though the same port at the host’s IP address (if your firewall allows it).

You can set up as many port forwards as you wish, just keep changing your DescriptiveName value. To remove a port forward, repeat the same three commands again, but without the values:
VBoxManage setextradata OpenSolaris "VBoxInternal/Devices/pcnet/0/LUN#0/Config/SSH/HostPort"
VBoxManage setextradata
OpenSolaris "VBoxInternal/Devices/pcnet/0/LUN#0/Config/SSH/GuestPort"
VBoxManage setextradata
OpenSolaris "VBoxInternal/Devices/pcnet/0/LUN#0/Config/SSH/Protocol"

You can view the configuration for a virtual machine, including port forwards, with this command:
VBoxManage getextradata GuestName enumerate

fenris@thinkbuntu:~/Documents$ VBoxManage getextradata OpenSolaris enumerate
VirtualBox Command Line Management Interface Version 2.1.4
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.

Key: GUI/SaveMountedAtRuntime, Value: yes
Key: GUI/LastCloseAction, Value: powerOff
Key: GUI/LastWindowPostion, Value: 231,5,720,447
Key: GUI/Fullscreen, Value: off
Key: GUI/Seamless, Value: off
Key: GUI/AutoresizeGuest, Value: on
Key: VBoxInternal/Devices/e1000/0/LUN#0/Config/SSH/HostPort, Value: 2222
Key: VBoxInternal/Devices/e1000/0/LUN#0/Config/SSH/GuestPort, Value: 22
Key: VBoxInternal/Devices/e1000/0/LUN#0/Config/SSH/Protocol, Value: TCP

One restriction you should be aware of is that you can’t use a host port lower than 1024 without running VirtualBox with escalated privilages.

Extra note: You can always access your host from inside your guest using the IP address 10.0.2.2 if you need to do the reverse of this.


Get Adobe Flash playerPlugin by wpburn.com wordpress themes