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?
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.
Comments