Vagrant安装与使用

一、简介

VirtualBox官网
Vagrant官网

下载

准备好Vagrant安装包,官网下载地址
准备好VirtualBox安装包,官网下载地址

安装

Vagrant和VirtualBox,双击安装,一路傻瓜化完成。

添加一个box

  • 添加下载好的镜像
# name可以自定义
vagrant box add {name} {url}

例如

vagrant box add centos/7 D:/vagrant/vagrant.box
  • ​添加远程镜像
    Vagrant可以从这里https://atlas.hashicorp.com/boxes/search 下载各种Vagrant映像文件。
vagrant box add centos/7

通过指定的URL添加远程box

vagrant box add CentOS7.1 file:///D:/Work/VagrantBoxes/CentOS-7.1.1503-x86_64-netboot.box
  • 使用Vagrantfile添加镜像(推荐)
mkdir centos7 && cd centos7/
vagrant init centos/7

此命令会在当前目录创建一个名为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://vagrantcloud.com/search.
  config.vm.box = "centos/7"

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

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt 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

当在此目录启动Vagrant后,Vagrant会从互联网下载“centos/7”这个box到本地,并使用它作为VM的映像。
要搜索可用的box,查看这里: https://atlas.hashicorp.com/boxes

命令

vagrant box add  添加box的操作
vagrant init    初始化box的操作,会生成vagrant的配置文件Vagrantfile
vagrant up  启动本地环境
vagrant ssh  通过 ssh 登录本地环境所在虚拟机,exit可以退出登录
vagrant halt    关闭本地环境
vagrant suspend  暂停本地环境
vagrant resume   恢复本地环境
vagrant reload   修改了 Vagrantfile 后,使之生效(相当于先 halt,再 up)
vagrant destroy     彻底移除本地环境
vagrant box list     显示当前已经添加的box列表
vagrant box remove   删除相应的box
vagrant package  打包命令,可以把当前的运行的虚拟机环境进行打包
vagrant plugin   用于安装卸载插件
vagrant status   获取当前虚拟机的状态
vagrant global-status    显示当前用户Vagrant的所有环境状态

注意

vagrant ssh登录到虚拟机后可使用 su root 切换到root用户,密码是vagrant
vagrant box文件存储在~/下

你可能感兴趣的:(Vagrant安装与使用)