AutoStart VirtualBox VMs on System Boot on Linux

|
Last Updated:
|
|

In this guide, we are going to learn how to autostart VirtualBox VMs on system boot on Linux. On a Linux system with VirtualBox installed, you can start VMs automatically during system boot.

Automatically Start VirtualBox VMs on System Boot on Linux

There are multiple ways in which you can configure your VirtualBox vms to automatically start on system boot.

A few of these ways that we will discuss in this guide include;

  1. Using VirtualBox Autostart Service
  2. Using Systemd service unit
  3. Using system cronjobs

Using VirtualBox Autostart Service

VirtualBox comes with a service called vboxautostart-service that makes it easy to automatically start virtual machines during system reboot.

In order to configure a VirtualBox VM to start on system boot on Linux, you need to activate the autostart service. The autostart service can be activated by setting two variables in /etc/default/virtualbox;

  • VBOXAUTOSTART_DB – which defines the absolute path to the autostart database directory, usually the /etc/vbox.
  • VBOXAUTOSTART_CONFIG – defines the path to the virtual machine autostart configuration.

These variables can be defined as;

VBOXAUTOSTART_DB=/etc/vbox
VBOXAUTOSTART_CONFIG=/etc/vbox/autostartvm.cfg

To place these variables in the /etc/default/virtualbox, run the command below;

echo -e "VBOXAUTOSTART_DB=/etc/vbox\nVBOXAUTOSTART_CONFIG=/etc/vbox/autostartvm.cfg" | sudo tee /etc/default/virtualbox

Define the virtual machine autostart configuration settings. The autostart configuration file contains options that controls how the virtual machine is auto started.

sudo vim /etc/vbox/autostartvm.cfg

default_policy = deny

kifarunix = {
    allow = true
    startup_delay = 10
}
  • default_policy – defines whether to allow or deny the virtual machine autostart by default. In our example above, we denied any one from auto-starting the VM and explicitly allow one user, amos.
  • username (kifarunix) – with the default deny policy, you can define the specific users that are allowed to autostart the virtual machine (allow = trues). You can also define how long to delay the VM startup. 10 seconds is used in this demo.

Set Ownership of Database directory

The database directory, /etc/vbox, should be writable by the user to be used to start VMs automatically. To make it easy, you can simply add your user to vboxusers group and set the group ownership of the database directory to vboxusers group. After that, set the write permissions for the group. In this case, amos user is to be used to automatically start the virtual machine.

sudo usermod -aG vboxusers kifarunix
sudo chgrp vboxusers /etc/vbox

Assign the group write permissions on the autostart database directory.

sudo chmod g+w /etc/vbox

To shield the directory from being modified or deleted by other users except the owner or the root user, set sticky bit.

sudo chmod +t /etc/vbox

Enable Virtual Machine Autostart

As a user, you can enable autostart for individual machines. This requires that you define the path to the database directory first.

VBoxManage setproperty autostartdbpath /etc/vbox/

Once that is done, you can now setup the virtual machine to automatically start on system boot.

vboxmanage modifyvm fedora30 --autostart-enabled on

Where fedora30 is the name of your virtual machine. You can also use UUID instead.

You can get the names/UUIDs of the vms by running the command below;

vboxmanage list vms

Restart  vboxautostart-service

The configuration is now done. To effect the settings, you need to restart the  vboxautostart-service.

sudo systemctl restart vboxautostart-service

Testing the Virtual Machine autostart

To test that your virtual machine can actually autostart on system boot, reboot you system and check. When system boots, your VM should now be running.

If you which to disable the virtual machine autostart;

vboxmanage modifyvm fedora30 --autostart-enabled off

Using Systemd service unit

Similarly, you can auto-start a VirtualBox by a creating a systemd service that starts the respective VM when system reboots.

To use this approach, just create the systemd unit service as follows (replace the name of the vm accordingly);

sudo vim /etc/systemd/system/fedora30-vm.service

