logo.png

Vagrant是一款用于构建及配置虚拟开发环境的软件,基于Ruby,主要以命令行的方式运行。
Vagrant作为最外层的虚拟软件,目的是帮助开发者更容易地与Providers互动。Vagrantfile记录Providers和Provisioners的相关信息。
Providers作为服务,帮助vagrant使用Boxes建立和创建虚拟环境。Vagrant提供的内嵌的Provider有 VirtualBox、Hyper-V、Docker、VMware,而AWS以插件形式提供支持。

1.安装 VirtualBox 和 安装 Vagrant,访问官方网站下载程序安装

2.下载Vagrant Boxes基本系统镜像- 访问 Discover Vagrant Boxes

d11.PNG

部署 debian/bullseye64 Vagrant box 两行命令就能自动部署完成了

vagrant init debian/bullseye64
vagrant up

3.使用 vagrant box add 建立本地镜像

vagrant box add debian D:vagrantdebian11.box
# 基础镜像保存硬盘位置  C:Usersvip.vagrant.doxesdebianvirtualbox

vagrant.PNG

PS C:VPCvagrant> vagrant box add --help
Usage: vagrant box add [options] <name, url, or path>

PS C:VPCvagrant> vagrant box add debian D:vagrantdebian11.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'debian' (v0) for provider:
    box: Unpacking necessary files from: file:///D:/vagrant/debian11.box
    box:
==> box: Successfully added box 'debian' (v0) for 'virtualbox'!

vagrant box list 查看镜像

PS C:VPCvagrant> vagrant box list
debian (virtualbox, 0)

4.使用 vagrant up 建立虚拟机和启动虚拟机

  • 先建立一个目录 C:VPCvagrant,在这个目录 init
  • 这个目录到时会自动挂载到虚拟机中,命令行进入这个目录

    vagrant init debian
    vagrant up
    # 虚拟机磁盘保存硬盘位置  C:UsersvipVirtualBox VMsvagrant_default_1632921757898_12141

    运行 vagrant up 前先编辑 Vagrantfile,开启 config.vm.network public_network""

  • 创建一个公共网络,它通常与桥接网络相匹配。
  • 桥接网络使机器显示为网络上的另一个物理设备。

    # Create a public network, which generally matched to bridged network.
    # Bridged networks make the machine appear as another physical device on
    # your network.
    config.vm.network "public_network"

    vagrant1.PNG

    PS C:VPCvagrant> vagrant init debian
    A "Vagrantfile" has been placed in this directory. You are now
    ready to "vagrant up" your first virtual environment! Please read
    the comments in the Vagrantfile as well as documentation on
    "vagrantup.com" for more information on using Vagrant.
    
    vagrant.exe up 

    vagrant status 查看虚拟机运行状态; vagrant halt 停止; vagrant up 重启

    vagrant2.PNG

    PS C:VPCvagrant> vagrant status
    Current machine states:
    
    default                   running (virtualbox)
    
    The VM is running. To stop this VM, you can run "vagrant halt" to
    shut it down forcefully, or you can run "vagrant suspend" to simply
    suspend the virtual machine. In either case, to restart it again,
    simply run "vagrant up".
    PS C:VPCvagrant> vagrant halt
    ==> default: Attempting graceful shutdown of VM...

    vagrant ssh 登陆虚拟机,ip addr 查看IP地址

    vagrant3.PNG

    vagrant ssh
    ip addr
    sudo -i # 切换到root

    5.添加证书方便使用 Xshell 登陆

    root@bullseye:~# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    
    /dev/sda1        20G  856M   18G   5% /
    vagrant         112G   64G   48G  58% /vagrant
    
    # Windows系统中 vagrant 目录 挂载在虚拟机 /vagrant
    
    mkdir -p /root/.ssh
    cp /vagrant/authorized_keys  /root/.ssh/authorized_keys
    

    6. vagrant reload 重载虚机 和 vagrant destroy 删除虚机

  • 重载虚机: 执行下面的命令会重启虚机,并且重新加载 Vagrantfile 中的配置信息
  • 删除虚机: 最后,执行下面的命令可以彻底删除虚机,包括整个虚机文件
  • PS. 执行 vagrant 命令, 要在 包含 Vagrantfile 的目录执行

7. 国内Linux系统更换中科大软件源

sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g'  /etc/apt/sources.list
sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g'  /etc/apt/sources.list.d/unstable.list
apt update

相关文章: 超详细的 Vagrant 上手指南


0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用 * 标注

© 2024 兰雅VBA代码分享
浙ICP备2021017795号-2 浙公网安备33078102100266号