Vagrant基本命令詳解

正在學(xué)習(xí)Docker Swarm,接觸到了Vagrant。Vagrant是一個(gè)創(chuàng)建虛擬機(jī)的技術(shù),可以把它認(rèn)為是一個(gè)Vmware,它讓我們可以通過(guò)代碼的方式快速地、可重復(fù)地創(chuàng)建針對(duì)不同虛擬環(huán)境的虛擬機(jī),包括Virtualbox、AWS、Docker等。它使得我們可以一次性地、自動(dòng)創(chuàng)建多個(gè)環(huán)境相同的虛擬機(jī),對(duì)于軟件開發(fā)和測(cè)試尤其有用,以下是在使用過(guò)程中常用到的一些基本命令。

1. 顯示當(dāng)前版本

# vagrant --version
Vagrant 2.0.2

2. 列出所有Box

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

3. 添加一個(gè)Box

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

1)可以從https://app.vagrantup.com/boxes/search下載各種Vagrant映像文件

#vagrant box add ubuntu/trusty64

2) 通過(guò)指定的URL添加遠(yuǎn)程box

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

3) 添加一個(gè)本地box

命令格式如下

# vagrant box add {box_name} {file_path}

示例如下:

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

4. 初始化一個(gè)新VM

# vagrant init ubuntu/trustry64

此命令會(huì)在當(dāng)前目錄創(chuàng)建一個(gè)名為Vagrantfile的配置文件,內(nèi)容大致如下:

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

當(dāng)在此目錄啟動(dòng)Vagrant后,Vagrant會(huì)從互聯(lián)網(wǎng)下載“ubuntu/trusty64”這個(gè)box到本地,并使用它作為VM的映像。

5. 初始化一個(gè)新VM

# vagrant up

如果我們想啟動(dòng)任意VM,首先進(jìn)入有Vagrantfile配置文件的目錄,然后執(zhí)行上面的命令??刂婆_(tái)的輸出通常如下:

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

進(jìn)入Vagrantfile配置文件所在的目錄,執(zhí)行以下命令:

# vagrant ssh

如果需要從虛擬機(jī)中退出,直接在虛擬機(jī)中的命令行輸入exit命令即可

7. 查看VM當(dāng)前的狀態(tài)

進(jìn)入Vagrantfile配置文件所在的目錄,執(zhí)行以下命令:

# vagrant status

如果VM正在運(yùn)行,你可以在控制臺(tái)中看到如下信息:

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.關(guān)閉VM

進(jìn)入Vagrantfile配置文件所在的目錄,執(zhí)行以下命令:

# vagrant halt

9.銷毀VM

命令格式如下:

vagrant destory [name|id]

示例如下:

# vagrant destroy ubuntu/trusty64

如果我們是使用了默認(rèn)的虛擬機(jī)名"default",那其實(shí)我們可以直接輸入如下命令銷毀VM

# vagrant desotry

運(yùn)行結(jié)果如下:

    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...

需要注意的是,我們要慎重使用該命令,該命令會(huì)停止VM的運(yùn)行,并銷毀所有在VM中創(chuàng)建的資源

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1,介紹Vagrant 我們做web開發(fā)的時(shí)候經(jīng)常要安裝各種本地測(cè)試環(huán)境,比如apache,php,mysql,r...
    meng_philip123閱讀 2,643評(píng)論 0 12
  • Vagrant是什么 Vagrant是一個(gè)軟件,可以自動(dòng)化虛擬機(jī)的安裝和配置流程。目前市面上個(gè)人PC的主流操作系統(tǒng)...
    北魏企鵝閱讀 30,465評(píng)論 3 40
  • 最近服務(wù)器老是宕機(jī),學(xué)習(xí)的時(shí)候很多環(huán)境有需要集群,忍無(wú)可忍的情況下終于決定自己裝一波虛擬機(jī)宮學(xué)習(xí)使用。本人系統(tǒng)為D...
    行書以鑒閱讀 2,070評(píng)論 0 1
  • 當(dāng)我敲下文章標(biāo)題的時(shí)候,我在內(nèi)心困惑地詢問(wèn)自己:你知道你自己的夢(mèng)想是什么嗎?答曰,不知道。所以,正因?yàn)槟悴⒉恢雷?..
    林茉秣閱讀 215評(píng)論 0 0
  • 琵琶聲過(guò)枯葉落, 風(fēng)雨未至花滿樽。 掌心化雪梅堪折, 指尖春水一城花。
    璃茉vae閱讀 131評(píng)論 0 2

友情鏈接更多精彩內(nèi)容