I created 2 VMs using VirtualBox and I can start them fine as my own user using the following command:

vboxmanage startvm myVM -type headless

but when I try the same command with sudo, or in a systemd service, it returns that it can't find the VM.

I found out this

It fails because you are using sudo. VirtualBox is designed to be run by any user (in the vboxusers group), and sudo runs the command as the root user whose VirtualBox configuration is empty.

But I just don't know how to make it so when run as root (with sudo) to have virtualbox settings present.

Any idea?

  • The end objective here is to auto start the VMs and gracefully terminate them on shutdown. – Martin B May 19 '18 at 14:47
  • Related possible duplicate: askubuntu.com/questions/128413/… – Elder Geek May 19 '18 at 15:19
  • Does the root user have a Virtualbox configuration? – Elder Geek May 19 '18 at 15:20
  • It's a fresh ubuntu install, so I doubt the root user have a virtualbox configuration. How would I do that? – Martin B May 19 '18 at 15:22
  • The same way you configured it for your user account, but as root. virtualbox.org/manual/ch01.html might be useful to you. You can clone your existing VM's if so desired. – Elder Geek May 19 '18 at 15:24

To start a VM on login and have it stop on logout, you can use systemd user units.

Create a file `~/.config/systemd/user/myvm.service with the following content:

Description=My VM

ExecStart=vboxmanage startvm myVM -type headless
ExecStop=vboxmanage controlvm myVM acpipowerbutton 
# Actually, I'm not sure about the stop command, but I'm confident you will figure that one out


Afterwards, run systemctl --user enable myvm.service. You can now start your VM with systemctl --user start myvm.service and stop it with systemctl --user stop myvm.service. Logging in and out should start and stop your VM.

If you need this on boot instead of log in, you can try to make this a system systemd unit and specify the executing user in the [Service] section with User= and Group=.

  • Thanks! I had tried setting my username in the services' unit, without luck. Adding the RemainAfterExit saved the day! Thanks a LOT! – Martin B May 23 '18 at 2:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.