centos

VirtualBox虚拟机如何设置启动项

小樊
53
2025-07-24 15:37:29
栏目: 云计算

在VirtualBox中设置虚拟机的启动项可以通过以下几种方法实现:

通过VirtualBox管理器设置启动项

  1. 打开VirtualBox管理器,选择你想要设置启动项的虚拟机。
  2. 点击虚拟机名称旁边的“设置”按钮。
  3. 在设置窗口中,选择“存储”选项卡。
  4. 在“存储树”中,选择“控制器:SATA”或“控制器:IDE”(取决于你的虚拟机配置)。
  5. 右键点击空白区域,选择“添加光驱”,然后选择“选择或创建一个光盘映像”。
  6. 浏览并选择你的ISO文件,点击“确定”添加到虚拟机。
  7. 点击“确定”保存设置。

通过systemd服务设置启动项(适用于Linux系统)

  1. 使用SSH登录到虚拟机。
  2. 创建或编辑自启动服务文件,例如,创建文件 /etc/systemd/system/auto_start_ubuntu.service
[Unit]
Description=Start Ubuntu Virtual Machine After=network.target virtualbox.service Before=runlevel2.target shutdown.target

[Service]
User=your_username
Group=vboxusers
Type=forking
Restart=no
TimeoutSec=5 min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPid=no
RemainAfterExit=yes
ExecStart=/usr/bin/VBoxManage startvm Ubuntu --type headless
ExecStop=/usr/bin/VBoxManage controlvm Ubuntu acpipowerbutton
  1. 设置开机启动服务:
sudo systemctl daemon-reload
sudo systemctl enable auto_start_ubuntu.service
  1. 取消开机启动服务(如果需要):
sudo systemctl disable auto_start_ubuntu.service
  1. 重启电脑,检查虚拟机是否自动启动。

通过配置文件设置(适用于CentOS系统)

  1. 在CentOS虚拟机中,编辑 /etc/default/virtualbox 文件,添加以下两行:
VBOXAUTOSTART_DB=/etc/vbox
VBOXAUTOSTART_CONFIG=/etc/vbox/vboxauto.conf
  1. 编辑 /etc/vbox/vboxauto.conf 文件,添加启动用户信息:
default_policy denywx {
    allow true
}
  1. wx 替换为实际用户,并确保该用户已加入 vboxusers 组。
  2. 修改 /etc/vbox 目录权限,将用户添加到 vboxusers 组:
usermod -G vboxusers wx
chgrp vboxusers /etc/vbox
chmod 1775 /etc/vbox
  1. 使用 VBoxManage 命令设置自动启动数据库路径:
VBoxManage setproperty autostartdbpath /etc/vbox
  1. 重启 vboxauto 服务:
systemctl restart vboxautostart-service
systemctl enable vboxautostart-service

完成以上步骤后,重启主机,虚拟机将会自动运行。

请注意,具体的步骤可能会因VirtualBox版本或操作系统的不同而有所差异。如果遇到问题,建议查阅VirtualBox和操作系统的官方文档或寻求社区帮助。

0
看了该问题的人还看了