mercoledì 2 gennaio 2013

VirtualBox port-forwarding

Today on my Munich-Florence train, I've waisted some time just trying some ways for "sshing" my puppet VirtualBox guest. Usually DHCPclient does everything and, once the ip address is got by the guest, I can easily "ssh" into it. This time, I had not a wifi connection and I've encoutered some annoying bounces on my ssh attempts. It looked like the guest was unreachable. I've instinctively tried every "nat", "bridged", "host-only" options.. and in the end .. it touched me to read the documentation :) (by the way, a very good paper) 

..and it was so i've discovered this official best practice to port-forward services on a VirtualBox guest

In this example (exactly extracted from the doc), we are going to portforwarding the SSH service, from our HOST 8888 port, to the GUEST (vm name: "Puppet Test Machine") on 22 port:

zmo@naropa:~$ VBoxManage modifyvm "Puppet Test Machine" --natpf1 "sshService,tcp,,8888,,22"
view raw gistfile1.sh hosted with ❤ by GitHub
"sshService" is just a label.

In this way, our HOST will keep the forward on each interfaces. Anyway, it's possible to bind a specific interface though.

Now that we have our forward ready, we can connect the loopback on the given port
zmo@naropa:~$ ssh -p 8888 root@localhost
Last login: Wed Jan 2 14:31:36 2013 from 10.0.2.2
Welcome to puppet!
[root@puppet ~]#
view raw gistfile1.sh hosted with ❤ by GitHub
This rule will be permanet unless you explicitly remove it. See the rule properties:
zmo@naropa:~$ VBoxManage showvminfo "Puppet Test Machine" | grep ssh
NIC 1 Rule(0): name = sshService, protocol = tcp, host ip = , host port = 8888, guest ip = , guest port = 22
view raw gistfile1.sh hosted with ❤ by GitHub
Then delete the rule:
zmo@naropa:~$ VBoxManage modifyvm "Puppet Test Machine" --natpf1 delete "sshService
view raw gistfile1.sh hosted with ❤ by GitHub