Ubuntu, VirtualBox – How to start your virtual machines automatically at boot!

I use a VirtualBox virtual host at work, and I needed to make it so that in the event things really went south with the server, someone at the office can just hard reboot it and it would take care of itself without me having to ssh in to start everything back up manually.

You probably have the application “screen” installed already, but if you don’t, it’s required for this tutorial.

sudo apt-get install screen

It’s a tiny install and should be done in seconds. Now, the file /etc/rc.local is more or less the “start-up script” that runs things after the system has come up but before anybody has logged in. We’ll edit it so that VirtualBox starts up in headless mode behind the scenes. We start with:

sudo nano /etc/rc.local

In this example, let’s assume my username is ‘jorg’ and I have all necessary personal permissions to run the virtual machine. Insert the following line anywhere above the ‘exit 0’ that comes at the end of the script.

sudo -u jorg screen -d -m VBoxHeadless –startvm “Windows 2008” –vrde=off

And you’re done.

So what does this do?

sudo -u tells the kernel “run this command as a different user” (we want to run it with my normal, non-privileged account because rc.local would run it as root otherwise!).
jorg is just my username.
screen -d -m starts an instance of “screen” that is already in “detached” mode (read: behind the scenes). You wouldn’t think so, but both parameters are required to do this in this context.
VBoxHeadless –startvm “Windows 2008” starts the VM itself.
–vrde=off suppresses the vrde module. It is normally not installed but this should suppress it if someone did come along and install and try to run it.

Leave a Reply