VirtualBox is an amazing software, it permits to emulate several guest systems. This process is also called virtualization. With this tool, it is, for example, possible to run a Linux installation (called Guest) on a Mac OS X system (called host).In this way, this is very easy to run a Linux safely. This virtual environment will be a great playground to perform tests, install softwares, etc. since it will be easy to save and restore a previous configuration without spoiling your main environment.

Ubuntu running on Mac OS X inside a VM
I will tell more about VirtualBox features and usages in next posts.
But know, I will explain how to access the system running in the VirtualBox environment (the Guest) from the host machine. This is useful, for example, if you want to use your Linux installation trough the SSH protocol. This avoid to be inside the Virtual Machine (VM) screen to use it. You can then use your VM as if was a server located on your network.
The default network protocol used when you create a VM is NAT (Network Address Translation). With this configuration, it is possible to access by default to the Internet form the Guest. But if we want to access the Guest from the Host trough ssh an additionnal setting is required. It consists in defining port forwarding for the ssh port. The goal is to forward to the default ssh port (22) on the Guest, queries made on a new port (2222) on the Host.
Declaring port forwarding (To run these instructions the VM must not be running)
VBoxManage setextradata "Ubuntu" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/Protocol" TCP VBoxManage setextradata "Ubuntu" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/GuestPort" 22 VBoxManage setextradata "Ubuntu" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/HostPort" 2222
Where:
- Ubuntu: is the name of the VM
- pcnet: is the default network card
- gestssh: is a name given to the setting
Behind the scene, the definition file of the VM (Ubuntu.xml in this case) is updated:
<ExtraData> <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/Protocol" value="TCP"/> <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/GuestPort" value="22"/> <ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/HostPort" value="2222"/> </ExtraData>
Installing the server on the guest
Now it’s time to install the ssh server on the Guest.
On the Guest:
sudo apt-get install openssh-server
Now the ssh server is installed and is running.
Enabling the X protocol on ssh
On a Ubuntu installation the X server is configured with the -nolisten tcp option. In order to be able to use the X protocol trough this configuration, this option has to be enabled.
sudo vi /etc/gdm/gdm.conf #DisallowTCP=true DisallowTCP=false
The X server needs a restart to take this modification into account.
Run the VM in Headless mode
The Headless mode permits to run a VM without the need to use the VirtualBox GUI nor to have the VM displayed in a window. To do that run the VM as folllow:
VBoxHeadless --startvm Ubuntu --vrdp=off
The vrdp option is turned off because it is useless since we will use ssh to connect to the VM.
Connect to the Guest form the Host
Use an ssh client, for example the command line to connect to the Guest.
ssh -X [user]@localhost -p 2222

Posted by Ro