dimitris kalamaras

math, social network analysis, web dev, free software…

Creating Virtual Machines on a headless server via VirtualBox

VirtualBox is a great piece of virtualization software. It not only allows us to run and test different Operating Systems inside Virtual Machines (VMs) from the comfort of our main desktop computer with point-and-click, but it also enables us to setup and run VMs on remote headless servers from the command line. And we can control these VMs remotely via a plain RDP client. This is great because when you host your VMs on server hardware (usually without any graphical UI) you don’t really need the full fledged GUI of VirtualBox (and its Qt/SDL dependencies) nor to display the VM output locally. Instead you just need some commands to setup and start your VM. Then all you want is to connect to it remotely and install an OS. With VirtualBox, this is accomplished via the commands VBoxManage and VBoxHeadless. This is how it can be done.

Obviously, we need a server and a SSH connection to it. In this example I am using a Debian Wheezy server as host.

First, we download and install the latest VirtualBox package:

wget http://download.virtualbox.org/virtualbox/4.3.26/virtualbox-4.3_4.3.26-98988~Debian~wheezy_amd64.deb

dpkg -i virtualbox-4.3_4.3.26-98988~Debian~wheezy_amd64.deb

The dpkg tool will complain about some missing packages so you force it to fix the missing dependencies:

apt-get install -f

Next, we need to install the VirtualBox extension pack, which extends the functionality of VirtualBox. Among others it offers the VirtualBox Remote Desktop Protocol (VRDP) support which we need to control our VMs remotely. So we download the corresponding extension pack for our version of VirtualBox:

wget http://download.virtualbox.org/virtualbox/4.3.26/Oracle_VM_VirtualBox_Extension_Pack-4.3.26-98988.vbox-extpack

and install it with VBoxManage as follows:

VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.3.26-98988.vbox-extpack

Now we list the installed extensions

VBoxManage list extpacks

and we get

Extension Packs: 1
Pack no. 0: Oracle VM VirtualBox Extension Pack
Version: 4.3.26
Revision: 98988
Edition:
Description: USB 2.0 Host Controller, Host Webcam, VirtualBox RDP, PXE ROM with E1000 support.
VRDE Module: VBoxVRDP
Usable: true
Why unusable:

We are ready to create our first VM on the remote server. Before we do that, we need to decide which guest OS we will be using and download an installation ISO image for it. That ISO will be attached to our VM on its first boot for the installation process to begin. I chose the Debian network installation image, so I downloaded the relevant ISO:

wget http://cdimage.debian.org/debian-cd/7.8.0/amd64/iso-cd/debian-7.8.0-amd64-netinst.iso

Of course, you can use whatever OS and install image you like.

Now we are really ready to create and run our first VM. Again this is done through VBoxManage tool:

VBoxManage createvm --name "debian" --ostype Debian_64 --register

This command creates a VM named “debian” of type Debian 64bit and registers it (for other OS see “Tips” at the end of the article).

We created our VM, but we still need to configure it with some RAM, some disks and of course an ethernet card. First, we use a command like this:

VBoxManage modifyvm "debian" --memory 512 --boot1 dvd --nic1 nat

Here, we specify that our “debian” VM should use 512MB of RAM from its host, have a virtual DVD as first boot device and a NIC card in NAT mode which is the default networking mode for VMs running in VirtualBox and it suits our purposes (at the moment).

Next we need to create a virtual hard disk for our guest OS to be installed and attach that disk to our VM. Again VBoxManage lets us do this easily:

VBoxManage createhd --filename "debian.vdi" --size 40000

With the above command I created a virtual hard disk called “debian.vdi” of 40GB total size.

Now, we need to attach that vdi disk to the “debian” VM. But before we do that, we need a virtual controller in that VM:

VBoxManage storagectl "debian" --name "IDE Controller" --add ide --controller PIIX4

Now we are ready to attach the disk with the command:

VBoxManage storageattach "debian" --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium "debian.vdi"

The command above attaches the virtual disk called “debian.vdi” to port 0, device 0 of the virtual IDE controller as hard disk type (hdd).

We’re almost done. The last step is to attach the ISO image we downloaded from Debian archive. The command is similar, except that we declare it as dvddrive type and we attach it to device 1:

VBoxManage storageattach "debian" --storagectl "IDE Controller" --port 0 --device 1 --type dvddrive --medium debian-7.8.0-amd64-netinst.iso

That’s all. Now, we just start the VM with the VBoxHeadless command:

VBoxHeadless --startvm "debian"

