How To: set up ssh through NAT to a VirtualBox instance
I’m currently participating in the private beta test of SpringSource’s tcServer (more about that later), and in order to kick the tires on the remote management features, I’m setting up a couple of VirtualBox instances on which to install tcServer.
I’d used VirtualBox before, and it compares favorably to VMWare in most cases. This is the first time, however, that I’ve tried to set up an inbound network connection to the VBox guest. A previous attempt to set up Host (bridged) Networking on VBox failed miserably. So, apparently, I have no choice but to use NAT for the VBox guest. This is the one place where, for desktop use, VMWare seems much better - bridged networking in VMWare “just works”.
So, in order to communicate with the VBox guest, I needed to forward a port through the NAT.
It’s very easy to do, using the VBoxManage tool, and is documented in many places, including here.
I’ve listed the commands to forward port 2222 on the host to 22 on the guest below, mostly for my own ease of reference:
VBoxManage setextradata <guest-name>
"VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 2222
VBoxManage setextradata <guest-name>
"VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" 22
VBoxManage setextradata <guest-name>
"VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" TCP
You can list the current settings with the following command:
VBoxManage getextradata <guest-name> enumerate
After that, you need to restart the virtual machine. Then you can then ssh to localhost on port 2222, and be forwarded to port 22 on the guest.
Note that the “ssh” part of the VBoxInternal device setting can be anything you want (but needs to be the same for all three settings for any given port that you’re forwarding). So, you can forward as many ports as you need to, you just need to set these three items for each forward, and use a different name (instead of “ssh”) for each one.