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 $USER
Code language: PHP (php)
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 $USER
Code 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.target
Code 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 vms
Code language: PHP (php)
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@ubuntu
Code language: CSS (css)
ะ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!
Additionally, you can check the status of the systemd service by running:
sudo systemctl status vboxvmservice@ubuntu
Code 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.