Make Virtual Box VM Autostart


Linux

Ubuntu 20.04

What you need to know

  • VMNAME : Virtual Manchine Name that is registered with VirtualBox
  • user : Account to run as

Edit /etc/default/virtualbox

sudo nano /etc/default/virtualbox

Add the following few lines into the file

#AutoStarting VMs
VBOXAUTOSTART_DB=/etc/vbox
VBOXAUTOSTART_CONFIG=/etc/vbox/autostart.cfg

Create /etc/systemd/system/VMNAME.service

sudo nano /etc/systemd/system/VMNAME.service
[Unit]
Description=vm1
After=network.target virtualbox.service
Before=runlevel2.target shutdown.target
[Service]
User=user
Group=vboxusers
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/usr/bin/VBoxManage startvm VMNAME --type headless
ExecStop=/usr/bin/VBoxManage controlvm VMNAME acpipowerbutton
[Install]
WantedBy=multi-user.target

Reload Daemon

sudo systemctl daemon-reload

Test

systemctl status VMNAME

Start

sudo systemctl start VMNAME

Stop

sudo systemctl stop VMNAME

Enable

sudo systemctl enable VMNAME

Disable

sudo systemctl disable VMNAME

,