使用Vagrant快速搭建Linux環境
什麼是Vagrant
我們知道,如果要在Windows系統下搭建Linux開發環境的話,一種選擇是安裝虛擬機器軟體,通過其虛擬出想要的系統。而Vagrant就是用來管理這種虛擬系統的第三方工具,通過簡單的幾句命令列,就可以快速的安裝系統,同時,通過配置檔案還可以管理網路、共享資料夾等內容。
更為重要的是,當配置好這個系統之後,還可以將這個系統進行打包,分享給他人使用。
準備工作
要使用Vagrant這個工具,首先需要安裝一個虛擬機器軟體,常見的虛擬機器軟體包括了virtualbox和VMware,但後者是要收費的,不建議使用,優選選擇virtualbox。
關於virtulbox和Vagrant的安裝工具,這裡就不具體介紹,注意在安裝完之後,為了方便使用,需要將vagrant新增到“Path”環境變數中。
使用Vagrant
新增box
首先,安裝Linux系統必不可少的是它的系統檔案,而這個檔案最好從網上下載到本地,可以從box映象源進行選擇下載。由於某種原因,最好使用科學上網的方法,不然會下載不動的。
在E盤下(根據自己情況)新建”vagrant”資料夾,在其目錄下新建”box”資料夾,並將下載好的box檔案”centos.box”放在”box”資料夾。
cd vagrant //進到vagrant資料夾
vagrant box add --name centos box\centos.box
//--name指定box的系統名稱,在Vagrant的配置檔案中將會使用到
而使用命令
vagrant box list
可以檢視已經新增的box,如下圖
配置Vagrantfile
在系統啟動之前,需要進行配置,使用命令
vagrant init centos
對”centos”進行初始,在”vagrant”目錄下會生成Vagrantfile檔案,檔案的內容如下
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "centos"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
可以看到對於常用的配置,這個檔案都有列出,並進行了解釋。這裡對某些配置做簡單介紹。
Vagrant.configure("2") do |config| # 配置開始
config.vm.box = "centos" # 對系統'centos'的配置
# 網路設定
# config.vm.network "forwarded_port", guest: 80, host: 8080
# 這裡是進行埠轉發,host: 8080 --> guest: 80
config.vm.network "public_network"
# 配置成公共網路,就像實際存在的物理電腦一致,IP地址將有網路中的路由器分配
# 同步共享資料夾
config.vm.synced_folder "./data", "/home/vagrant"
# 這裡配置兩個資料夾,前一個是本地資料夾,後一個是centos系統下的資料夾
# 因此需要在"vagrant"目錄下新建"data"資料夾
# 顯示和記憶體設定
config.vm.provider "virtualbox" do |vb| # 這裡指定虛擬機器軟體virtualbox
vb.gui = false # 是否顯示gui
vb.memory = "1024" # 虛擬機器記憶體設定
end
end # 配置結束
以上就是單個虛擬機器的配置,使用命令
vagrant up
啟動該虛擬機器,如圖
同樣,Vagrantfile可以配置多虛擬機器啟動,其大致機構如下
Vagrant.configure("2") do |config|
# 虛擬機器centos
config.vm.define :centos do |centos|
centos.vm.box="centos"
centos.vm.network "public_network"
end
# 虛擬機器ubuntu
config.vm.define :ubuntu do |ubuntu|
ubuntu.vm.box="ubuntu"
end
end
這樣的話在啟動時,可以指定虛擬機器啟動,比如
vagrant up centos
可以只啟動centos系統,不指定的話,會啟動全部的虛擬機器。
更多的配置選項,可以參考中文文件進行更多的配置學習。
登入系統
在成功系統之後,輸入命令
vagrant ssh
後,輸入密碼,即可登入成功,如圖
預設的系統使用者名稱和密碼
user: vagrant
passwd: vagrant //管理員密碼也是這個
使用命令
ip addr
獲取虛擬機器的IP地址,如圖
可以看到虛擬在區域網中的地址為”192.168.199.215”,使用該地址,我們也可以從區域網中的其他電腦進行登入,如圖
關於檔案共享就更簡單了,之前我們配置了
config.vm.synced_folder "./data", "/home/vagrant"
在登入之後,進行一下測試
cd ~
pwd
touch guset.txt
如圖
在本地目錄下,可以看到
因此,我們可以在Windows系統下選用自己喜歡的編輯器對檔案進行編輯,而且能夠快速地部署到Linux系統下。
系統打包
如果想要分享你配置好了的Linux系統,這時需要將該系統進行打包,打包前需要將虛擬機器關閉,使用命令
vagrant halt
將虛擬機器關機。使用命令
vagrant package
對系統進行打包,如圖
在”vagrant”目錄下多了一個.box檔案,如圖
總結
使用Vagrant可以方便管理和配置虛擬機器系統,關於更多的用法可以參考Vagrant官網文件或者中文文件。
還有,關於Vagrantfile檔案最好使用git進行管理,方便學習使用。