基于VirtualBox和Vagrant搭建虚拟机

一、VirtualBox和vagrant的安装

VirtualBox 是一款开源的虚拟机软件,和VMWare是同类型的软件,用于在当前的电脑上构建一台虚拟机,在这台虚拟机上可以安装系统和软件,与真实的电脑一般无二。

Vagrant用于创建和部署虚拟化开发环境。它使用Oracle的开源VirtualBox虚拟化系统.。

注意的是这两个的版本,如果匹配不上在运行vagrant命令时就会出现版本问题,笔者使用的是windows7系统,这里提供上面两个安装包的百度云的地址:

1、VirtualBox

链接:https://pan.baidu.com/s/1Qcb9DIYJV7LUG32VAMPLCQ
提取码:y2sx

2、Vagrant

链接:https://pan.baidu.com/s/1AlWX6mkBvem4XYsLS_Ze_Q
提取码:0xn9

(一)VirtualBox的安装

1、下载完毕后如下

基于VirtualBox和Vagrant搭建虚拟机

2、点击安装

基于VirtualBox和Vagrant搭建虚拟机

 

 

 不需要改变什么,只需要按照步骤,一直next进行安装即可。

(二)Vagrant的安装

1、下载后如下

基于VirtualBox和Vagrant搭建虚拟机

2、点击安装

基于VirtualBox和Vagrant搭建虚拟机

这与VirtualBox的安装一样,只是不断的next就可以进行安装完毕,这个安装完毕后会进行重启电脑,在重启电脑后可以打开cmd命令行窗口进行验证看是否已经可以使用了。

基于VirtualBox和Vagrant搭建虚拟机

可以看到并没有出现什么问题,可以正常的使用了,下面是vagrant对应的命令。

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.
     connect         connect to a remotely shared Vagrant environment
     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 Vagrant
file
     login           log in to HashiCorp's Atlas
     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 destinatio
n
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configurati
on
     resume          resume a suspended vagrant machine
     share           share your Vagrant environment with anyone in the world
     snapshot        manages snapshots: saving, restoring, etc.
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machi
ne
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     validate        validates the Vagrantfile
     version         prints current and latest Vagrant version

二、创建虚拟机

(一)使用Vagrant创建centos虚拟机

1、新建文件夹

可以先创建一个文件夹,比如centos7:

 基于VirtualBox和Vagrant搭建虚拟机

 2、Vagrant创建虚拟机

  • 下载box

box就是我们需要安装的虚拟机操作系统,Vagrant开源社区提供了许多已经打包好的操作系统:
官方镜像:https://vagrantcloud.com/boxes/search

第三方仓库:http://www.vagrantbox.es/ 

当从这上面下载box后,将其拷贝到上述新建文件夹中,如下所示:

基于VirtualBox和Vagrant搭建虚拟机

  • vagrant box add --name base vagrant-centos-7.box

将下载好的box通过下面命令进行添加:

基于VirtualBox和Vagrant搭建虚拟机

  • vagrant box list

查看是否添加成功

基于VirtualBox和Vagrant搭建虚拟机

  • vagrant init base

在命令行下进入到该目录,然后执行

vagrant init base

基于VirtualBox和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 = "base"  #base就是刚才的镜像名称

  # 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.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # 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 up

启动虚拟机

基于VirtualBox和Vagrant搭建虚拟机

3、连接虚拟机

其实当你创建好虚拟机后,在你的VirtureBox中已经有这个虚拟机的信息了。

基于VirtualBox和Vagrant搭建虚拟机

 此时你直接点击登陆即可,默认的用户名和密码是:vagrant

基于VirtualBox和Vagrant搭建虚拟机

 或者你也可以通过xshell进行连接,这些都是可以的。

 

上一篇:laravel 安装 homestead


下一篇:九析带你玩转 linux 系列