[Unit]
Description=Autostart VirtualBox VM
After=network.target vboxdrv.service

[Service]
User=kifarunix
ExecStart=/usr/bin/vboxheadless -s fedora30
ExecStop=/usr/bin/VBoxManage controlvm fedora30 acpipowerbutton

[Install]
WantedBy=multi-user.target

Replace the User=kifarunix, with your correct username.

Reload the systemd daemon to read the new unit file:

sudo systemctl daemon-reload

Enable the service to start automatically on boot:

sudo systemctl enable vmname.service

Reboot the system and confirm if all is good.

Autostart VirtualBox VM using System Cronjob

It is also possible to use cronjob to automatically start a virtualbox machine on system reboot.

Simply, as user with which you want to start the VirtualBox vm as, create a cronjob as follows;

crontab -e

Enter this line to start your VM. Replace the names accordingly;

@reboot sleep 60 && vboxheadless --startvm "fedora30" &

The command simply starts the vm 1 minute after the system boots.

That is all! You have learnt how to automatically start VirtualBox VMs on system boot on Linux using the following methods;

  1. Using VirtualBox Autostart Service
  2. Using Systemd service unit
  3. Using system cronjobs

Resize Root Partition of VirtualBox VM Snapshot

Install VirtualBox Guest Additions on Debian 10 Buster

Install Debian 10 Buster on VirtualBox

Install and Configure Endian Firewall on VirtualBox

How to Upgrade VirtualBox 5.2 to VirtualBox 6.0 on Ubuntu 16.04

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

14 thoughts on “AutoStart VirtualBox VMs on System Boot on Linux”

  1. Your directions are great! Thanks! Much, much better than the official VirtualBox documentation (see “9.23.1. Linux: Starting the Autostart Service With init”) which very, very briefly mentions about one tenth of what you mentioned above.

    Reply
  2. You have an error in your
    echo -e “VBOXAUTOSTART_DB=/etc/vbox\nVBOXAUTOSTART_CONFIG=/etc/vbox/autostart.cfg” | sudo tee /etc/default/virtualbox

    you have autostart.cfg but you name the file autostartvm.cfg

    Reply
  3. Hi all
    Can you help me?
    I have issue with autostart my vm(ls: cannot access ‘/etc/vbox/*.start’: No such file or directory).

    “vboxautostart-service.service
    Loaded: loaded (/usr/lib/virtualbox/vboxautostart-service.sh; enabled; vendor preset: enabled)
    Active: active (exited) since Wed 2020-03-18 10:05:12 CET; 13s ago
    Process: 14472 ExecStart=/usr/lib/virtualbox/vboxautostart-service.sh start (code=exited, status=0/SUCCESS)
    Mar 18 10:05:12 is3 systemd[1]: Starting vboxautostart-service.service…
    Mar 18 10:05:12 is3 vboxautostart-service.sh[14472]: vboxautostart-service.sh: Starting VirtualBox VMs configured for autostart.
    Mar 18 10:05:12 is3 vboxautostart-service.sh[14472]: ls: cannot access ‘/etc/vbox/*.start’: No such file or directory
    Mar 18 10:05:12 is3 systemd[1]: Started vboxautostart-service.service.”

    Anyone have an idea where is issue?
    Thank you

    Reply
  4. On ubuntu 20 and having issues getting this working.
    I am able to trigger it to start by manually restarting the service using

    sudo systemctl restart vboxautostart-service

    However, when rebooting it does not start on its own.
    Any advice on where and what to look for logs?
    I do get a notification in syslogs that “Starting VirtualBox VMs configured for autostart.”

    Reply
  5. Hi

    I get error Failed to restart vboxautostart-service.service: Unit vboxautostart-service.service not found.

    when doing the last step – any tips?

    Reply
    • hmm, please check if virtualbox-guest-utils package is installed. If not just install it.
      You can also use other methods like using systemd or cronjob described in the guide.

      Reply

Leave a Comment