vagrant指北

Vagrant安装&配置

  • An error occurred while downloading the remote file. The error message, if any, is reproduced below. Please fix this error and try again

    csdn

安装

可通过官网下载安装地址下载安装。

windows10安装

选择AMD64版本,下载得到exe文件后双击打开安装即可。

image-20230319214628259

验证安装

控制台输入vagrant -v查看版本,可验证是否安装完成,正常输出版本如下:例如我的版本是2.3.4

image-20230319215133740

配置VMware插件

安装vagrant-vmware-desktop

官网文档

前提:安装完成vagrant后

执行命令安装vmware插件

1
vagrant plugin install vagrant-vmware-desktop

稍等几分钟后出现如下提示则安装完成

image-20230319224920262

安装Vagrant VMware Utility

下载页Install | Vagrant | HashiCorp Developer

windows10选择X86_64版本下载后双击安装。

基础使用配置

初始化

通过简单的几个vagrant命令即可快速初始化一个虚拟机

1
2
3
4
mkdir centos7_1
cd centos7_1
vagrant init
vagrant up

<<<<<<< HEAD
### 问题集合

常用操作

1
2
3
4
5
6
7
8
9
vagrant up #启动:,状态是 running

vagrant halt #停止:状态是 poweroff

vagrant suspend #暂停:状态是 saved

vagrant reload #重启:状态是 running 命令 reload 是先关闭,再开启

vagrant destroy #销毁:状态是 not created

时间同步

228ea1bf0a6fba68ec563ecd720756c6d6913775

vagrant up报错:Vagrant encountered an error while attempting to load the utility service key file.

Vagrant encountered an error while attempting to load the utility
service key file. This error can occur if the Vagrant VMware Utility
has not yet been installed, or if it was installed incorrectly. If
this error persists after running the Vagrant VMware Utility installer
again, please contact support at: support@hashicorp.com

Information about the Vagrant VMware Utility, including installation
instruction, can be found here:

https://www.vagrantup.com/docs/vmware/vagrant-vmware-utility.html

Path: C:/ProgramData/hashicorp/vagrant-vmware-desktop/certificates/vagrant-utility.client.crt
Error: No such file or directory @ rb_sysopen - C:/ProgramData/hashicorp/vagrant-vmware-desktop/certificates/vagrant-utility.client.crt

原因:

未安装Vagrant VMware Utility。

解决

参考上面安装Vagrant Vmware Utility

连接

vagrant up启动虚拟机后,可通过

1
vagrant ssh

ssh连接虚拟机,去除了繁杂的密码密钥等,交由vagrant管理。

配置网络

配置hostname

1
config.vm.hostname = "centos01"

配置ip

由于VirtualBox默认的虚拟机策略为网络地址转换,此策略对于后续的程序安装,需要访问时,都得转换一次端口,较为麻烦。为给虚拟机分配一个独有的ip地址,可通过vagrant配置文件指定。编辑虚拟机的Vagrantfile文件,加入如下行

hostname: true启用hostname

1
config.vm.network "public_network", ip: "192.168.1.10", hostname: true

保存后,执行vagrant reload命令重新加载配置,通过ip addr命令可查看虚拟机当前ip地址

基础命令

1
2
3
4
5
6
# 初始化一个vagrant镜像
vagrant init
# 启动vagrant镜像
vagrant up
# 重新加载配置,用于修改vagrantfile后
vagrant reload

查看端口监听

1
sudo netstat -ntalp | grep 80

问题合集

1. vagrant up faild、在线安装慢

在首次执行vagrant up时需要下载centOS镜像,由于网络问题可能会下载失败或者下载很慢。这时候可以手动把镜像下载到本地,再通过add命令加载镜像到vagrant,再使用up命令即可跳过下载步骤

镜像搜索:https://app.vagrantup.com/boxes/search

Centos/7:https://app.vagrantup.com/centos/boxes/7

下载镜像后通过命令添加到vagrant:

1
2
vagrant add 镜像名 路径
#例如 vagrant box add centos/7 ./CentOS-7-x86_64-Vagrant-2004_01.VirtualBox.box

此时再通过vagrant up命令安装启动虚拟机就不需要下载了,速度较快,推荐

2.code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole

1
2
3
4
5
6
7
8
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["startvm", "605c2e37-9c5a-48fc-8884-26c9160bdf3d", "--type", "headless"]

Stderr: VBoxManage: error: hv_vm_create() failed: 0xfae94001 (VERR_NEM_INIT_FAILED).
VBoxManage: error: Driverless mode (VERR_SUP_DRIVERLESS)
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole

截屏2022-11-10 22.29.15

原因之一:AMD好像只支持到6.1的vbox,卸载7.0安装6.1的,然后在隐私项勾选oracle,确认重启搞定。

Is the docker daemon running?

docker服务未启动,

1
2
3
4
#启动docker
service start docker
#开机自启
sudo systemctl enable docker

3.ping time out

配置了ip无法连接,检查配置的ip是否和主机处于同一网段


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!