Win7安裝Vagrant

前言

本來覺得Vagrant安裝很容易,沒想到整了大半天。網上看到的文章比較舊了,整理一篇新一些的,但願對像我同樣遇到問題的人有所幫助。git

安裝Virtualbox

去VB官方網站下載VirtualBox的Windows安裝包。一路Next就OK了。VirtualBox的硬件加速很重要,Windows 7 默認是關閉VT-x(Intel芯片),須要去BIOS裏開啓。網絡

安裝Vagrant

去Vagrant官網下載Windows下的安裝包,一樣是一路Next,安裝完畢後須要重啓,正好藉此機會去BIOS裏查看下是否開啓了虛擬化技術。ssh

添加路徑到Path

有兩個路徑要手動添加:ide

  1. Virtualbox的運行程序路徑,Vagrant腳本運行Virtualbox和VBoxManager的時候須要。
  2. ssh.exe程序所在路徑,ssh.exe在安裝git的時候會一塊兒安裝,若是尚未安裝git,須要下載安裝git

添加Box

雖然能夠網絡方式安裝Box,可是,天朝的狀況也是知道的,所以,保險起見,下載下來先。下載地址:http://www.vagrantbox.es/網站

下載完畢,假設把鏡像存在d:\boxes下,運行命令:this

vagrant box add trusty64 d:\boxes\trusty-server-cloudimg-amd64-vagrant-disk1.box

其中,trusty64是盒子在vagrant管理的盒子列表中的名字,後面跟的路徑是盒子文件所在的路徑。正常狀況下,能夠看到以下輸出:編碼

==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'trusty64' (v0) for provider:
    box: Unpacking necessary files from: file://d:/boxes/trusty-server-cloudi
mg-amd64-vagrant-disk1.box
    box: Progress: 100% (Rate: 19.0M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'trusty64' (v0) for 'virtualbox'!

初始化工做區

工做區能夠理解爲項目在本機的代碼,Vagrant會將這個代碼掛載到啓動的虛擬機的相應目錄下,這就實現了,在本機編碼,而代碼在虛擬機裏運行的方法,再也不須要ftp或者別的方式去反覆部署代碼。假設工做區是d:\va, 在命令行下切換到這個目錄,而後執行vagrant init trusty64, 其中trusty64就是在上一步建立的盒子名稱,該命令就是使用trusty64盒子初始化當前目錄爲vagrant的工做區。命令執行成功,會輸出以下信息:加密

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

在當前目錄下,會有一個Vagrantfile文件。.net

運行

萬事具有,能夠運行一個實例了。使用命令 Vagrant up,正常狀況下,會有以下輸出:命令行

D:\va>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: va_default_1456111656613_62745
==> default: Clearing any previously set forwarded ports...
==> 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: Warning: Remote connection disconnect. Retrying...
    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 o
f
    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 t
he
    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.0
==> default: Mounting shared folders...
    default: /vagrant => D:/va

管理虛擬機實例

如下幾個命令,用來管理vagrant啓動的虛擬機。

  1. 列出當前虛擬機狀態:vagrant status
  2. 關閉當前虛擬機: vagrant halt
  3. 暫停當前虛擬機:vagrant suspend
  4. 重器,以使配置文件的更新生效: vagrant reload

錯誤排查

按照上述步驟,應該能一次搞定,若是仍是遇到了問題,請先查看博文:http://my.oschina.net/u/248080/blog/618060

登錄虛擬機

安裝完畢,運行起來後,總不能什麼都不安裝配置吧。勢必須要ssh登錄。用Windows自帶命令行也是能夠的,執行vagrant ssh就能夠了。

putty

若是習慣用putty,那須要先把Vagrant自帶的key給轉化成putty須要的格式:

  1. 運行puttygen.exe,Conversions菜單>>Import key
  2. Vagrant生成的私鑰在工做區目錄下的.vagrant\machines\default\virtualbox\private_key
  3. 導入這個私鑰到puttygen裏,點Save private key,轉存成ppk格式。
  4. 啓動putty.exe,在Connection>>SSH>>Auth裏的選擇剛剛生成的ppk文件
  5. Host爲127.0.0.1,端口是2222,點Open
  6. ssh的用戶名是vagrant

Secure Shell Client

若是習慣用Secure Shell Client,經過以下步驟添加一個公鑰:

  1. 啓動Secure Shell Client
  2. 進入Edit>>Settings>>Global Settings>>User Authentication>>Keys
  3. 點Generate New按鈕,生成一個RSA 2048加密的密鑰對。
  4. 生成完畢,點export導出。
  5. 再次用puttygen導入剛剛導出的私鑰文件,複製文本框裏的公鑰字符串,粘貼到虛擬機的.ssh/authorized_keys文件裏
  6. 一樣的方法,用127.0.0.1,端口2222,用戶名vagrant登錄。
相關文章
相關標籤/搜索