VirtualBox: Port Forwarding for NAT network

October 29th, 2010 mysurface Posted in VBoxManage, VirtualBox | Hits: 149087 | 2 Comments »

If you came across the solution of setting up port forwarding for ssh port like this:


VBoxManage setextradata [guestname] "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 2222
VBoxManage setextradata [guestname] "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" 22
VBoxManage setextradata [guestname] "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" TCP

You may want to try out this alternative approach. This approach is using the same command ‘VBoxManage’, but it is more flexible and simple.

1. First of all, I power off my guest machine (virtual machine), this approach refuse to work if my guest machine is running.

2. Next, I query my guest machine’s vminfo.

Let say my guest machine ‘s name is ‘gentoo’ (I am gonna use this as example for the entire post) , I will do this:

VBoxManage showvminfo gentoo

It shows a long list of very informative results. I am only interested on my guest machine’s NIC info. Therefore I do this instead.

VBoxManage showvminfo gentoo | grep NIC
NIC 1:           MAC: 0800272DC585, Attachment: NAT, Cable connected: on, Trace: off (file: none), Type: virtio, Reported speed: 0 Mbps, Boot priority: 0
NIC 1 Settings:  MTU: 0, Socket( send: 64, receive: 64), TCP Window( send:64, receive: 64)
NIC 2:           disabled
NIC 3:           disabled
NIC 4:           disabled
NIC 5:           disabled
NIC 6:           disabled
NIC 7:           disabled
NIC 8:           disabled

Based on the result of vminfo, indicates that my guest machine configured a NIC which is NIC1.

Ok, now lets do a port forwarding. Take ssh port as an example. I would like to forward my guest ssh port (22) to my host machine at port (2222). I will do this:

VBoxManage modifyvm gentoo --natpf1 "ssh,tcp,,2222,,22"

The syntax for modifyvm –natpf shows as below:

VBoxManage [guestname] --natpf[1-N] "[function-name],tcp|udp,[hostip],[hostport],[guestip], [guestport]" 

‘function-name’ can be any name which well describe the port forwarding rule.

Lets querry the vminfo again.

VBoxManage showvminfo gentoo | grep "NIC 1"
NIC 1:           MAC: 0800272DC585, Attachment: NAT, Cable connected: on, Trace: off (file: none), Type: virtio, 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 = ssh, protocol = tcp, host ip = , host port = 2222, guest ip = , guest port = 22

Observed that I ignore the IP address settings because I am using DHCP on my guest machine NIC.

To delete the port forward rule, I do this:

VBoxManage [guestname] --natpf[1-N] delete [function-name]: 
VBoxManage gentoo --natpf1 delete ssh

At last, I boot up my guest machine to verify the port forwarding setting.

VBoxManage startvm gentoo

VBoxManage can do more than just port forwarding, check it out the manual if you want to know more.

Hope you enjoy.

2 Responses to “VirtualBox: Port Forwarding for NAT network”

  1. Take a look at http://code.google.com/p/phpvirtualbox/. I set it up last week and it is awesome!

  2. ?????? ?????????????????????????????

Leave a Reply