Posted 29 October 2008 (updated 18 July 2012) by Brendan Kidwell

Setup VirtualBox as a Service in Linux

I run VirtualBox on my office desktop to provide not only a Windows desktop environment for sticky Windows-requiring apps, but also to provide MS SQL Server for my web development stack of JRun, ColdFusion, and MS SQL Server. As such, it would be useful to have an init script that starts and stops VirtualBox automatically when the host system boots up and shuts down. That way, I don't have to think about manually starting VirtualBox before I can interact with my web development environment, and this is particularly useful if I have to work remotely after calling in to ask my office mate to start my computer.

This guide shows you how to setup a virtual machine hosted by VirtualBox on a Linux host. The guest OS can be anything. The guest will be configured to start or resume when the host boots and hibernate (suspend execution, save to disk, and terminate) when the host shuts down. The guest can be started and stopped from the command prompt as well. I have only tested this setup on Linux Mint 13 (based on Ubuntu 12.04), but it should work fine on any Linux that uses init.d scripts to start and stop services.

Alternate version available:

[Spanish] Crear servicio para VirtualBox – based on this page, by Carlos Gacimartín. Thanks Carlos!

10 Oct 2012: According to the VirtualBox manual, these instructions have been superseded by new built-in functionality as of version 4.2. I haven't tried the built-in method yet, but it should work better than my instructions on this web page.

Install VirtualBox in the usual way. Create your virtual machine. In particular I recommend these settings, but YMMV:

setting value
Display → Remote Display → Enable Servertrue
Display → Remote Display → Server Port 3389
Network → Adapter 1 → Attached to NAT

The reason I use NAT networking is because I feel it's easier to manage than any other networking. My network admin doesn't see any new mysterious MAC and IP addresses appearing on the network, and I know exactly what ports on the virtual machine are exposed to the outside world. If you prefer, you can use bridged networking instead.

Save your virtual machine, start it (in the GUI) and install your guest OS and VirtualBox Guest Additions. It's important that you install every kind of remote access you'll need, since the virtual machine's screen won't actually reside on your real desktop when you run the virtual machine as a service. I've found that VirtualBox's RDP server has some issues with catching screen updates, so I like to enable Windows' Remote Desktop server and forward it from an alternate port in VirtualBox's NAT.

