{"id":3833,"date":"2019-08-03T00:06:52","date_gmt":"2019-08-02T21:06:52","guid":{"rendered":"https:\/\/kifarunix.com\/?p=3833"},"modified":"2024-03-12T07:47:14","modified_gmt":"2024-03-12T04:47:14","slug":"autostart-virtualbox-vms-on-system-boot-on-linux","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/autostart-virtualbox-vms-on-system-boot-on-linux\/","title":{"rendered":"AutoStart VirtualBox VMs on System Boot on Linux"},"content":{"rendered":"\n<p>In this guide, we are going to learn how to <a aria-label=\"autostart VirtualBox VMs on system boot (opens in a new tab)\" href=\"https:\/\/www.virtualbox.org\/manual\/ch09.html#autostart\" target=\"_blank\" rel=\"noreferrer noopener\">autostart VirtualBox VMs on system boot<\/a> on Linux. On a Linux system with VirtualBox installed, you can start VMs automatically during system boot.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#automatically-start-virtual-box-v-ms-on-system-boot-on-linux\">Automatically Start VirtualBox VMs on System Boot on Linux<\/a><ul><li><a href=\"#using-virtual-box-autostart-service\">Using VirtualBox Autostart Service<\/a><ul><li><a href=\"#set-ownership-of-database-directory\">Set Ownership of Database directory<\/a><\/li><li><a href=\"#enable-virtual-machine-autostart\">Enable Virtual Machine Autostart<\/a><\/li><li><a href=\"#restart-vboxautostart-service\">Restart \u00a0vboxautostart-service<\/a><\/li><li><a href=\"#testing-the-virtual-machine-autostart\">Testing the Virtual Machine autostart<\/a><\/li><\/ul><\/li><li><a href=\"#using-systemd-service-unit\">Using Systemd service unit<\/a><\/li><li><a href=\"#autostart-virtual-box-vm-using-system-cronjob\">Autostart VirtualBox VM using System Cronjob<\/a><\/li><li><a href=\"#related-tutorials\">Related Tutorials;<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"automatically-start-virtual-box-v-ms-on-system-boot-on-linux\">Automatically Start VirtualBox VMs on System Boot on Linux<\/h2>\n\n\n\n<p>There are multiple ways in which you can configure your VirtualBox vms to automatically start on system boot.<\/p>\n\n\n\n<p>A few of these ways that we will discuss in this guide include;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"#vbox-autostart-service\">Using VirtualBox Autostart Service<\/a><\/li>\n\n\n\n<li><a href=\"#using-systemd-service\">Using Systemd service unit<\/a><\/li>\n\n\n\n<li><a href=\"#using-cronjob\">Using system cronjobs<\/a><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"using-virtual-box-autostart-service\">Using VirtualBox Autostart Service<\/h3>\n\n\n\n<p>VirtualBox comes with a service called <strong>vboxautostart-service <\/strong>that makes it easy to automatically start virtual machines during system reboot.<\/p>\n\n\n\n<p>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 <strong>\/etc\/default\/virtualbox<\/strong>;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>VBOXAUTOSTART_DB<\/strong> &#8211; which defines the absolute path to the autostart database directory, usually the <strong>\/etc\/vbox<\/strong>.<\/li>\n\n\n\n<li><strong>VBOXAUTOSTART_CONFIG<\/strong> &#8211; defines the path to the virtual machine autostart configuration.<\/li>\n<\/ul>\n\n\n\n<p>These variables can be defined as;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VBOXAUTOSTART_DB=\/etc\/vbox\nVBOXAUTOSTART_CONFIG=\/etc\/vbox\/autostartvm.cfg<\/code><\/pre>\n\n\n\n<p>To place these variables in the <strong>\/etc\/default\/virtualbox<\/strong>, run the command below;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo -e \"VBOXAUTOSTART_DB=\/etc\/vbox\\nVBOXAUTOSTART_CONFIG=\/etc\/vbox\/autostartvm.cfg\" | sudo tee \/etc\/default\/virtualbox<\/code><\/pre>\n\n\n\n<p>Define the virtual machine autostart configuration settings. The autostart configuration file contains options that controls how the virtual machine is auto started.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo vim \/etc\/vbox\/autostartvm.cfg<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\ndefault_policy = deny\n\nkifarunix = {\n    allow = true\n    startup_delay = 10\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>default_policy<\/strong> &#8211; 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.<\/li>\n\n\n\n<li><strong>username<\/strong> (kifarunix) &#8211; with the default deny policy, you can define the specific users that are allowed to autostart the virtual machine (<strong>allow = trues<\/strong>). You  can also define how long to delay the VM startup. 10 seconds is used in this demo.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"set-ownership-of-database-directory\">Set Ownership of Database directory<\/h4>\n\n\n\n<p>The database directory, <strong>\/etc\/vbox<\/strong>, should be writable by the user to be used to start VMs automatically. To make it easy, you can simply add your user to <strong>vboxusers<\/strong> 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.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo usermod -aG vboxusers kifarunix<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo chgrp vboxusers \/etc\/vbox<\/code><\/pre>\n\n\n\n<p>Assign the group write permissions on the autostart database directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo chmod g+w \/etc\/vbox<\/code><\/pre>\n\n\n\n<p>To shield the directory from being modified or deleted by other users except the owner or the root user, set sticky bit.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo chmod +t \/etc\/vbox<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"enable-virtual-machine-autostart\">Enable Virtual Machine Autostart<\/h4>\n\n\n\n<p>As a user, you can enable autostart for individual machines. This requires that you define the path to the database directory first.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VBoxManage setproperty autostartdbpath \/etc\/vbox\/<\/code><\/pre>\n\n\n\n<p>Once that is done, you can now setup the virtual machine to automatically start on system boot.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vboxmanage modifyvm fedora30 --autostart-enabled on<\/code><\/pre>\n\n\n\n<p>Where <strong>fedora30<\/strong> is the name of your virtual machine. You can also use UUID instead.<\/p>\n\n\n\n<p>You can get the names\/UUIDs of the vms by running the command below;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vboxmanage list vms<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"restart-vboxautostart-service\">Restart &nbsp;vboxautostart-service<\/h4>\n\n\n\n<p>The configuration is now done. To effect the settings, you need to restart the &nbsp;<strong>vboxautostart-service<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo systemctl restart vboxautostart-service<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"testing-the-virtual-machine-autostart\">Testing the Virtual Machine autostart<\/h4>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>If you which to disable the virtual machine autostart;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vboxmanage modifyvm fedora30 --autostart-enabled off<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"using-systemd-service-unit\">Using Systemd service unit<\/h3>\n\n\n\n<p>Similarly, you can auto-start a VirtualBox by a creating a systemd service that starts the respective VM when system reboots.<\/p>\n\n\n\n<p>To use this approach, just create the systemd unit service as follows (replace the name of the vm accordingly);<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vim \/etc\/systemd\/system\/fedora30-vm.service<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n[Unit]\nDescription=Autostart VirtualBox VM\nAfter=network.target vboxdrv.service\n\n[Service]\nUser=kifarunix\nExecStart=\/usr\/bin\/vboxheadless -s fedora30\nExecStop=\/usr\/bin\/VBoxManage controlvm fedora30 acpipowerbutton\n\n[Install]\nWantedBy=multi-user.target\n<\/code><\/pre>\n\n\n\n<p>Replace the <strong><code>User=kifarunix<\/code><\/strong>, with your correct username.<\/p>\n\n\n\n<p>Reload the systemd daemon to read the new unit file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl daemon-reload<\/code><\/pre>\n\n\n\n<p>Enable the service to start automatically on boot:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo systemctl enable vmname.service<\/code><\/pre>\n\n\n\n<p>Reboot the system and confirm if all is good.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"autostart-virtual-box-vm-using-system-cronjob\">Autostart VirtualBox VM using System Cronjob<\/h3>\n\n\n\n<p>It is also possible to use cronjob to automatically start a virtualbox machine on system reboot.<\/p>\n\n\n\n<p>Simply, as user with which you want to start the VirtualBox vm as, create a cronjob as follows;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -e<\/code><\/pre>\n\n\n\n<p>Enter this line to start your VM. Replace the names accordingly;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@reboot sleep 60 &amp;&amp; vboxheadless --startvm \"fedora30\" &amp;<\/code><\/pre>\n\n\n\n<p>The command simply starts the vm 1 minute after the system boots.<\/p>\n\n\n\n<p>That is all! You have learnt how to automatically start VirtualBox VMs on system boot on Linux using the following methods;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Using VirtualBox Autostart Service<\/li>\n\n\n\n<li>Using Systemd service unit<\/li>\n\n\n\n<li>Using system cronjobs<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"related-tutorials\">Related Tutorials;<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/resize-root-partition-of-virtualbox-vm-snapshot\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Resize Root Partition of VirtualBox VM Snapshot<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-virtualbox-guest-additions-on-debian-10-buster\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Install VirtualBox Guest Additions on Debian 10 Buster<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-debian-10-buster-on-virtualbox\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Install Debian 10 Buster on VirtualBox<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-and-configure-endian-firewall-on-virtualbox\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Install and Configure Endian Firewall on VirtualBox<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-upgrade-virtualbox-5-2-to-virtualbox-6-0-on-ubuntu-16-04\/\">How to Upgrade VirtualBox 5.2 to VirtualBox 6.0 on Ubuntu 16.04<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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,<\/p>\n","protected":false},"author":1,"featured_media":15915,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[36,121,46],"tags":[6473,6472,6474,1057,1058,24],"class_list":["post-3833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-virtualization","category-howtos","category-virtualbox","tag-auto-start-virtualbox-vm-using-cronjob","tag-auto-start-virtualbox-vm-using-systemd","tag-autostart-virtualbox-vms-on-system-boot-on-linux","tag-autostart-vm","tag-vboxautostart-service","tag-virtualbox","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/3833"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=3833"}],"version-history":[{"count":17,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/3833\/revisions"}],"predecessor-version":[{"id":21190,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/3833\/revisions\/21190"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/15915"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=3833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=3833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=3833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}