Vous êtes ici

Headless VirtualBox virtual machine

LogoNeed

For all my software development projects, I now use one VirtualBox virtual machine per project. This allows me to isolate my development environment from the physical machine I'm using, not to mix several software environment configurations that could be potentially incompatible, and, when required, to easily provide a colleague with a perfect copy of my development environment.

A few days ago, another need appeared: to make available a demonstration based on a given version of a project. This time, the virtual machine would not run on a desktop PC, but on a server. And it should be kept running for weeks and possibly months.

Solution

One possible way to fulfill this need is to run the virtual machine in headless mode. You can find below required steps.

Environment

But before, a few words about the software environment. The server runs Ubuntu Desktop (yes!) 16.04. My desktop PC runs Linux Mint 17.3 (a distribution based on Ubuntu 14.04).

By the way, the so-called server is a desktop workstation. This explains why it runs Ubuntu Desktop... It is not in my office, but the only time I used its keyboard, mouse and display was when I configured the BIOS (see below). All other operations were performed remotely.

BIOS configuration

Ensure that virtualization is enabled in the BIOS.

VirtualBox installation

Connect to the server using ssh, with X protocol tunneling:

ssh -X <username>@<machineName>

then install VirtualBox on the server:

$ wget https://download.virtualbox.org/virtualbox/6.0.4/virtualbox-6.0_6.0.4-128413~Ubuntu~xenial_amd64.deb
$ sudo dpkg -i virtualbox-6.0_6.0.4-128413~Ubuntu~xenial_amd64.deb
# Install missing dependencies.
$ sudo apt-get -f install
# Download VirtualBox Extension Package.
$ wget https://download.virtualbox.org/virtualbox/6.0.4/Oracle_VM_VirtualBox_Extension_Pack-6.0.4.vbox-extpack
# Install gksu, required to install the extension package.
$ sudo apt-get install gksu
# Set root password to a known value, as this password will be requested when
# installing the extension package.
$ sudo su
passwd
# Starts an xterm.
$ xterm

From the xterm, start VirtualBox, with the virtualbox command. From File > Preferences... > Extensions, install the extension package. Requested password is root's one.

Virtual machine management

Transfer a copy of the virtual machine from the development machine to the server. It must have been saved as an appliance. From VirtualBox UI, import it, with File > Import Appliance....

Click on the virtual machine, and enable remote desktop with Settings > Display > Remote Display:

  • tick Enable Server
  • you can keep default values:
    • 3389 for Server Port
    • Null for Authentication Method
    • 5000 for Authentication Timeout

With Settings > Network, set Attached to to NAT. Click on Advanced, then on Port Forwarding, and add a forwarding rule for every port of the virtual machine that must be accessible from a remote machine.

Stop the guest machine by performing a shutdown, close VirtualBox, close the xterm.

Now, start the virtual machine in headless mode, from the ssh terminal:

VBoxManage startvm "<VMName>" --type headless

You can now close the ssh connection. The virtual machine will keep running.

To access its GUI from a remote PC (running Linux):

# -k option enforces the right keyboard mapping. Use required mapping
# e.g. en-US, etc.
$ rdesktop -k fr-fr <hostName>:3389

Some other useful commands:

# Stop the VM, saving its state.
$ VBoxManage controlvm "<VMName>" savestate
# Display list of existing VMs.
$ VBoxManage list vms
# Display list of active VMs.
$ VBoxManage list runningvms

VirtualBox full documentation is here.