Sometimes, you want to test a new server in your guest machine, such as a web/application/database server.
In this cases you need a way to connect to a port on the guest machine from your host pc.
Luckily, VirtualBox, through the VBoxManage tool, allows you to forward packets from one port on the host machine to one on the guest machine easily.
On this example, I’ll show you how to forward from port 9022 on host machine to port 22 on guest machine, commonly used for the SSH daemon.
VBoxManage setextradata "Operating system - Debian" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 9022
VBoxManage setextradata "Operating system - Debian" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" 22
VBoxManage setextradata "Operating system - Debian" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" TCP
* “Operating system - Debian” is the name of your VirtualBox image
You can check for the succesful completion of these commands by typing:
VBoxManage getextradata "Operating system - Debian" enumerate
Finally, you need to restart your guest machine and test the connection.
ssh -p 9022 localhost