使用Vagrant和VirtualBox一步步地建立一個Base Box

box集合  http://www.vagrantbox.es/git

Vagrant和VirtualBox軟件的安裝步驟省略,去官網下載最新的版本而後下一步下一步地安裝就好了,和正常的安裝軟件沒有什麼區別。下面看看利用vagrant和virtualbox若是一步步打造出本身的可複用性強的開發環境。github

開始前確保您的計算機支持虛擬化技術,在BIOS中能夠開啓。shell

1、在virtualbox中新建一個虛擬機

我這裏使用的是CentOS-6.5-x86_64-minimal.iso這個鏡像文件來安裝的,1G內存,8G虛擬硬盤的配置(後面能夠修改)。虛擬機名爲centos。ubuntu

安裝好後該虛擬機已經自帶了ssh服務,若是沒有,須要本身先進行安裝。centos

開啓虛擬機,並登錄root用戶。緩存

2、配置虛擬機

建立admin用戶組網絡

[root@localhost ~]# groupadd admin

建立vagrant用戶並添加到admin用戶組ssh

[root@localhost ~]# useradd -g admin vagrant

爲vagrant用戶設置密碼,密碼爲vagrant(會提示密碼太弱,能夠不用管它)工具

[root@localhost ~]# passwd vagrant

修改sudoers文件ui

[root@localhost ~]# vi /etc/sudoers

在該文件添加下面兩行代碼,並註釋掉requiretty行

Defaults env_keep="SSH_AUTH_SOCK"
%admin ALL=NOPASSWD: ALL

配置網卡:將ONBOOT=no 改成 ONBOOT=yes

[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=08:00:27:25:4A:9A
TYPE=Ethernet
UUID=751ae3bb-eb75-4f60-b5c3-7f1afa1dfce1
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp

配置好網卡後須要重啓網絡服務:

[root@localhost ~]# service network restart

3、設置端口轉發

配置virtualbox端口轉發(NAT模式)

主機ip127.0.0.1

主機端口22(或者其餘未被佔用端口,最好仍是設置成22)

子系統ip能夠在虛擬經過ifconfg命令查看到,即eth0網卡的ip地址

子系統端口爲22

設置好端口轉發規則以後,就能在本地的命令行工具(Xshell,putty..)裏經過命令 ssh root@127.0.0.1 鏈接到該虛擬機。

4、添加vagrant公鑰,使vagrant用戶能夠免密碼ssh鏈接該虛擬機

在虛擬機裏切換到vagrant用戶

[root@localhost ~]# su vagrant
[vagrant@localhost root]$ cd ~
[vagrant@localhost ~]$

而後執行:

[vagrant@localhost ~]$ mkdir ~/.ssh/
[vagrant@localhost ~]$ cd ~/.ssh
[vagrant@localhost .ssh]$ sudo yum -y install wget
[vagrant@localhost .ssh]$ wget http://github.com/mitchellh/vagrant/raw/master/keys/vagrant
[vagrant@localhost .ssh]$ wget http://github.com/mitchellh/vagrant/raw/master/keys/vagrant
[vagrant@localhost .ssh]$ mv vagrant.pub authorized_keys

清理緩存

[vagrant@localhost .ssh]$ sudo yum clean all

而後關閉此虛擬機,經過virtualbox將其電源關閉

5、打包生成box文件

在本地命令行進入該虛擬機目錄

cd Users\Admin\VirtualBox VMs\centos\

進行打包

vagrant package --output mycentos.box --base centos

完成以後你將會在C:\Users\Admin\VirtualBox VMs\centos目錄下看到一個mycentos.box文件,個人這個文件有544M。

VirtualBox VMs這個目錄能夠在virtualbox全局設定-常規裏進行修改

到此一個Base Box就製做完成了。

這個box是很乾淨的,而後能夠在這個box的基礎上去搭建本身的開發環境,好比lnmp等等。再次進行打包,最後一個帶開發環境的box就作好了,能夠本身用也能夠分發給其餘小夥伴使用,不用之後每次都要去搭建一遍環境。

6、附上vagrant經常使用操做命令

vagrant box list 查看在vagrant裏已經安裝了哪些box

vagrant box add name boxname.box 添加一個本地的box鏡像到vagrant 這個box鏡像能夠本身製做也能夠使用網上的(vagrant box add ubuntu ubuntu/trusty64)

vagrant init name 初始化一個虛擬機,這個name是經過vagrant box list查看

vagrant up 啓動虛擬機 在virtualbox中能夠看到啓動後的虛擬機,第一次啓動時時間會長一點

vagrant ssh 鏈接虛擬機

vagrant reload 重啓虛擬機

vagrant status 查看虛擬機當前狀態

7、常見問題處理

問題1:

default: in which case you may ignore this message.
==> default: Mounting shared folders...
default: /vagrant => E:/vagrantbox/centos67
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=500,gid=500 vagrant /vagrant

The error output from the command was:

mount: unknown filesystem type 'vboxsf'

上面這樣的問題能夠執行下命令 vagrant plugin install vagrant-vbguest ,而後重啓vagrant reload來解決。

問題2:

vagrant up的時候出現 「rsync」 could not be found on your PATH. Make sure that rsync is properly installed on your system and available on the PATH. 出現這樣的錯誤

找到C:\Users\admin.vagrant.d\boxes\centos7\0\virtualbox相似這樣的目錄下面的Vagrantfile文件

修改type配置項: config.vm.synced_folder 「.」, 「/vagrant」, type: 「virtualbox」

相關文章
相關標籤/搜索