使用vagrant可以快速创建VirtualBox虚拟机,避免由iso从头开始。
安装软件
安装VirtualBox
下载VirtualBox时,除了选择合适的主程序外,也需要下载扩展程序:

官网:https://www.virtualbox.org/
安装完主程序后,再安装扩展程序即可。
安装vagrant
官网:https://www.vagrantup.com/
安装完成后,vagrant默认会将路径添加到PATH中,可通过vagrant version检查是否安装成功。
软件配置
配置VirtualBox
虚拟机一般占用较大的磁盘空间,可打开VirtualBox,通过菜单 管理 -> 全局设定 修改 默认虚拟电脑位置 指定到一个可用空间较大的磁盘。
配置vagrant
通过vagrant创建虚拟机需要先导入镜像文件,也就是 box。它默认存放在系统磁盘下,如果希望将它存放到其它磁盘,可修改环境变量,添加VAGRANT_HOME,指定一个目录即可。

下载虚拟机
vagrant提供了镜像网站,可以从这里打开,里面提供了各种不同的镜像文件并提供详尽的文档。

下载镜像文件:vagrant box add ubuntu/trusty64
其中ubuntu/trusty64是镜像的名称:
下载的box一般都较大,需要一段时间,出现如下提示后即代表下载成功:
==> box: Loading metadata for box 'ubuntu/trusty64'
box: URL: https://vagrantcloud.com/ubuntu/trusty64
==> box: Adding box 'ubuntu/trusty64' (v20190514.0.0) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/ubuntu/boxes/trusty64/versions/20190514.0.0/providers/virtualbox.box
Download redirected to host: cloud-images.ubuntu.com
box:
==> box: Successfully added box 'ubuntu/trusty64' (v20190514.0.0) for 'virtualbox'!
初始化虚拟机
创建一个目录例如:ubuntu64,并通过命令行进入该目录,执行如下命令:
vagrant init ubuntu/trusty64
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.
启动虚拟机
在ubuntu64目录,执行如下命令:
vagrant up
镜像首先启动时,vagrant会安装VirtualBox虚拟机,如果有多个网络接口,vagrant会要求用户选择一个网络接口连接到虚拟机,跟据实际情况选择一个即可。
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' version '20190514.0.0' is up to date...
==> default: Setting the name of the VM: ubuntu64_default_1614005749871_47974
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2200 (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:2200
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection reset. Retrying...
default: Warning: Connection aborted. Retrying...
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.40
default: VirtualBox Version: 6.1
==> default: Mounting shared folders...
default: /vagrant => T:/.vagrant.d/ubuntu64
连接虚拟机
在ubuntu64目录,执行如下命令:
vagrant ssh
Welcome to Ubuntu 14.04.6 LTS (GNU/Linux 3.13.0-170-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information disabled due to load higher than 1.0
UA Infrastructure Extended Security Maintenance (ESM) is not enabled.
0 updates can be installed immediately.
0 of these updates are security updates.
Enable UA Infrastructure ESM to receive 64 additional security updates.
See https://ubuntu.com/advantage or run: sudo ua status
New release '16.04.7 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
vagrant@vagrant-ubuntu-trusty-64:~$
关闭虚拟机
在ubuntu64目录,执行如下命令:
vagrant halt
==> default: Attempting graceful shutdown of VM...
销毁虚拟机
vagrant destroy