Vagrant.configure("2") do |config| # 定义虚拟机数量 vms = Array(1..3) vms.each do |i| config.vm.define "centos8-#{i}"do |node| # 设置虚拟机的Box node.vm.box = "centos8" # 设置虚拟机的主机名 node.vm.hostname="centos8-#{i}" # 设置虚拟机的IP node.vm.network "private_network", ip:"172.24.20.4#{i}", hostname:true # 虚拟化设置cpu/mem node.vm.provider "virtualbox"do |v| # 设置虚拟机的名称 v.name = "centos8-#{i}" # 设置虚拟机的内存大小 v.memory = 1024 # 设置虚拟机的CPU个数 v.cpus = 1 end end end end
up log
mvpbang@mvpbang c8-4x % vagrant up
Bringing machine 'centos8-1' up with 'virtualbox' provider...
Bringing machine 'centos8-2' up with 'virtualbox' provider...
Bringing machine 'centos8-3' up with 'virtualbox' provider...
==> centos8-1: Importing base box 'centos8'...
==> centos8-1: Matching MAC address for NAT networking...
==> centos8-1: Setting the name of the VM: centos8-1
==> centos8-1: Clearing any previously set network interfaces...
==> centos8-1: Preparing network interfaces based on configuration...
centos8-1: Adapter 1: nat
centos8-1: Adapter 2: hostonly
==> centos8-1: Forwarding ports...
centos8-1: 22 (guest) => 2222 (host) (adapter 1)
==> centos8-1: Running 'pre-boot' VM customizations...
==> centos8-1: Booting VM...
==> centos8-1: Waiting for machine to boot. This may take a few minutes...
centos8-1: SSH address: 127.0.0.1:2222
centos8-1: SSH username: vagrant
centos8-1: SSH auth method: private key
centos8-1:
centos8-1: Vagrant insecure key detected. Vagrant will automatically replace
centos8-1: this with a newly generated keypair for better security.
centos8-1:
centos8-1: Inserting generated public key within guest...
centos8-1: Removing insecure key from the guest if it's present...
centos8-1: Key inserted! Disconnecting and reconnecting using new SSH key...
==> centos8-1: Machine booted and ready!
==> centos8-1: Checking for guest additions in VM...
centos8-1: No guest additions were detected on the base box for this VM! Guest
centos8-1: additions are required for forwarded ports, shared folders, host only
centos8-1: networking, and more. If SSH fails on this machine, please install
centos8-1: the guest additions and repackage the box to continue.
centos8-1:
centos8-1: This is not an error message; everything may continue to work properly,
centos8-1: in which case you may ignore this message.
==> centos8-1: Setting hostname...
==> centos8-1: Configuring and enabling network interfaces...
==> centos8-1: Rsyncing folder: /Users/mvpbang/vagrants/c8-4x/ => /vagrant
==> centos8-2: Importing base box 'centos8'...
==> centos8-2: Matching MAC address for NAT networking...
==> centos8-2: Setting the name of the VM: centos8-2
==> centos8-2: Fixed port collision for 22 => 2222. Now on port 2200.
==> centos8-2: Clearing any previously set network interfaces...
==> centos8-2: Preparing network interfaces based on configuration...
centos8-2: Adapter 1: nat
centos8-2: Adapter 2: hostonly
==> centos8-2: Forwarding ports...
centos8-2: 22 (guest) => 2200 (host) (adapter 1)
==> centos8-2: Running 'pre-boot' VM customizations...
==> centos8-2: Booting VM...
==> centos8-2: Waiting for machine to boot. This may take a few minutes...
centos8-2: SSH address: 127.0.0.1:2200
centos8-2: SSH username: vagrant
centos8-2: SSH auth method: private key
centos8-2:
centos8-2: Vagrant insecure key detected. Vagrant will automatically replace
centos8-2: this with a newly generated keypair for better security.
centos8-2:
centos8-2: Inserting generated public key within guest...
centos8-2: Removing insecure key from the guest if it's present...
centos8-2: Key inserted! Disconnecting and reconnecting using new SSH key...
==> centos8-2: Machine booted and ready!
==> centos8-2: Checking for guest additions in VM...
centos8-2: No guest additions were detected on the base box for this VM! Guest
centos8-2: additions are required for forwarded ports, shared folders, host only
centos8-2: networking, and more. If SSH fails on this machine, please install
centos8-2: the guest additions and repackage the box to continue.
centos8-2:
centos8-2: This is not an error message; everything may continue to work properly,
centos8-2: in which case you may ignore this message.
==> centos8-2: Setting hostname...
==> centos8-2: Configuring and enabling network interfaces...
==> centos8-2: Rsyncing folder: /Users/mvpbang/vagrants/c8-4x/ => /vagrant
==> centos8-3: Importing base box 'centos8'...
==> centos8-3: Matching MAC address for NAT networking...
==> centos8-3: Setting the name of the VM: centos8-3
==> centos8-3: Fixed port collision for 22 => 2222. Now on port 2201.
==> centos8-3: Clearing any previously set network interfaces...
==> centos8-3: Preparing network interfaces based on configuration...
centos8-3: Adapter 1: nat
centos8-3: Adapter 2: hostonly
==> centos8-3: Forwarding ports...
centos8-3: 22 (guest) => 2201 (host) (adapter 1)
==> centos8-3: Running 'pre-boot' VM customizations...
==> centos8-3: Booting VM...
==> centos8-3: Waiting for machine to boot. This may take a few minutes...
centos8-3: SSH address: 127.0.0.1:2201
centos8-3: SSH username: vagrant
centos8-3: SSH auth method: private key
centos8-3:
centos8-3: Vagrant insecure key detected. Vagrant will automatically replace
centos8-3: this with a newly generated keypair for better security.
centos8-3:
centos8-3: Inserting generated public key within guest...
centos8-3: Removing insecure key from the guest if it's present...
centos8-3: Key inserted! Disconnecting and reconnecting using new SSH key...
==> centos8-3: Machine booted and ready!
==> centos8-3: Checking for guest additions in VM...
centos8-3: No guest additions were detected on the base box for this VM! Guest
centos8-3: additions are required for forwarded ports, shared folders, host only
centos8-3: networking, and more. If SSH fails on this machine, please install
centos8-3: the guest additions and repackage the box to continue.
centos8-3:
centos8-3: This is not an error message; everything may continue to work properly,
centos8-3: in which case you may ignore this message.
==> centos8-3: Setting hostname...
==> centos8-3: Configuring and enabling network interfaces...
==> centos8-3: Rsyncing folder: /Users/mvpbang/vagrants/c8-4x/ => /vagrant
mvpbang@mvpbang c8-4x % vagrant status
Current machine states:
centos8-1 running (virtualbox)
centos8-2 running (virtualbox)
centos8-3 running (virtualbox)
mvpbang@mvpbang c8-4x % vagrant ssh centos8-1
[vagrant@centos8-1 ~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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 fq_codel state UP group default qlen 1000
link/ether 52:54:00:fa:65:03 brd ff:ff:ff:ff:ff:ff
altname enp0s3
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic noprefixroute eth0
valid_lft 86038sec preferred_lft 86038sec
inet6 fe80::5054:ff:fefa:6503/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:36:99:09 brd ff:ff:ff:ff:ff:ff
altname enp0s8
inet 172.24.20.41/24 brd 172.24.20.255 scope global noprefixroute eth1
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe36:9909/64 scope link
valid_lft forever preferred_lft forever
[vagrant@centos8-1 ~]$
[vagrant@centos8-1 ~]$ ping qq.com
PING qq.com (111.33.167.71) 56(84) bytes of data.
64 bytes from 111.33.167.71 (111.33.167.71): icmp_seq=1 ttl=63 time=19.3 ms
^C
--- qq.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 19.348/19.348/19.348/0.000 ms
[vagrant@centos8-1 ~]$ free -m
total used free shared buff/cache available
Mem: 964 116 641 12 207 699
Swap: 2047 0 2047
[vagrant@centos8-1 ~]$ ll /vagrant/
total 4
-rw-r--r--. 1 vagrant vagrant 763 Jun 17 14:47 Vagrantfile