# 入门基础
# 基本操作
# 生成vagrant file,init后面是镜像地址
vagrant init maroskukan/debian12
# 启动虚拟机
vagrant up --provider=hyperv --color
# 查看虚拟机状态
vagrant status
# SSH连接
vagrant ssh name
# 查看SSH配置
vagrant ssh-config
# 暂停,恢复,重载,停止
vagrant suspend/resume/reload/halt name
# 删除移除
vagrant destroy name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 国内镜像
使用国内镜像:
# 下载镜像
vagrant box add generic/ubuntu2004 --provider=hyperv --box-version=3.1.22
# 删除镜像
vagrant box remove generic/ubuntu2004 --provider=hyperv --box-version=3.1.22
# 增加镜像地址:
vagrant box add name repoADDR --provider=hyperv
# 查看所有镜像
vagrant box list
vagrant init repoADDR
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
下面是几个镜像地址:
# Ubuntu
vagrant init ubuntu-bionic https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box
vagrant init ubuntu-jammy https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/jammy/current/jammy-server-cloudimg-amd64-vagrant.box
# Centos
vagrant init centos7 https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box
1
2
3
4
5
2
3
4
5
# vagrantfile
参考文档:
vagranfile是ruby的语法。下面是一个样例:
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.hostname = "vagrant-demo"
config.vm.box_version = "1905.1"
config.ssh.insert_key = false
config.vm.define "web-server" do |web|
web.vm.hostname = "web"
end
config.vm.define "database" do |db|
db.vm.hostname = "db"
end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
通过一个循环来实现上面的定义:
host_list = [
{
:name => "host1",
:box => "centos7"
},
{
:name => "host2",
:box => "debian"
}
]
Vagrant.configure("2") do |config|
config.vm.provider "hyperv" do |v|
config.vm.synced_folder ".", "/vagrant", type: "smb"
end
config.vm.provider "virtualbox" do |_,override|
override.vm.synced_folder ".", "/vagrant", type: "virtualbox"
end
host_list.each do |onehost|
config.vm.define onehost[:name] do |host|
host.vm.box = onehost[:box]
end
end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 基本实现
Vagrant.configure("2") do |config|
config.vm.box = "debianenvbox"
config.vm.hostname = "vagrant-test1"
config.ssh.username = "root"
config.ssh.password = "root"
config.vm.synced_folder "./smbfolder", "/vagrant",
type: "smb",
smb_username: "administrator",
smb_password: "hehepass",
mount_options: ["username=administrator","password=hehepass"]
end
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 文件夹同步
# 禁用同步:
# 进入 C:\Users\username\.vagrant.d\boxes\centos-vagrantslash-7/2004.1/virtualbox/vagrantfile
# 去掉 config.vm.synced_folder
# 如果是virtualbox则安装插件
vagrant plugin list
vagrant plugin install vagrant-vbguest --plugin-version 0.21
# 如果是hyperv的话,可以使用smb来共享
host.vm.synced_folder ".", "/vagrant", type: "smb"
# 查看powershell的版本
Get-Host | Select-Object Version
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 创建镜像
# 创建一个virtual box类型的box
vagrant package --base virtualhostname
# 虚拟机需要提前安装
apt-get install hyperv-daemons
# HyperV导出镜像到vagrant box
# 1. 导出HyperV虚拟机到一个文件夹。
# 2. 创建一个metadata.json文件:
{"provider":"hyperv"}
# 3. 压缩文件为box格式
tar zcvf ../testname.box ./*
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10