VirtualBox Headless VM

Scenario / Question:

How do I control VirtualBox from the linux command line CLI

Solution / Answer:

Use the included tools VBoxManage and VBoxHeadless

First Install VirtualBox from this post:

Install VirtualBox without GUI or Headless

VBoxManage for controlling VirtualBox on the Command Line

The main tool for controlling VirtualBox is called VBoxManage. The VirtualBox manual has 27 pages describing all the available paramaters for using VirtualBox. TheVirtualBox manual is a great resource to accompany this tutorial.

For this tutorial we will use Centos 5 as our guest OS to be installed.

Step 1 – Upload OS ISO

Upload Centos OS DVD / CD ISO file to server

Step 2 – Create new VM

First log in to the system with the user you want to use for your VM’s or create a new user for managing the VM’s. VirtualBox allows each user on the system to have and control there own VM’s.  The VM information is stored in /home/<username>/.VirtualBox ( The directory is created after you create your fist VM )

Create a new user (optional)

# useradd vmadmin
# passwd vmadmin

Login in as the new user vmadmin

Create the new VirtualBox guest VM

First we need to get the ID value for the type of OS that we are installing. Pick the ID value that corresponds to the type of OS you are going to install for the guest VM:

vmadmin$ VBoxManage list ostypes

Since I am going to install Centos 5 I would choose “RedHat”

–name – Name you want for your new VM guest

– ostype – Type of OS you are installing

–register – Register the new VM with VirtualBox (important)

vmadmin$ VBoxManage createvm --name "<guestvmname>"  --ostype RedHat --register

Create a Harddrive for the new VirtualBox guest VM to use

Now we need to create a VirtualBox VM Harddrive to go along with our VirtualBox VM machine:

– filename – File name you want for you new VM Harddrive

– size – The size of your new VirtualBox VM Harddrive in MB (n)

–register – Register the new VM Harddrive with VirtualBox (important)

vmadmin$ VBoxManage createhd --filename "<harddrivename.vdi>" --size n -- register

Modify the guest VM to use the VM Harddrive

–hda – Harddrive device

<guestvmname> – The name of guest vm you want to modify

<harddrivename.vdi> – The vm harddrive you want to add to this guest vm

vmadmin$ VBoxManage modifyvm "<guestvmname>" --hda "<harddrivename.vdi>"

Register our OS ISO with VirtualBox

–dvd (used for CD iso and DVD iso)

<filelocation and file name> – Full path to ISO file. (/home/vmadmin/Download/Centos5.iso)

vmadmin$ VBoxManage openmedium dvd <filelocation and file name>

Attach the ISO file to the VM

–dvd (used for CD iso and DVD iso)

<filelocation and file name> – Full path to ISO file. (/home/vmadmin/Download/Centos5.iso)

vmadmin$ VBoxManage modifyvm "<guestvmname>" --dvd <filelocation and file name>

Step 3 – Configure VRDP for VM

We need a way to view our guest VM. VirtualBox provides an RDP server that allows you to interact with each VM and mimic the functionally of a monitor.keyboard, and mouse that would normally be plugged in to a real server.

Enable VRDP on the guest VM

–vrdp – Turn vrdp function off and on

–vrdpport – Specify a port to use for the RDP connection (n)

–vrdpauthtype – Specify the type of authentication for connecting RDP users.

null – no authentication (insecure)

external – Use PAM (login with linux users on the system)

vmadmin$ VBoxManage modifyvm "<guestvmname>" --vrdp on --vrdpport n --vrdpauthtype null --vrdpmulticon on

Open the –vrdpport n port on your firewall

Step 4 – Start the VirtualBox VM

When using the command VBoxHeadless the VM will start in the current console and if you logout or close that console your VM will terminate. Therefore we use the linux commands nohup and & to place the process in the background.

vmadmin$ nohup VBoxHeadless --startvm <guestvmname>

Step 5 – Connect to your VM

Use an RDP client to connect to your VM. Enter the servers IP and your port number that you set with –vrdpport

Step 6 – Remove DVD / CD from VM

After you have installed your OS on the VM from the DVD / CD you will want to remove it from the list of devices, so that your VM doesnt always boot into the DVD / CD on startup.

vmadmin$ VBoxManage modifyvm "<guestvmname>" --dvd none

For more options to Control your VM:

VirtualBox Controlling Headless VM

Fabio Milano has written 80 articles for us. Fabio Milano is a certified RHCE, MCP, and CFOI. He runs an IT consulting and services company called RDS Support inc. Website:http://www.rdssupport.com
The information provided is for educational purposes only. All content including links and comments is provided "as is" with no warranty, expressed or implied. Use is at your own risk and you are solely responsible for what you do with it.

4 Comments so far

  1. David Ramirez on November 2nd, 2009

    Thank you for posting. Especially for Step 6 that other tutorials seem to forget… I couldn’t break the loop of restarting the VM only to see it again wanting to start the installation of the guest OS.

  2. Confluence: TMS Flux on July 15th, 2010

    Testing a clean redundant installation…

    Install Virtual Box If not already installed ({{rpm qa grep VirtualBox}}), install virtual box. See…

  3. Confluence: TMS Flux on July 15th, 2010

    I have Virtual Box installed, now I want to install CentOS myself…

    Create a clean CentOS installation inside Virtual Box # Copy the CentOS DVD iso to the server you have installed Virtual Box # Create an empty virtual machine with a harddisk: VBoxManage createvm name “CentOS 5…….

  4. [...] How to make Virtual Box Image Auto Start on Ubuntu (Without GUI) January 11, 2012 Gigih Forda Nama Leave a comment Go to comments Virtualisasi saat ini merupakan salah satu alternatif dalam rangka mewujudkan Teknologi Ramah Lingkungan / Green Technology, sumber daya perangkat yang digunakan  bisa diminimalisir namun dapat menghasilkan Cluster System baru yang bisa dikustomize dengan kualitas tangguh. Proses maintenance dapat dengan mudah dilakukan meskipun berhadapan dengan banyak aplikasi produksi. Beberapa aplikasi tersohor mengusung teknologi Virtualiasi ini misalnya VMWare, VirtualBOX, Citrix, Hyper-V, Dll, kesemuanya memiliki kelebihan dan kekurangan masing-masing.  Bagi anda penggemar Dunia Open Source patut bersenang hati  karena VirtualBox hadir dengan layanan Free/Gratis namun fasilitas yang Oke punya. Bagi yang penasaran dan ingin mencoba VirtualBox bisa ikuti panduan dibawah, saya paste dari alamat berikut  http://www.kernelhardware.org/virtualbox-headless-vm [...]

Leave a reply