or
VBoxManage startvm debian --type headless

Our VM should be up and running, provided we see no error message. What is more, VBoxHeadless has already enabled RDP access to it, so we can connect to our VM through an ordinary rdp client (port 3389):

root@rory:~# VBoxHeadless --startvm "debian"
Oracle VM VirtualBox Headless Interface 4.3.26
(C) 2008-2015 Oracle Corporation
All rights reserved.

VRDE server is listening on port 3389.

Now we can connect to our debian guest remotely via rdp with a command like the following. Change “server_address” with the address of your host server and “user” with a valid user in the host server. You will be asked to provide the user’s password, obviously…

rdp://user@server_address:3389

You’ll see the familiar debian grub screen to begin the installation process.

vbox-rdp-debian

Have fun!

Tips

1. After you install Debian, you might want to port forward a specific port from the host to your guest OS. For instance, you might want to forward port 8000 from the host to port 80 of the guest debian, so you can serve web pages from inside the VM:

VBoxManage modifyvm "debian" --natpf1 "natapache,tcp,,9000,,80"

2. Also, you might want to see the full configuration of a particular VM. For this you use the “showvminfo” command:

VBoxManage showvminfo "debian"

3. To pause, resume, reboot or poweroff VMs, there is the “controlvm” command. Examples:
VBoxManage controlvm "debian" pause
VBoxManage controlvm "debian" resume
VBoxManage controlvm "debian" reset
VBoxManage controlvm "debian" poweroff

or you might prefer the more elegant “savestate” option which saves the current VM state to the disk before powering off:

VBoxManage controlvm "debian" savestate

4. If you are setting up another guest OS, you can see all the supported ostypes with the command:

VBoxManage list ostypes

You’ll see something like this (abbreviated):


....
ID: WindowsNT4
Description: Windows NT 4
Family ID: Windows
Family Desc: Microsoft Windows
64 bit: false
....
ID: Linux26
Description: Linux 2.6 / 3.x (32 bit)
Family ID: Linux
Family Desc: Linux
64 bit: false
...
ID: ArchLinux_64
Description: Arch Linux (64 bit)
Family ID: Linux
Family Desc: Linux
64 bit: true
...
ID: Debian
Description: Debian (32 bit)
Family ID: Linux
Family Desc: Linux
64 bit: false

5. If your guest OS will host some websites but you don’t want them to take all your host’s bandwidth, you can limit their maximum allowed bandwidth (for outgoing traffic only). Limits are configurable via the bandwidthctl command of VBoxManage. For instance, the following commands set up a 2Mbit/second limit on the “debian” guest:

VBoxManage bandwidthctl "debian" add Limit --type network --limit 2m
VBoxManage modifyvm "debian" --nicbandwidthgroup1 Limit

6. To remove a dvd drive from the VM you use storageattache but with emptydrive medium:

VBoxManage storageattach "debian" --storagectl "IDE Controller" --port 0 --device 1 --type dvddrive --medium emptydrive

7. Once you setup a good VM, you can use it as “master” for your future VMs. The “clonevm” command allows you to clone the VM into a new one and register it at once:

VBoxManage clonevm "debian" --name "newname" --register

By default, the current state of the VM is cloned without any snapshots. If you want to clone the snapshots as well, use the “–mode all” parameter.

VBoxManage clonevm "debian" --name "newname" --mode all --register

8. If you need to transfer the VM to another server, you can export it into a virtual appliance in OVF format (including its hard disks) via the export command:

VBoxManage export "debian" -o debian.ovf

Then you can transfer the ovf file and import it to the VirtualBox of the other server via import:

VBoxManage import debian.ovf

VirtualBox will recreate the VM completely for you.

Previous

The new features in versions 1.3 and 1.4 of SocNetV

Next

How to setup Maildir style virtual mailboxes in Debian with Postfix

4 Comments

  1. MH

    Hi,
    I’ve followed the tutorial till the end, but when i try to connect to VRDP i get error: “no bootable medium found”, please help.
    Thanks.

  2. Obviously, something went wrong when you installed your preferred OS. This message has nothing to do with VirtualBox. You should attach the OS installation ISO to your VM and try to re-install the operating system.

  3. Telokis

    Hello!
    Thanks for the tutorial but, before trying it, I’d like to know if it is still considered up to date in april 2017.
    Thanks in advance!

  4. This tutorial is for Varnish 3.x. If you use that version, the tutorial is still relevant. But you cannot use it for Varnish 4.x

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Powered by WordPress & Theme by Anders Norén