Connect to VirtualBox guest PCs via ssh without using 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”
7 de May de 2008 a las 12:54 am
Tenes otros truquitos semejantes aca:
Apuntes sobre linea de comando y Virtualbox
7 de May de 2008 a las 12:07 pm
Good tips on that page, thanks Rodolfo!
23 de June de 2008 a las 2:44 pm
gracias Rodolfo Pilas!