VirtualBox Headless

Sun Virtual Box Logo

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

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

4 Responses to VirtualBox Headless

  1. […] use your repositories from outside the VM, you can forward the TCP port 3690 to 3699 for e.g. see this post for more details on port […]

  2. Phil says:

    Hi !

    Do you have any suggestions for users attempting to run Sun VirtualBox headless within a Windows host? Perhaps as a Windows Service, with the same autostart features?

  3. itxperiment says:

    You can do the same thing on a Windows host.
    To run the VM in haedless mode simply use the same command line (you will have to add theVBoxHeadless command in the PATH).

    To connect to the VM by using ssh and the X display it will require two additionnal applications:
    – A X Server: My advice is to use Xming http://sourceforge.net/projects/xming/
    – A SSH client: My advice is to use Putty http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

    First run Xming server, it will simply appear in the task bar.
    Putty will require a little bit of configuration:
    – Choose SSH on localhost and change the port to 2222 in the main session window
    – In the SSH / X11 section in the tree, check “Enable X11 forwarding”
    That’s all !
    Hope it helps.

  4. [Quote]
    Do you have any suggestions for users attempting to run Sun VirtualBox headless within a Windows host?…
    [/Quote]

    I know that people frown upon people promoting their blog in someone else’s comment feed, but I wrote a small bit on running a headless virtual machine (Sun VirtualBox) on Windows. This may help others in search of assistance.

    http://www.justinkblog.com/2010/07/how-to-run-headless-virtual-machine.html

Leave a comment