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 來源:簡書 簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。
相關文章
相關標籤/搜索