Vagrant基本命令详解

正在学习Docker Swarm,接触到了Vagrant。Vagrant是一个建立虚拟机的技术,能够把它认为是一个Vmware,它让咱们能够经过代码的方式快速地、可重复地建立针对不一样虚拟环境的虚拟机,包括Virtualbox、AWS、Docker等。它使得咱们能够一次性地、自动建立多个环境相同的虚拟机,对于软件开发和测试尤为有用,如下是在使用过程当中经常使用到的一些基本命令。javascript

1. 显示当前版本

# vagrant --version Vagrant 2.0.2 

2. 列出全部Box

#vagrant box list iamseth/rhel-7.3 (virtualbox, 1.0.0) 

3. 添加一个Box

#vagrant box add [options] <name, url, or path> 

1)能够从https://app.vagrantup.com/boxes/search下载各类Vagrant映像文件

#vagrant box add ubuntu/trusty64 

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

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

3) 添加一个本地box

命令格式以下php

# vagrant box add {box_name} {file_path} 

示例以下:css

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

4. 初始化一个新VM

# vagrant init ubuntu/trustry64 

此命令会在当前目录建立一个名为Vagrantfile的配置文件,内容大体以下:html

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

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

5. 初始化一个新VM

# vagrant up 

若是咱们想启动任意VM,首先进入有Vagrantfile配置文件的目录,而后执行上面的命令。控制台的输出一般以下:ubuntu

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' is up to date... ==> default: Setting the name of the VM: start_default_1518789015107_16928 ==> default: Clearing any previously set forwarded ports... Vagrant is currently configured to create VirtualBox synced folders with the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant guest is not trusted, you may want to disable this option. For more information on this option, please refer to the VirtualBox manual: https://www.virtualbox.org/manual/ch04.html#sharedfolders This option can be disabled globally with an environment variable: VAGRANT_DISABLE_VBOXSYMLINKCREATE=1 or on a per folder basis within the Vagrantfile: config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false ==> 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.36 default: VirtualBox Version: 5.2 ==> default: Mounting shared folders... default: /vagrant => /Users/qianlong/DockerProjects/vagrant/start 

6.启用SSH登录VM

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

# vagrant ssh 

若是须要从虚拟机中退出,直接在虚拟机中的命令行输入exit命令便可bash

7. 查看VM当前的状态

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

# vagrant status 

若是VM正在运行,你能够在控制台中看到以下信息:ssh

Current machine states:

default running (virtualbox) The VM is running. To stop this VM, you can run `vagrant halt` to shut it down forcefully, or you can run `vagrant suspend` to simply suspend the virtual machine. In either case, to restart it again, simply run `vagrant up`. 

8.关闭VM

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

# vagrant halt 

9.销毁VM

命令格式以下:

vagrant destory [name|id] 

示例以下:

# vagrant destroy ubuntu/trusty64 

若是咱们是使用了默认的虚拟机名"default",那其实咱们能够直接输入以下命令销毁VM

# vagrant desotry 

运行结果以下:

default: Are you sure you want to destroy the 'default' VM? [y/N] y ==> default: Forcing shutdown of VM... ==> default: Destroying VM and associated drives... 

须要注意的是,咱们要慎重使用该命令,该命令会中止VM的运行,并销毁全部在VM中建立的资源

做者:蓝色的咖啡 连接:https://www.jianshu.com/p/b7d599403aa6 来源:简书 简书著做权归做者全部,任何形式的转载都请联系做者得到受权并注明出处。
相关文章
相关标签/搜索