Vagrant是一款用于构建及配置虚拟开发环境的软件,基于Ruby,主要以命令行的方式运行。
Vagrant作为最外层的虚拟软件,目的是帮助开发者更容易地与Providers互动。Vagrantfile记录Providers和Provisioners的相关信息。
Providers作为服务,帮助vagrant使用Boxes建立和创建虚拟环境。Vagrant提供的内嵌的Provider有 VirtualBox、Hyper-V、Docker、VMware,而AWS以插件形式提供支持。
1.安装 VirtualBox
和 安装 Vagrant
,访问官方网站下载程序安装
VirtualBox
官方网址: https://www.virtualbox.org/Vagrant
官方网址: https://www.vagrantup.com/
2.下载Vagrant Boxes基本系统镜像- 访问 Discover Vagrant Boxes
-
https://app.vagrantup.com/boxes/search
找到适用
virtualbox
虚拟机的Debian 11
镜像 - https://app.vagrantup.com/debian/boxes/bullseye64
部署 debian/bullseye64 Vagrant box 两行命令就能自动部署完成了
vagrant init debian/bullseye64
vagrant up
-
实际情况你懂的,所以我先在国外vps机器,先下载保存镜像
debian11.box
, 下载离线镜像的wget -O debian11.box \r https://app.vagrantup.com/debian/boxes/bullseye64/versions/11.20210829.1/providers/virtualbox.box
国内中科大镜像: Centos和Ubuntu系统的 Vagrant box 下载地址,适合国内网络部署
- https://mirrors.ustc.edu.cn/centos-cloud/centos/8/vagrant/x86_64/images/
- https://mirrors.ustc.edu.cn/ubuntu-cloud-images/vagrant/trusty/
3.使用 vagrant box add
建立本地镜像
vagrant box add debian D:vagrantdebian11.box
# 基础镜像保存硬盘位置 C:Usersvip.vagrant.doxesdebian virtualbox
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"
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
重启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地址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
0 条评论