Linux vagrant 导入Centos到virtualbox

前言

  • vagrant 导入centos 虚拟机

前提要求

  • 安装 virtualbox 和vagrant<vagrant-disksize> (Linux 方式 Windows 方式)

创建一键部署centos 虚拟机

  • /opt/vagrant 安装目录

  • /opt/VirtualBox 安装目录

  • /opt/centos8/Vagrantfile (可配置网络IP,内存,cpu,磁盘及分区,启动项,虚拟化开启,usb3.0,安装软件) 参考

  • 用户名密码 root:1234@com,vagrant:1234@com 远程连接端口 2220

  • 虚拟机内部挂载了分区/opt/ 90G ,根据主机需要更改即可 63行 66 行是增加新分区

  • /usr/lib/systemd/system/vagrant-autostart.service 开机启动脚本

  • /usr/bin/vagrant_start.sh 启动脚本,可以执行批量启动关闭

  • vagrant_stop.sh 关闭脚本,可以执行批量启动关闭

  • /var/log/vagrant_startup.log 启动日志记录

  • /var/log/vagrant_shutdown.log 关闭日志记录

  • box 导入参数参考

  • 导出参考

    vim /vagrant_import_centos.sh

    #!/bin/bash

    -- coding: utf-8 --

    Author: make.han

    Date: 2025/04/10

    vagrant centos

    <<!

    vagrant vm

    https://portal.cloud.hashicorp.com/vagrant/discover

    vagrant docs

    https://developer.hashicorp.com/vagrant/docs

    vagrant download

    https://releases.hashicorp.com/vagrant

    vagrant gitlab

    https://github.com/hashicorp/vagrant/releases

    vagrantfile config

    https://developer.hashicorp.com/vagrant/docs/vagrantfile/machine_settings

    vagrant 常用命令参考

    vagrant init # 初始化
    vagrant up # 启动虚拟机
    vagrant halt # 关闭虚拟机
    vagrant reload # 重启虚拟机
    vagrant ssh # SSH 至虚拟机
    vagrant suspend # 挂起虚拟机
    vagrant resume # 唤醒虚拟机
    vagrant status # 查看虚拟机运行状态
    vagrant destroy # 销毁当前虚拟机

    #box管理命令
    vagrant box list # 查看本地box列表
    vagrant box add # 添加box到列表
    vagrant box remove # 从box列表移除
    !

    echo "create centos directory"
    mkdir -p /opt/centos8
    cd /opt/centos8

    echo "add init config"
    cat <<'EOF'>>/opt/centos8/Vagrantfile

    -- mode: ruby --

    vi: set ft=ruby :

    Vagrant.configure("2") do |config|
    # The most common configuration options are documented and commented below.
    # For a complete reference, please see the online documentation at
    # https://docs.vagrantup.com.

    复制代码
    # Every Vagrant development environment requires a box. You can search for
    # boxes at https://vagrantcloud.com/search.
    config.vm.box = "centos8.3"
    
    # hosts name
    config.vm.hostname = "centos8"
    
    # 默认启动磁盘大小
    config.vm.disk :disk, size: "100GB", primary: true
    
    # Add new disks (需要安装插件vagrant plugin install vagrant-disksize)
    #config.disksize.size = "80GB"
    
    # Set the guest operating system type (:linux :windows)
    config.vm.guest = :linux
    
    # Disable automatic box update checking. If you disable this, then
    # boxes will only be checked for updates when the user runs
    # `vagrant box outdated`. This is not recommended.
    # config.vm.box_check_update = false
    
    # Create a forwarded port mapping which allows access to a specific port
    # within the machine from a port on the host machine. In the example below,
    # accessing "localhost:8080" will access port 80 on the guest machine.
    # NOTE: This will enable public access to the opened port
    # config.vm.network "forwarded_port", guest: 80, host: 8080
    
    # Create a forwarded port mapping which allows access to a specific port
    # within the machine from a port on the host machine and only allow access
    # via 127.0.0.1 to disable public access
    # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
    config.vm.network :forwarded_port, guest: 22, host: 2220, host_ip: "0.0.0.0"
    
    # Create a private network, which allows host-only access to the machine
    # using a specific IP.
    # config.vm.network "private_network", ip: "192.168.33.10"
    
    # Create a public network, which generally matched to bridged network.
    # Bridged networks make the machine appear as another physical device on
    # your network.
    # 配置为公有网络,虚拟机将通过 DHCP 获取一个与宿主机同网段的 IP 地址
    # config.vm.network "public_network"
    #config.vm.network "public_network", ip: "192.168.11.82"
    
    # Share an additional folder to the guest VM. The first argument is
    # the path on the host to the actual folder. The second argument is
    # the path on the guest to mount the folder. And the optional third
    # argument is a set of non-required options.
    # config.vm.synced_folder "../data", "/vagrant_data"
    
    # Disable the default share of the current code directory. Doing this
    # provides improved isolation between the vagrant box and your host
    # by making sure your Vagrantfile isn't accessible to the vagrant box.
    # If you use this you may want to enable additional shared subfolders as
    # shown above.
    # config.vm.synced_folder ".", "/vagrant", disabled: true
    
    config.vm.provider "virtualbox" do |vb|
    #   # Display the VirtualBox GUI when booting the machine
    vb.gui = false
    #
    # Customize the amount of memory on the VM:
    vb.memory = "8096" 
    
    # 42 CPU cores
    vb.cpus = 2 
    
    # Enable nested virtualization (optional)
    vb.customize ["modifyvm", :id, "--nested-hw-virt", "on"]
    
    # vm name
    vb.name = "centos8.3"
    
    # Startup Item Settings
    vb.customize ["modifyvm", :id, "--boot1", "disk"]
    vb.customize ["modifyvm", :id, "--boot2", "dvd"]
    vb.customize ["modifyvm", :id, "--boot3", "floppy"]
    vb.customize ["modifyvm", :id, "--boot4", "net"]
    
    # Enable USB controller (USB 2.0)
    #vb.customize ["modifyvm", :id, "--usb", "on"]
    #vb.customize ["modifyvm", :id, "--usbehci", "on"]
     
    # Enable USB controller (USB 3.0)
    vb.customize ["modifyvm", :id, "--usb", "on"]
    vb.customize ["modifyvm", :id, "--usbxhci", "on"]
    
    # 启用双向共享粘贴板和拖放功能
    vb.customize ["modifyvm", :id, "--clipboard", "bidirectional"]
    vb.customize ["modifyvm", :id, "--draganddrop", "bidirectional"]
    end
    
    # View the documentation for the provider you are using for more
    # information on available options.
    
    # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
    # such as FTP and Heroku are also available. See the documentation at
    # https://docs.vagrantup.com/v2/push/atlas.html for more information.
    # config.push.define "atlas" do |push|
    #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
    # end
    
    # Enable provisioning with a shell script. Additional provisioners such as
    # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
    # documentation for more information about their specific syntax and use.
    # config.vm.provision "shell", inline: <<-SHELL
    #   apt-get update
    #   apt-get install -y apache2
    # SHELL
    config.vm.provision "shell", inline: <<-SHELL
      echo "root:1234@com" | chpasswd
      echo "vagrant:1234@com" | chpasswd
          
      # 配置 DNS
      echo "nameserver 8.8.8.8" > /etc/resolv.conf
      echo "nameserver 8.8.4.4" >> /etc/resolv.conf
          
      rm -rf /etc/yum.repos.d/*
      curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
      yum clean all
      yum makecache
      sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
      sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/g' /etc/ssh/sshd_config
      systemctl restart sshd
          
      # 分区插件安装配置
      yum install -y xfsprogs xfsdump parted vim net-tools
          
      # 使用 fdisk 创建单一分区
      (
      echo n      # 添加新分区
      echo p      # 主分区
      echo 2      # 分区号(1)
      echo        # 起始扇区(默认)
      echo        # 结束扇区(默认,使用所有可用空间)
      echo w      # 写入更改并退出
      ) | fdisk /dev/sda
    
      mkfs.xfs -f /dev/sda2
      mount -t xfs /dev/sda2 /opt
      sed -i '$a/dev/sda2  /opt xfs  defaults  0  0' /etc/fstab 
    SHELL

    end
    EOF

    echo "download centos vagrant"
    wget -N -P /opt/centos8 https://cloud.centos.org/centos/8/vagrant/x86_64/images/CentOS-8-Vagrant-8.3.2011-20201204.2.x86_64.vagrant-virtualbox.box

    echo "add vagrant list"
    vagrant box add --name centos8.3 /opt/centos8/CentOS-8-Vagrant-8.3.2011-20201204.2.x86_64.vagrant-virtualbox.box

    在线下载

    #vagrant init centos8.3 --box-version 2004.01

    #echo "init vagrant-disksize"
    #vagrant plugin install vagrant-disksize

    echo "start centos vm"
    vagrant up

    #echo "进入"
    #vagrant ssh

    echo "查看"
    vagrant box list

    echo "port firewall"
    firewall-cmd --zone=public --add-port=2220/tcp --permanent && firewall-cmd --reload

    echo "vagrant service"
    cat <<'EOF'>>/usr/lib/systemd/system/vagrant-autostart.service
    [Unit]
    Description=Auto-start Vagrant VM on boot
    After=network.target vboxdrv.service

    [Service]
    Type=oneshot
    ExecStart=/usr/bin/vagrant_start.sh
    ExecStop=/usr/bin/vagrant_stop.sh
    RemainAfterExit=true
    User=root

    [Install]
    WantedBy=multi-user.target
    EOF

    echo "Vagrant startup script"
    cat <<'EOF'>>/usr/bin/vagrant_start.sh
    #!/bin/bash

    定义 Vagrant 项目目录列表

    VAGRANT_PROJECTS=(
    "/opt/centos8"
    "/opt/ubuntu20"
    "/opt/debian11"
    )

    遍历每个 Vagrant 项目并启动虚拟机

    for project in "{VAGRANT_PROJECTS[@]}"; do echo "切换到 Vagrant 项目目录: project 并启动虚拟机..."
    if [ -d "project" ]; then cd "project" || { echo "无法进入目录: project"; continue; } vagrant up >> /var/log/vagrant_startup.log 2>&1 if [ ? -eq 0 ]; then
    echo "虚拟机启动成功: project" else echo "虚拟机启动失败: project"
    fi
    else
    echo "项目目录不存在: $project"
    fi
    done
    EOF

    echo "Vagrant close script"
    cat <<'EOF'>>/usr/bin/vagrant_stop.sh
    #!/bin/bash

    定义 Vagrant 项目目录列表

    VAGRANT_PROJECTS=(
    "/opt/centos8"
    "/opt/ubuntu20"
    "/opt/debian11"
    )

    遍历每个 Vagrant 项目并关闭虚拟机

    for project in "{VAGRANT_PROJECTS[@]}"; do echo "切换到 Vagrant 项目目录: project 并关闭虚拟机..."
    if [ -d "project" ]; then cd "project" || { echo "无法进入目录: project"; continue; } vagrant halt >> /var/log/vagrant_shutdown.log 2>&1 if [ ? -eq 0 ]; then
    echo "虚拟机关闭成功: project" else echo "虚拟机关闭失败: project"
    fi
    else
    echo "项目目录不存在: $project"
    fi
    done
    EOF

    echo "赋予脚本权限"
    chmod +x /usr/bin/{vagrant_start.sh,vagrant_stop.sh}

    echo "启动服务,开机自启动"
    systemctl enable vagrant-autostart.service
    systemctl status vagrant-autostart.service

执行安装

复制代码
bash /vagrant_centos_install.sh

进入centos 系统

复制代码
cd /opt/centos8
vagrant ssh

进入virtualbox 管理界面

复制代码
virtualbox

远程连接 MobaXterm或者Xmanager

  • 端口 2220
  • root:1234@com,vagrant:1234@com
相关推荐
良许Linux11 分钟前
嵌入式算吃青春饭么?
linux
良许Linux23 分钟前
马上要毕业去工作了,做嵌入式软件开发工程师,但是完全不会编程怎么办?
linux
良许Linux27 分钟前
学stm32,有什么学习方法?
linux
良许Linux28 分钟前
为啥有好多人说 Arduino 是玩具?
linux
独行soc39 分钟前
2025年常见渗透测试面试题-红队面试宝典下(题目+回答)
linux·运维·服务器·前端·面试·职场和发展·csrf
mosaicwang43 分钟前
dnf install openssl失败的原因和解决办法
linux·运维·开发语言·python
想躺在地上晒成地瓜干1 小时前
树莓派超全系列教程文档--(24)本地化设置、SSH及配置防火墙
linux·ssh·树莓派·raspberrypi·树莓派教程
小丁爱养花1 小时前
驾驭 Linux 云: JavaWeb 项目安全部署
java·linux·运维·服务器·spring boot·后端·spring
HORSE RUNNING WILD2 小时前
为什么我们需要if __name__ == __main__:
linux·python·bash·学习方法
王闯写bug3 小时前
linux一次启动多个jar包
linux·jar