Vagrant基本命令使用详解

时间:2021-05-19

Vagrant基本命令详解,具体如下:

1、检查当前的版本

# vagrant --versionVagrant 1.8.1

2、列出所有的box

# vagrant box listcentos/7 (virtualbox, 1603.01)ubuntu/trusty64 (virtualbox, 20160406.0.0)

3、添加一个box

# vagrant box add ADDRESS

1)box名简写

Vagrant可以从这里https://atlas.hashicorp.com/boxes/search 下载各种Vagrant映像文件。

# vagrant box add ubuntu/trusty64

2)通过指定的URL添加远程box

# vagrant box add https://atlas.hashicorp.com/ubuntu/boxes/trusty64

3)添加一个本地box

# vagrant box add CentOS7.1 file:///D:/Work/VagrantBoxes/CentOS-7.1.1503-x86_64-netboot.box

4、初始化一个新VM

# vagrant init ubuntu/trustry64

此命令会在当前目录创建一个名为Vagrantfile的配置文件,内容大致如下:

Vagrant.configure(2) do |config| config.vm.box = "ubuntu/trusty64"end

当在此目录启动Vagrant后,Vagrant会从互联网下载“ubuntu/trusty64”这个box到本地,并使用它作为VM的映像。

要搜索可用的box,查看这里: https://atlas.hashicorp.com/boxes

5、启动VM

# vagrant up

如果我们想启动任意VM,首先进入有Vagrantfile配置文件的目录,然后执行上面的命令。控制台的输出通常如下:

Bringing machine 'default' up with 'virtualbox' provider...==> default: Box 'ubuntu/trusty64-juju' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: >= 0==> default: Loading metadata for box 'ubuntu/trusty64-juju' default: URL: https://atlas.hashicorp.com/ubuntu/trusty64-juju==> default: Adding box 'ubuntu/trusty64-juju' (v20160707.0.1) for provider: virtualbox default: Downloading: https://atlas.hashicorp.com/ubuntu/boxes/trusty64-juju/versions/20160707.0.1/providers/virtualbox.box==> default: Waiting for cleanup before exiting... default: Progress: 0% (Rate: 0/s, Estimated time remaining: --:--:--):--)

6、启用SSH登陆VM

进入Vagrantfile配置文件所在的目录,执行以下命令:

# vagrant ssh

要注意,本机上必须先安装SSH客户端。

7、关闭VM

进入Vagrantfile配置文件所在的目录,执行以下命令:

# vagrant halt

8、销毁VM

# vagrant destory [name|id]

比如:

vagrant destroy ubuntu/trusty64

此命令会停止VM的运行,并销毁所有创建的资源。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章