Port forwarding with VirtualBox

Thought I’d share this little tip for anyone running VirtualBox (and that should be everyone….if you’re not….you know who you are….)

Setting up port forwarding for those who aren’t familiar with it can be a little confusing.
For those who don’t know… http://en.wikipedia.org/wiki/Port_forwarding (cause Wikipedia is the single source of truth for EVERYTHING!)

In VBox it is pretty easy…

I’ve tested this on my Mac but I’m pretty sure it would work with Windows as well

From terminal, with the VBox app running but with your virtual machine powered off issue this command


export VBOXName=DemoDB
VBoxManage modifyvm $VBOXName --natpf1 SSH,tcp,,2222,,22

The above calls the VBoxManage tool with the modifyvm switch.
The name of my virtual machine is DemoDB (you put in your guest vm name…which can be found from by looking in the Virutal Box Manager screen)
I pass the NAT network device, the name of the port forward title (SSH), the protocol, the host IP (empty), the host port to listen on, the guest IP (empty) and the guest port to forward

You can use this for any port on the guest that you would like to forward to the host like this…

VBoxManage modifyvm DemoDB --natpf1 DBControlORCL,tcp,,1158,,1158

– or –

VBoxManage modifyvm DemoDB --natpf1 FTP,tcp,,2121,,21

– or –

VBoxManage modifyvm DemoDB --natpf1 sql,tcp,,1521,,1521

To see the port forwarding you’ve set up just query VBoxManage again


tardis:Desktop matt$ VBoxManage showvminfo DemoDB | grep NIC
NIC 1:           MAC: 080027BD68CA, Attachment: NAT, Cable connected: on, Trace: off (file: none), Type: Am79C973, Reported speed: 0 Mbps, Boot priority: 0
NIC 1 Settings:  MTU: 0, Socket( send: 64, receive: 64), TCP Window( send:64, receive: 64)
NIC 1 Rule(0):   name = DBControlOrcl, protocol = tcp, host ip = , host port = 1158, guest ip = , guest port = 1158
NIC 1 Rule(1):   name = FTP, protocol = tcp, host ip = , host port = 2121, guest ip = , guest port = 21
NIC 1 Rule(2):   name = SSH, protocol = tcp, host ip = , host port = 2222, guest ip = , guest port = 22
NIC 1 Rule(3):   name = sql, protocol = tcp, host ip = , host port = 1521, guest ip = , guest port = 1521

To connect via ssh to you guest VM just do this…as tested in a Mac but would work with Linux….and maybe Cygwin(?)


ssh oracle@localhost -p 2222

To hit the Database Control from your browser du jour do this
https://localhost:1158/em/console

I’ve tested this while connected with VPN and it works!!!!

Hope this helps!!!

2 thoughts on “Port forwarding with VirtualBox

Leave a comment