(Aside: Most people wouldn't think of it for a Windows guest, but I find the SSH server provided by Cygwin quite useful, especially for no-nonsense SFTP file transfers. If you choose to use it, you can copy your host's /etc/ssh/ssh_host_* to your guest's Cygwin /etc/ folder and that will prevent SSH clients from complaining about different keys being used on different ports on your host IP address.)

To configure port forwarding in VirtualBox's NAT, go to Settings for your chosen VM, then Network → Adapter (n) (the one using NAT) → Advanced → Port Forwarding. Here are examples from my setup.

name protocolhost IPhost portguest IPguest port
MS SQLServerTCP 1433 1433
VNC TCP 5904 5900
RDP TCP 3390 3389
SSH TCP 8022 22

To test all these mapped ports, connect to them on “localhost” when the virtual machine is running:

rdesktop -g 1024x768 localhost      # connect to VirtualBox RDP server
rdesktop -g 1024x768 localhost:3390 # connect to Windows RDP server
ssh -p 8022 localhost               # connect to ssh

You have some alternatives for setting up networking.

If you are on a non-mobile computer that is always connected to the same network and it can grab as many IPs from your local gateway as it wants to, you can use Bridged Networking — the host OS and all the guest machines in this configuration look like peers on the local network.

You can also get by with NAT and Host-Only Networking — two network interfaces on the guest machine. VirtualBox will setup a private network in the 192.168.56.x subnet for the Host-Only virtual network, and your host OS and the guests can use that network to talk to each other. The guest can talk to the outside world using the NAT interface.

Be sure edit /etc/hosts on each host so they know what to call each other — or setup static hostnames in your local home/office router or use some other method if you need mnemonic names for your hosts instead of remembering IP addresses.

Now that you've got your virtual machine setup and configured and tested, you're ready to create and configure scripts in /etc/init.d/ to start and suspend the guest on demand.

First, if you look at /etc/init.d/vboxdrv, the service manager script for the VirtualBox kernel driver, you'll see that it looks for a couple of options to tell it what to do with running virtual machines when vboxdrv stops. Create or edit the config file:

sudo touch /etc/default/virtualbox
sudo nano /etc/default/virtualbox
SHUTDOWN_USERS="user1 user2" # space-delimited list of users who might have runnings vms
SHUTDOWN=savestate           # if any are found, suspend them to disk

Now, the above settings only cover the system shutdown process, not startup. VirtualBox itself doesn't provide any mechanism for starting virtual machines automatically. To do that, create a new service manager script of your own.

Old script used to be included here…

Grab a copy of my vbox-service-template init.d script and follow the instructions there to customize it for your VM and install it in /etc/init.d.

Test the script's status, start, and start commands:

/etc/init.d/vbox-NAME status # Is the VM running?
/etc/init.d/vbox-NAME start  # Start the VM
rdesktop localhost           # Connect to the VM
/etc/inid.d/vbox-NAME stop   # Stop the VM

Following the instructions on the vbox-service-tempalte page, Use the update-rc.d or chkconfig command to install the script if you want it to start your VM when you boot your computer

sudo update-rc.d vbox-NAME defaults 90 # Ubuntu
# ... or ...
sudo chkconfig vbox-NAME on         # Fedora

Now whenever you enter normal multiuser run mode (at boot time, for example) your VM will start, and when you exit multiuser mode (during shutdown, for example), your VM will be suspended to disk if it was running.

Steve, 2013-07-05 02:32

The reports of your old service template's death are greatly exaggerated.

VirtualBox's new autostart feature only handles the start -- VMs are unceremoniously killed on shutdown. Not acceptable. Your script is way, way better, please keep it around.

By the way, I ran into an issue with one of my Windows VMs where it didn’t want to shut down. In the event of a server shutdown, I can’t have the script waiting forever for the VM. So I modified the script to take an adjustable timeout, after which the VM is powered off. Happy to contribute the changes if you're interested, email me for it.

Fernando Abel Gottlieb, 2013-09-03 22:09

Hi Brendan!
Thanks for this special code!! I love it! :-)
Well, I'm using the code for a Linux Server (Ubuntu 13.04).
My server is configured like:
- RAID 1
- Various VMs
*- Snapshot Backup (actually programmed to 30 days)

* I changed your code to easily take and delete snapshots and if you are interested I can send you the code.
By the way, I wish your oppinion about that.

Thank you very much

Garyq, 2013-10-07 07:30


Thanks for your script!

A quick question....

If you have multiple VMs you want to start in sequence, what is the best way to proceed?

TNT, 2013-11-10 09:09

The vbox-service-template works on my Debian 7.1 box also very well! Thank you!

Knut, 2014-01-07 06:14

Works for me too in a Debian 7.3 in a HA environment, but I had to swap the exit codes on line 180 to get the correct exit codes on status (runnig=0, not running=1)


Haroon, 2014-05-07 11:20

If your VM starts when explicitly told to (i.e. 'service vbox-name start') but not after a reboot, you may want to simply leave VM_HOSTNAME blank. Also, I needed to change line 127 from '-type vrdp' to '-type headless.' I'm using Debian Squeeze and VirtualBox 3.2.10_OSE.

Peter, 2014-09-07 01:09

Your Service Script works on Ubunto14.04 but the VM would not start on reboot or Coldboot.
What may i do ?

To comment on this page, please copy and paste the following into an email and send it to me. Useful and informative comments will be published within a day or two.

To: brendan@glump.net
Subject: Glump.net comment - Setup VirtualBox as a Service in Linux

Regarding: https://www.glump.net/howto/virtualization/setup-virtualbox-as-a-service-in-linux
My name: [your name here]
My social media or web site: [optional URL here]
Publish this comment: yes

[your comment here]