在最小化安裝的centos7中安裝vagrant+virtualbox創建虛擬主機教程


前言

由於工作和學習的需要,在使用centos7時,也需要使用虛擬化創建主機,會經常使用vagrant管理工具來創建虛擬主機,所以寫了這個總結,回顧一下。

特別說明:如果你只是想成功的快速安裝好這個環境的情況下,請閱讀完全文后在操作,其中有些依賴我沒有同時安裝,謝謝!

1、前期准備

1)vagrant

Vagrant是一個基於Ruby的工具,用於創建和部署虛擬化開發環境。它 使用Oracle的開源VirtualBox虛擬化系統,使用 Chef創建自動化虛擬環境。(引用百度百科https://baike.baidu.com/item/Vagrant/1321946?fr=aladdin)

詳細的說明,請查詢官網文檔,軟件下載地址:https://www.vagrantup.com/

2)VirtualBox

VirtualBox is a powerful x86 and AMD64/Intel64 virtualization product for enterprise as well as home use. Not only is VirtualBox an extremely feature rich, high performance product for enterprise customers, it is also the only professional solution that is freely available as Open Source Software under the terms of the GNU General Public License (GPL) version 2.(官網說明)

軟件下載地址:https://www.virtualbox.org/

這里我們需要的是在官網下載rpm的安裝包的安裝方式,當然也可以使用yum安裝VirtualBox,下邊我會放上yum源(往后翻翻哦)。

3)檢查電腦的bios中的虛擬化是不是打開了,這個電腦的型號比較多,請自行百度。centos中查看虛擬化的命令如下:

查看cpu是否支持

# grep -E 'svm|vmx' /proc/cpuinfo

- vmx is for Intel processors

- svm is for AMD processors

4)VirtualBox的yum源配置(官方地址:https://www.virtualbox.org/wiki/Linux_Downloads,在頁面的最下邊)

vim /etc/yum.repos.d/vbox.repo

[virtualbox]
name=Oracle Linux / RHEL / CentOS-$releasever / $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/el/$releasever/$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc

 

2、安裝部署

1、當上邊配置完成或者軟件下載完成后直接安裝,安裝命令如下:

[root@localhost ~]# rpm -ivh VirtualBox-6.0-6.0.8_130520_el7-1.x86_64.rpm
警告:VirtualBox-6.0-6.0.8_130520_el7-1.x86_64.rpm: 頭V4 DSA/SHA1 Signature, 密鑰 ID 98ab5139: NOKEY
錯誤:依賴檢測失敗:
        libGL.so.1()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libICE.so.6()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libSDL-1.2.so.0()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libSM.so.6()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libX11-xcb.so.1()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libX11.so.6()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libXcursor.so.1()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libXext.so.6()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libXmu.so.6()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libXrender.so.1()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libXt.so.6()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libfontconfig.so.1()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libopus.so.0()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libpng15.so.15()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libpng15.so.15(PNG15_0)(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libvpx.so.1()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
        libxcb.so.1()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要

使用rpm包安裝的時候,會出現上邊的錯誤,缺少依賴,這是linux的特性,下邊我們安裝依賴:

yum -y install libGL libICE libSM libXcursor libXmu libXrender libXt libpng libvpx SDL fontconfig opus
安裝依賴庫

然后就可以直接安裝vbox(這里簡寫了)了

[root@localhost ~]# rpm -ivh VirtualBox-6.0-6.0.8_130520_el7-1.x86_64.rpm
警告:VirtualBox-6.0-6.0.8_130520_el7-1.x86_64.rpm: 頭V4 DSA/SHA1 Signature, 密鑰 ID 98ab5139: NOKEY
准備中...                          ################################# [100%]
正在升級/安裝...
   1:VirtualBox-6.0-6.0.8_130520_el7-1################################# [100%]

Creating group 'vboxusers'. VM users must be member of that group!

This system is currently not set up to build kernel modules.
Please install the gcc make perl packages from your distribution.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
    kernel-devel kernel-devel-3.10.0-862.el7.x86_64
This system is currently not set up to build kernel modules.
Please install the gcc make perl packages from your distribution.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
    kernel-devel kernel-devel-3.10.0-862.el7.x86_64

There were problems setting up VirtualBox.  To re-start the set-up process, run
  /sbin/vboxconfig
as root.  If your system is using EFI Secure Boot you may need to sign the
kernel modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) before you can load
them. Please see your Linux system's documentation for more information.

到這里,還是缺少東西,提示我們我們linux核心上的支持相應虛擬化的模塊沒有添加,但是添加這些模塊的也有相應的依賴,解決依賴如下:

yum -y install gcc gcc-c++ kernel-devel

安裝完成后,執行如下命令,重新加載相應的內核模塊

[root@localhost ~]# vboxconfig
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Starting VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.

其實,在加載相應的模塊的時候,我在官網的教程上也看到這樣的一個命令,同樣是可以的。

rcvboxdrv setup
系統核心加載相應的虛擬化模塊,具體的原因參考vbox官網的使用手冊。

2、安裝vagrant,這個安裝就沒有那么多的問題了,直接執行如下命令。

[root@localhost ~]# rpm -ivh vagrant_2.2.5_x86_64.rpm
准備中...                          ################################# [100%]
正在升級/安裝...
   1:vagrant-1:2.2.5-1                ################################# [100%]

3、關於vagrant的使用

查看幫助文檔

[root@localhost ~]# vagrant --help
Usage: vagrant [options] <command> [<args>]

    -v, --version                    Print the version and exit.
    -h, --help                       Print this help.

Common commands:
     box             manages boxes: installation, removal, etc.
     cloud           manages everything related to Vagrant Cloud
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     port            displays information about guest port mappings
     powershell      connects to machine via powershell remoting
     provision       provisions the vagrant machine
     push            deploys code in this environment to a configured destination
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     snapshot        manages snapshots: saving, restoring, etc.
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     upload          upload to machine via communicator
     validate        validates the Vagrantfile
     version         prints current and latest Vagrant version
     winrm           executes commands on a machine via WinRM
     winrm-config    outputs WinRM configuration to connect to the machine

For help on any individual command run `vagrant COMMAND -h`

Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.

最常用的命令順序是:

vagrant box 【url】 下載鏡像*.box

vagrant init  box-name  虛擬機初始化,創建vagrantfile文件

vagrant up 啟動虛擬機

vagrant  ssh   遠程連接虛擬機

vagrant halt  關閉虛擬機

vagrant destory  刪除虛擬機

由於國外的box鏡像下載太不好用,導致經常下載失敗,建議使用清華的源,命令如下:

vagrant box add https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box --name ubuntu/bionic
下載box的虛擬鏡像
vagrant init ubuntu/bionic
  初始化鏡像
 
vagrant up
  啟動虛擬機

vagrant ssh
  通過ssh連接虛擬機

vagrant halt
  關閉虛擬機

注:其實這里如果你沒有相應的box源的話,也可以通過瀏覽器下載完成后,復制到centos中,在執行init初始化,也是可以的。

到這里基本的安裝就結束了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM