Here are useful commands to setup the port forwarding in a VirtualBox VM.
-- Forward local port 8888 to VM port 80 (Apache httpd)
VBoxManage setextradata "Ubuntu" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/httpd/Protocol" TCP
VBoxManage setextradata "Ubuntu" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/httpd/GuestPort" 80
VBoxManage setextradata "Ubuntu" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/httpd/HostPort" 8888
-- Forward local port 9090 to VM port 8080 (Apache Tomcat)
VBoxManage setextradata "Ubuntu" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/tomcat/Protocol" TCP
VBoxManage setextradata "Ubuntu" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/tomcat/GuestPort" 8080
VBoxManage setextradata "Ubuntu" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/tomcat/HostPort" 9090
-- Forward local port 2222 to VM port 22 (SSH)
VBoxManage setextradata "Ubuntu" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" TCP
VBoxManage setextradata "Ubuntu" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" 22
VBoxManage setextradata "Ubuntu" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 2222
- Ubuntu: Represents the name of the VM.
- pcnet: Represents the driver of the network interface configured in the VM:
- pcnet corresponds to the PCnet FAST II and PCnet FAST III devices
- e1000 corresponds to the Intel PRO/1000 devices
- 0: Represent the network interface number (you can configure multiple network interfaces.
- “httpd”, “tomcat”, “ssh” are custom (and identifier) names for a particular configuration.
- Protocol: The protocol to be used, can be TCP or UDP
- GuessPort: The target port (in our VM)
- HostPort: The source port (in the PC hosting the VM)
Note: These commands need to be executed from the command line of the host operating system while the VM is off.