VirtualBox: How to Set VMs to Start on Linux Automatically

Set VirtualBox VMs to start automatically on Linux. Simple, effective methods to ensure your VMs are ready at boot.

VirtualBox is powerful yet free virtualization software that allows users to run multiple operating systems on a single machine. It is a versatile tool for testing, developing, or exploring new OS environments.

However, one feature that can significantly enhance the convenience and efficiency of using VirtualBox is the ability to set virtual machines (VMs) to start automatically on a Linux host system.

VM Autostart: Use Cases & Benefits

Imagine having a server running on a VM that needs to be up and running 24/7, or you’re developing an application that requires frequent testing across different operating systems.

In these scenarios and many others, the ability to auto-start VMs upon system boot can save you a tremendous amount of time and hassle.

Furthermore, you no longer need to manually start each VM every time your host system restarts. With a few simple configurations, your VMs can spring to life automatically and be ready for use whenever needed.

VirtualBox: How to Autostart VMs on Linux Boot

This article will guide you through setting up your VMs to start automatically on a Linux host system. So, let’s dive in and unlock the full potential of VirtualBox, making your virtualization experience smoother and more efficient.

Add User to vboxusers Group

Before we move on to our main task, we need to check a few things. The most important one is ensuring that the user with whose permissions the VirtualBox VMs will be automatically started at boot must be a “vboxusers” group member.

We assume that this is your user account that you’re using for all your activities on your Linux desktop. To see which groups it’s a part of, you can run this command:

groups $USERCode language: PHP (php)
Check which groups the user belongs to.
Check which groups the user belongs to.

You should see “vboxusers” listed in the results, like in the image above. If you don’t see it, don’t worry! You can quickly add your user to this group by using the following command:

sudo usermod -a -G vboxusers $USERCode language: PHP (php)

Restart your computer for the changes to take effect. Once you log in again, ensure your user account has been added to the “vboxusers” group. Then, we can proceed.

Create the Needed Systemd Services

To achieve our goal, namely, having our VirtualBox virtual machines start on their own after the system reboots, we will use some functionalities provided by systemd. As I’ll explain, it’s pretty simple to do.

Using your preferred terminal text editor, create a “[email protected]” file inside the “/etc/systemd/system/” directory. Then, copy and paste the following content into it.

sudo vim /etc/systemd/system/[email protected]
[Unit]
Description=VBox Virtual Machine %i Service
Requires=systemd-modules-load.service
After=systemd-modules-load.service

[Service]
User=linuxiac
Group=vboxusers
ExecStart=/usr/bin/VBoxManage startvm %i --type headless
ExecStop=/usr/bin/VBoxManage controlvm %i acpipowerbutton
RemainAfterExit=yes

[Install]
WantedBy=multi-user.targetCode language: JavaScript (javascript)

Don’t forget in the “User=…” part to replace the user with the one you use.

But let’s now break down what we just did, especially for those who might not be very familiar with systemd. What’s the meaning of the “@” symbol in the filename?

In systemd, “@” denotes an instance name for a templated unit file. Templated unit files allow you to create services that can be started with different instances, each with a separate configuration or environment.

This is particularly useful when you want to run multiple instances of the same service, each configured slightly differently, without having to create a separate unit file for each instance. That is exactly our case.

Now, let’s list the available VirtualBox virtual machines:

VBoxManage list vmsCode language: PHP (php)
List VirtualBox's VMs.
List VirtualBox’s VMs.

As we can see from the image above, we have two options here: “debian” and “ubuntu.” We mentioned these names because we’ll need to pick one for the next systemd service we’re making. We’ve decided to go with “ubuntu.”

To set it to start automatically, just run the command below. After the “@” symbol, add the name you picked from the list of VirtualBox virtual machines. For us, that name is “ubuntu“.

sudo systemctl enable vboxvmservice@ubuntuCode language: CSS (css)
Set VirtualBox virtual machine to start automatically.
Set VirtualBox virtual machine to start automatically.

Аs you can see, our new systemd service, “vboxvmservice@ubuntu“, has been automatically linked to “[email protected]“. This means that whatever follows the “@” symbol, in this case “ubuntu“, will be used in the service and replaced in the place of the “%i” variable.

Finally, reload systemd’s configuration files:

sudo systemctl daemon-reload

Testing VirtualBox’s VM Autostart

Okay, it’s the big moment. Go ahead and restart your Linux system. When you log back in, you’ll find your VirtualBox’s VM running. Congratulations on a job well done!

Successful auto-start of VirtualBox virtual machine.
Successful auto-start of VirtualBox virtual machine.

Additionally, you can check the status of the systemd service by running:

sudo systemctl status vboxvmservice@ubuntuCode language: CSS (css)

Conclusion

Configuring VirtualBox virtual machines to start automatically on a Linux system offers significant advantages for users looking for an efficient and streamlined virtualization experience.

Successfully implementing this setup means no longer needing to manually start each VM upon the host system’s restart, thus saving time and reducing manual intervention.

Furthermore, this method ensures that essential services running on VMs are consistently available, making it an invaluable addition to any VirtualBox user’s toolkit.

Also, if you’re considering setting up VirtualBox on a remote server to make it the heart of your virtual setup and need help managing it remotely, our guide “How to Setup and Manage VMs on a Headless VirtualBox Server” is all you need.

Lastly, as always, I’d love to hear your feedback or thoughts, so please don’t hesitate to share them in the comment box below.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%

Leave a Reply

Your email address will not be published. Required fields are marked *