美文网首页
ubuntu22.04下virtualbox设置开机自启某个虚拟

ubuntu22.04下virtualbox设置开机自启某个虚拟

作者: haiyong6 | 来源:发表于2023-02-11 13:05 被阅读0次

    最近打算自己弄一台服务器,打算用virtualbox多开虚拟机,virtualbox还是很强大的,记录下virtualbox开机自启。

    设置前需知

    vname:替换成要启动的虚拟机的名字
    user: 替换成当前用户名

    在/etc/default中新建virtualbox文件

    sudo vim /etc/default/virtualbox
    

    填入:

    VBOXAUTOSTART_DB=/etc/vbox
    VBOXAUTOSTART_CONFIG=/etc/vbox/autostart.conf
    

    设置vbox目录的权限,添加user用户到vboxusers用户组

    sudo chgrp vboxusers /etc/vbox
    
    sudo chmod 1775 /etc/vbox
    
    sudo adduser user vboxusers
    

    新建虚拟机自动启动配置文件autostart.conf

    sudo vim /etc/vbox/autostart.conf
    

    填入:

    # Default policy is to deny starting a VM, the other option is "allow".
    default_policy = deny
    
    # user vbox is allowed to start virtual machines but starting them
    # will be delayed for 10 seconds
    user = {
    allow = true
    startup_delay = 10
    }
    

    设置开启自动启动

    设置成功后会在/etc/vbox目录下生成user.start文件。

    VBoxManage setproperty autostartdbpath /etc/vbox
    VBoxManage modifyvm vname --autostart-enabled on

    到这里自启就设置成功了 利用virtualbox自带的自启脚本实现的,自启脚本在下面位置

    /etc/systemd/system/multi-user.target.wants/vboxautostart-service.service
    /usr/lib/virtualbox/vboxautostart-service.sh
    

    可以重启下试试

    下面也可自己设置自启服务,但推荐上面这个。

    也可设置systemctl开启自动启动

    编写启动服务文件
    sudo vim /etc/systemd/system/vname.service
    

    填入:

    [Unit]
    Description=vname
    After=network.target virtualbox.service
    Before=runlevel2.target shutdown.target
    [Service]
    User=user
    Group=vboxusers
    Type=forking
    Restart=no
    TimeoutSec=5min
    IgnoreSIGPIPE=no
    KillMode=process
    GuessMainPID=no
    RemainAfterExit=yes
    ExecStart=/usr/bin/VBoxManage startvm vname --type headless
    ExecStop=/usr/bin/VBoxManage controlvm vname acpipowerbutton
    [Install]
    WantedBy=multi-user.target
    

    可以看到上面设置了开机和关机虚拟机的命令

    Reload Daemon
    sudo systemctl daemon-reload
    
    测试状态
    sudo systemctl status vname
    
    启动服务
    sudo systemctl start vname
    
    设置允许自启
    sudo systemctl enable vname
    
    停止服务
    sudo systemctl stop vname
    
    关闭自启
    sudo systemctl disable vname
    

    更多命令可以看这篇博文:VBoxManage 命令详解

    参考:https://blog.51cto.com/zhangnq/5624066
    https://www.paulligocki.com/make-virtual-box-vm-autostart/

    相关文章

      网友评论

          本文标题:ubuntu22.04下virtualbox设置开机自启某个虚拟

          本文链接:https://www.haomeiwen.com/subject/ksujfdtx.html