歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Vagrant基本命令詳解

Vagrant基本命令詳解

日期:2017/2/28 13:47:48   编辑:Linux教程

Vagrant基本命令詳解

1、檢查當前的版本

# vagrant --version
Vagrant 1.8.1

2、列出所有的box

# vagrant box list
CentOS/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 a
nd 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: vir
tualbox
    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的運行,並銷毀所有創建的資源。

Copyright © Linux教程網 All Rights Reserved