0. vagrant+vbox创建centos7虚拟机

准备工作

安装 virthbox和vagrant 下载地址:

常用vagrant命令

$ vagrant init      # 初始化
$ vagrant up        # 启动虚拟机
$ vagrant halt      # 关闭虚拟机
$ vagrant reload    # 重启虚拟机
$ vagrant ssh       # SSH 至虚拟机
$ vagrant suspend   # 挂起虚拟机
$ vagrant resume    # 唤醒虚拟机
$ vagrant status    # 查看虚拟机运行状态
$ vagrant destroy   # 销毁当前虚拟机
#box管理命令
$ vagrant box list    # 查看本地box列表
$ vagrant box add     # 添加box到列表
$ vagrant box remove  # 从box列表移除

1. 添加本地镜像

vagrant box add centos/7 /Users/qiuzhiqing/0-my-soft/1-vm/vbox/vagrant-centos-7.box

2. 创建虚拟机脚本

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.require_version ">= 1.6.0"

boxes = [
    {
        :name => "rabbitmq-host",
        :eth1 => "192.168.205.10",
        :mem => "1024",
        :cpu => "1"
    }
]

Vagrant.configure(2) do |config|

  config.vm.box = "centos/7"
  boxes.each do |opts|
    config.vm.define opts[:name] do |config|
      config.vm.hostname = opts[:name]
      config.vm.provider "vmware_fusion" do |v|
        v.vmx["memsize"] = opts[:mem]
        v.vmx["numvcpus"] = opts[:cpu]
      end
      config.vm.provider "virtualbox" do |v|
        v.customize ["modifyvm", :id, "--memory", opts[:mem]]
        v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
      end
      config.vm.network :private_network, ip: opts[:eth1]
    end
  end
#  config.vm.synced_folder "./labs", "/home/vagrant/labs"
#  config.vm.provision "shell", privileged: true, path: "./setup.sh"
end

3.启动虚拟机

vbox_centos % vagrant up
Bringing machine ‘docker-host‘ up with ‘virtualbox‘ provider...
==> rabbitmq-host: Importing base box ‘centos/7‘...
==> rabbitmq-host: Matching MAC address for NAT networking...
==> rabbitmq-host: Setting the name of the VM: vbox_centos_rabbitmq-host_1578748388229_14016
==> rabbitmq-host: Clearing any previously set network interfaces...
==> rabbitmq-host: Preparing network interfaces based on configuration...
    rabbitmq-host: Adapter 1: nat
    rabbitmq-host: Adapter 2: hostonly
==> rabbitmq-host: Forwarding ports...
    rabbitmq-host: 22 (guest) => 2222 (host) (adapter 1)
......
==> rabbitmq-host: Setting hostname...
==> rabbitmq-host: Configuring and enabling network interfaces...
==> rabbitmq-host: Rsyncing folder: /Users/qiuzhiqing/0-my-soft/1-vm/vbox_centos/ => /vagrant

4.查看状态

vbox_centos % vagrant status
Current machine states:

rabbitmq-host               running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

5. ssh登陆

vbox_centos % vagrant ssh rabbitmq-host
[ ~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:ca:e4:8b brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic eth0
       valid_lft 86182sec preferred_lft 86182sec
    inet6 fe80::5054:ff:feca:e48b/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:7a:8d:6e brd ff:ff:ff:ff:ff:ff
    inet 192.168.205.10/24 brd 192.168.205.255 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe7a:8d6e/64 scope link
       valid_lft forever preferred_lft forever

6. 关闭虚拟机并且查看状态

vbox_centos % vagrant halt rabbitmq-host
==> rabbitmq-host: Attempting graceful shutdown of VM...
 vbox_centos % vagrant status
Current machine states:

rabbitmq-host               poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`
 vbox_centos %

相关推荐