VirtualBox普通用户无法检测运行虚拟机及开机自启服务启动失败问题求助
VirtualBox普通用户无法检测运行虚拟机及开机自启服务启动失败问题求助
我最近遇到了VirtualBox的权限相关问题,折腾了好久都没解决,想请教大家:
一、普通用户与sudo执行命令的差异
- 当我以普通用户身份执行
VBoxManage list runningvms时,没有任何输出——既看不到运行的虚拟机,也没有错误提示,完全是空的; - 但用sudo执行
sudo VBoxManage list runningvms时,能正常看到我的虚拟机正在运行,输出内容如下:"homeassistant" {5e97e033-e726-41ec-a3aa-3f8e6b4bf448}
二、我已经尝试过的操作
这个普通用户已经是vboxusers组的成员,除此之外我还做了这些调整:
- 删除用户目录下的VirtualBox配置文件夹:
rm -r ~/.config/VirtualBox - 用sudo重新设置该文件夹的权限:
sudo chmod -R 752 ~/.config/VirtualBox
三、开机自启服务的问题
我希望虚拟机能在系统重启后自动启动,所以创建了一个自定义systemd服务:
- 服务文件:路径为
/etc/systemd/system/ha.service,内容如下:[Unit] Description=vm1 After=network.target virtualbox.service Before=runlevel2.target shutdown.target [Service] User=ovm Group=ovm Type=forking Restart=no TimeoutSec=5min RemainAfterExit=yes ExecStart=/bin/bash /home/hans/startha.sh [Install] WantedBy=multi-user.target - 启动脚本:路径为
/home/hans/startha.sh,内容如下:#! /bin/bash sleep 40 VBoxManage startvm homeassistant --type headless
之后我执行了这些操作来启用服务:
- 重新加载systemd配置:
sudo systemctl daemon-reload - 启用服务:
systemctl enable ha.service
但重启系统后过了几分钟检查,发现服务启动失败,提示普通用户没有权限启动虚拟机。执行 systemctl status ha 得到的输出如下:
ha.service - vm1 Loaded: loaded (/etc/systemd/system/ha.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Tue 2023-04-18 13:41:08 EDT; 5min ago Process: 1119 ExecStart=/bin/bash /home/hans/startha.sh (code=exited, status=217/USER) CPU: 1ms
补充信息
- 我的系统是不带GUI的Debian,虚拟机完全是通过终端配置的,用sudo启动时一切正常;
- 另外,当我以用户hans的身份执行
sudo ./startha.sh时,虚拟机可以正常启动。
想请教大家,这种情况该怎么处理才能让普通用户正常检测到运行的虚拟机,同时让开机自启服务正常工作呢?
备注:内容来源于stack exchange,提问作者Hans Van Den Broek
相关产品推荐
相关产品推荐

