20 Aug, 2008
VirtualBox uses NAT networking so that the running machine appears to the outside world to be the same as the host. This isn't a lot of use if you want to SSH from the host to the virtual machine. Luckily there is a work around using port forwarding described at http://mydebian.blogdns.org/?p=148
The essence of the technique is to type these commands to forward port 2222 to port 22 replacing <guestname> with the VirtualBox name for the guest, in my case "Etch 2":
$ 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'll need to power off and power on the virtual machine (not just restart it) then you will be able to SSH in with this:
$ ssh -p 2222 root@localhost
You can do the same with other ports such as 80:
$ VBoxManage setextradata <guestname> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/HostPort" 8080 $ VBoxManage setextradata <guestname> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/GuestPort" 80 $ VBoxManage setextradata <guestname> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/Protocol" TCP
You Apache server will be available at http://localhost:8080
It appears you can choose whatever name you like eg ssh, apache etc but if you use the same key twice the second settings will over-write the first.
This isn't completely ideal so if anyone knows a good way of setting up bridged networking for VirtualBox on Mac OS X please let me know.
Update 2008-12-19: If you get this error:
ssh_exchange_identification: Connection closed by remote host
It could simply be because you forgot to install openssh-server on the guest OS. I made that mistake today and it took me 15 minutes of Googling obscure articles to figure out!
Copyright James Gardner 1996-2020 All Rights Reserved. Admin.