Think before you speak, read before you think.

vagrant使用笔记

安装 vagrant

https://www.vagrantup.com/downloads.html

如果网络不好首先下载 box ,比如 centos6.box centos7.box

注册一个 box,经常用的可以添加为 base,做为 default box

vagrant box add base ~/osx-dmg/centos7.box
vagrant box add centos6 ~/osx-dmg/centos6.box
vagrant box add centos7 ~/osx-dmg/centos7.box

查看已经导入的 box

vagrant box list
#或者
ls -l ~/.vagrant.d/boxes

如果网络条件好,添加官方的 centos/7 (注意写法)

$ vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
    box: URL: https://vagrantcloud.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop

Enter your choice: 2

工作目录为 ~/test,进入目录,创建一台 centos7 的机器,--minimal 生成的文件没有注释

vagrant init centos/7 --minimal

启动

vagrant up

连接 vagrant

vagrant ssh

执行 id 命令

vagrant ssh -c 'id'

查看当前 vagrant 管理的机器,最前面为机器 id

                                                                                
vagrant global-status

修剪 vagrant 已经坏掉的机器

                                                   
vagrant global-status --prune

如果有不需要的 box

                                                                
vagrant destroy id

准备多台虚拟机环境

  config.vm.define "web" do |web|                                               
    web.vm.box = "centos6"                                                      
  end                                                                           
                                                                                
  config.vm.define "db" do |db|                                                 
    db.vm.box = "centos6"                                                       
  end

查看机器的状态

vagrant status

查看 ssh 信息

vagrant ssh-config

关闭 vm

vagrant halt

不需要的 box 删除

vagrant box remove centos/7

vagrant 官方 centos7 box:

https://vagrantcloud.com/centos/7/

centos 提供的 box:

http://cloud.centos.org/centos/7/vagrant/x86_64/images/

windows 10 将 virtualbox 换为 hyperv
上面获取 box 步骤时选 hyperv, 将原来 virtualbox 的相关 box 删除

vagrant.exe box  remove centos/7 --provider virtualbox --all

切到目录下,删除相关的 vm
vagrant.exe destroy
启动

vagrant up --provider hyperv

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *