欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用vagrant和virtualbox虚拟环境

程序员文章站 2024-03-13 18:02:27
...

软件安装

安装vagrant,安装virtualbox
安装步骤省略

使用方法

  • 新建文件夹vagrant, 进入该文件夹
mkdir vagrant
  • 下载.box系统的镜像文件

获取镜像文件地址:[http://www.vagrantbox.es/]

两种方式:
1.从url直接添加

vagrant box add {name} {镜像url}

2.下载后添加

vagrant box add {name} {下载好的本地box路径}

例:我新建了box文件夹,用下载的方式将.box文件放到了box文件夹下.

下载速度慢可以转到清华的源上,本次用到的.box下载路径:
https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box

E:\vagrant\box\{name}.box

并执行添加命令:

vagrant box add {myname} {E:\vagrant\box\{name}.box}
  • 列出添加后的box列表
vagrant box list
  • 初始化box
vagrant init {myname}
  • 启动box
vagrant up --provider=virtualbox

启动box如果报以下错误

E:\vagrant\box>vagrant up --provider=virtualbox
The provider 'virtualbox' that was requested to back the machine
'default' is reporting that it isn't usable on this system. The
reason is shown below:

Vagrant has detected that you have a version of VirtualBox installed
that is not supported by this version of Vagrant. Please install one of
the supported versions listed below to use Vagrant:

4.0, 4.1, 4.2, 4.3, 5.0, 5.1

A Vagrant update may also be available that adds support for the version
you specified. Please check www.vagrantup.com/downloads.html to download
the latest version.

是因为vagrant和virtualbox版本不匹配导致。

解决办法参考次大神博客,亲测有效:
https://www.cnblogs.com/chen-785/p/12881625.html

解决完成后能够正常启动:

E:\vagrant\box>vagrant up --provider=virtualbox
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: box_default_1591669991423_51699
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.28
    default: VirtualBox Version: 6.0
==> default: Mounting shared folders...
    default: /vagrant => E:/vagrant/box

然后可以看到virtualbox里已经添加成功正在运行。
使用vagrant和virtualbox虚拟环境
启动连接窗口后输入默认用户名密码:
用户名 : vagrant , 登录密码 : vagrant
使用vagrant和virtualbox虚拟环境

相关标签: 环境