Connect To Virtualbox Guest Pcs Via Ssh Without Bridge Utils

I needed a virtual machine for debugging some Python memory leaking software without risking the health of my Ubuntu system. VirtualBox was my first choice because of its ease of install (just "apt-get install virtualbox"). Since I wanted to access the VM via ssh (instead of using the VirtualBox console) I found a bit annoying not being able to access the guest PC from my host PC. This is because VirtualBox does user-level NATing for providing connectivity to the guest PC. Although you can change the default virtual networking method, and use bridging instead of NAT, that requires installing bridge-utils and some networking configuration changes). I wanted something simpler, which didn't involve installing additional software or modifying my network settings. After all, that's the reason why I installed VirtualBox in the first place: to leave my system untouched. So after peaking at the manual, I found VirtualBox supports port forwarding between the host and guest PCs.

You have to run the following commands with the guest PC turned off:

VBoxManage setextradata "Guest PC" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" TCP
VBoxManage setextradata "Guest PC" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" 22
VBoxManage setextradata "Guest PC" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 2222

Where "Guest PC" is the virtual machine name, the same name you see in the machines list (in the VirtualBox main window). Those commands add a local forward from TCP port 2222 (on the host) to TCP port 22 on the guest.

So now you can access the guest PC via ssh by running "ssh localhost -p 2222"

If you want to make it even simpler you can provide a quick shortcut by adding these lines to your ~/.ssh/config:

Host guestpc
    Hostname localhost
    Port 2222

After that, you can connect by just typing "ssh guestpc"