版權聲明:本文為部落客chszs的原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/chszs/article/details/51925179
Vagrant基本指令詳解
作者:chszs,未經部落客允許不得轉載。經許可的轉載需注明作者和部落格首頁: http://blog.csdn.net/chszs
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/boxes5、啟動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
# vagrant halt
8、銷毀VM
# vagrant destory [name|id]
比如:
vagrant destroy ubuntu/trusty64
此指令會停止VM的運作,并銷毀所有建立的資源。
寫在最後
我建立了一個微信個人公衆号,部落格的資訊也會在公衆号同步更新。關注随意。