when starting virtualbox vm using sudo, it can't find the VMs

Martin B

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?

Lienhart Woitok

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:

[Unit]
Description=My VM

[Service]
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
RemainAfterExit=true

[Install]
WantedBy=default.target

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=.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

VirtualBox fails to attach driver when starting VM

Can't start a virtualbox VM

How can I relocate or disable the VirtualBox logfile when starting a vm from Vagrant?

Brooklyn Starting Blueprint: VMs created but Brooklyn can't SSH in

Can't restore VM in Python Virtualbox API

Can't find installed virtualbox

ubuntu VM gets stuck and can't reach login page when launching on virtualbox (Windows host OS)

Can't Access Apache on VirtualBox VM (using NAT + Host-only)

Oracle VirtualBox VMs are automatically starting at login, but not sure how

virtualbox/docker/VMs - Is it possible to use the VM's PHP from the host?

Can powercli tell me what the VM Name is when listing network adapters for vms?

Virtualbox can't find USB devices

Can I recover data that disappeared when deleting a VirtualBox VM snapshot?

Virtualbox -> Can't add 2nd adapter to network for VM

Can't resize QEMU VMs

Can't find application class in @INC when starting Mojolicious-generated app

Can't find io.jsonwebtoken.impl.DefaultJwtBuilder when starting project in a docker container

Ubuntu freezes when starting virtualbox

Can't find exact match in OS type when creating new VM in VMware Workstation

Can't install or find python-pip and python-requests using sudo apt-get install

When a VirtualBox VM's Adapter is set to NAT can a VM access the Host computer?

When was my VM in VirtualBox created?

VirtualBox VM won't boot

When using the randint() function in python, I can't set the starting value at 1000?

Problem when trying to install a service for running Virtualbox VMs headless

VM very slow using VirtualBox

Can't source .bashrc from .bash_profile when using `sudo su - username -c`

vim: E212: Can't open file for writing (even when using sudo)

Mocha can't find my test files when using TypeScript