โ˜• XOPSSCHOOL โ€” LEARNING MADE AFFORDABLE
Build the Skills Your Next Career Move Demands.
For the Price of One Coffee a Month.
Why spend hundreds on individual courses? Unlock an entire year of technical learning with one Coffee Pass.
US$4.90 /month
US$58.80/year
โ‚น499 + GST/month
โ‚น5,988 + GST/year
โ‚ฌ4.90 /month
โ‚ฌ58.80/year
12 Months of Access • Billed Annually • No Automatic Renewal
DevOps • Cloud • SRE • Kubernetes • Terraform • DevSecOps • AI • AIOps • MLOps • LLMOps • FinOps • GitOps • Platform Engineering & Much More
192
Premium Courses
1,085
Video Sessions
99
Ops Disciplines
86
Tool Courses
340
Assignments
227
Quizzes
โœ“ QR-Verifiable Certificates   โœ“ Learn at Your Own Pace   โœ“ Hands-on Learning   โœ“ New Courses Included
Learn from real-world, instructor-led video sessions by
Rajesh Kumar
Principal Architect • Trainer • Mentor • 15+ Years of Industry Experience
โ˜• Get My Coffee Pass โ€” Start Learning Today →
One Payment. Every Premium Course. One Full Year.
The best investment you can make is in the person you are becoming.

Vagrant: vagrantfile example for creating 3 linux vms

The Vagrantfile above uses a Ruby loop to create three Ubuntu VMs on VirtualBox (node1, node2 and node3). Each VM gets its own hostname, a static private IP, 2 GB of RAM and 2 vCPUs. This guide explains each line, supplies the missing bootstrap.sh script, shows the commands to run and manage the cluster, and gives an updated version that works with current VirtualBox network restrictions.


What the Vagrantfile does, line by line

The file uses Vagrant's multi-machine feature. A Ruby range (1..NodeCount) loops three times, and each pass calls config.vm.define to declare a separately named machine. Settings placed on config apply to every machine. Settings placed on node apply only to the machine being defined in that pass.

LinePurpose
ENV['VAGRANT_NO_PARALLEL'] = 'yes'Brings machines up one at a time. The VirtualBox provider already works this way, so the line mainly matters for providers that support parallel creation.
Vagrant.configure(2)Uses version 2 of the configuration format, which is the current one.
config.vm.provision "shell", path: "bootstrap.sh"Runs bootstrap.sh as root on every VM the first time it is created.
NodeCount = 3The number of VMs. Change it to scale the lab up or down.
config.vm.define "node#{i}"Gives each machine a Vagrant name (node1, node2, node3) that you use with vagrant ssh, halt and other commands.
node.vm.box = "bento/ubuntu-20.04"The base image, downloaded from the public box catalog on first use.
node.vm.hostnameSets the guest OS hostname, for example node1.example.com.
node.vm.network "private_network", ip: ...Adds a host-only adapter with a static IP: 172.16.16.101, .102 and .103.
v.name, v.memory, v.cpusSets the VM name shown in VirtualBox Manager, plus 2048 MB of RAM and 2 vCPUs per VM.

Note: Three VMs at 2 GB and 2 vCPUs each use 6 GB of RAM and 6 vCPUs on the host. Lower v.memory to 1024 if your laptop has 8 GB or less.

Prerequisites

  • VirtualBox installed, with hardware virtualization (VT-x/AMD-V) turned on in the BIOS/UEFI.
  • A recent Vagrant release that supports your VirtualBox version. Older Vagrant releases often fail to detect newer major versions of VirtualBox.
  • The bento boxes used here are x86_64. On Apple Silicon Macs, use an arm64 box and a provider that supports it.
  • An empty project directory that holds the Vagrantfile and bootstrap.sh.
vagrant --version
VBoxManage --version
mkdir vagrant-3-nodes && cd vagrant-3-nodes

The missing bootstrap.sh script

The example above points to bootstrap.sh but does not include it. If the file is missing, vagrant up stops with an error saying the shell provisioner's path does not exist. Save the script below next to the Vagrantfile. It adds host entries so the nodes can find each other by name, then installs a few basic tools. The grep check stops it from adding duplicate lines when you run provisioning again.

#!/usr/bin/env bash
set -euo pipefail

# Name resolution between the nodes
if ! grep -q "node1.example.com" /etc/hosts; then
  cat >> /etc/hosts <<EOF
172.16.16.101 node1.example.com node1
172.16.16.102 node2.example.com node2
172.16.16.103 node3.example.com node3
EOF
fi

# Basic packages
export DEBIAN_FRONTEND=noninteractive
apt-get update -y
apt-get install -y vim curl net-tools

echo "Bootstrap finished on $(hostname)"Code language: PHP (php)

Bringing the VMs up and managing them

  1. Run vagrant up in the project directory. The first run downloads the box, then creates node1, node2 and node3 in order.
  2. Run vagrant status to confirm all three machines are running.
  3. Open a shell on one node with vagrant ssh node1.
  4. From inside node1, test the private network with ping -c 2 node2 and ping -c 2 172.16.16.103.
  5. Shut everything down with vagrant halt, or delete the VMs with vagrant destroy -f.
$ vagrant status
Current machine states:

node1                     running (virtualbox)
node2                     running (virtualbox)
node3                     running (virtualbox)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.Code language: JavaScript (javascript)

Most commands take a machine name, so you can act on one node at a time:

vagrant up node2                  # start only node2
vagrant halt node3                # stop only node3
vagrant provision node1           # re-run bootstrap.sh on node1
vagrant reload node1 --provision  # restart and re-provision
vagrant ssh-config node1          # SSH details for PuTTY or other clients
vagrant destroy -f node3          # delete only node3Code language: PHP (php)

Note: Provisioning runs automatically only the first time a machine is created. After you edit bootstrap.sh, apply the change with vagrant provision or vagrant reload --provision.

Updated Vagrantfile

The version below keeps the same layout and fixes four problems. It moves to a supported Ubuntu LTS box, because standard support for Ubuntu 20.04 has ended. It builds IP addresses with arithmetic rather than string concatenation; the original produces an invalid address such as 172.16.16.1010 once the count reaches 10. It moves the node count into a top-level setting. It also defines the box name and IP prefix once at the top so they are easy to change.

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

ENV['VAGRANT_NO_PARALLEL'] = 'yes'

NODE_COUNT = 3
BOX_IMAGE  = "bento/ubuntu-22.04"
IP_PREFIX  = "172.16.16."

Vagrant.configure("2") do |config|
  config.vm.provision "shell", path: "bootstrap.sh"

  (1..NODE_COUNT).each do |i|
    config.vm.define "node#{i}" do |node|
      node.vm.box      = BOX_IMAGE
      node.vm.hostname = "node#{i}.example.com"
      node.vm.network "private_network", ip: "#{IP_PREFIX}#{100 + i}"

      node.vm.provider "virtualbox" do |v|
        v.name   = "node#{i}"
        v.memory = 2048
        v.cpus   = 2
      end
    end
  end
endCode language: PHP (php)

If you increase NODE_COUNT, add the matching lines to the /etc/hosts block in bootstrap.sh. Before you bring up a new layout, run vagrant validate to catch Ruby syntax errors.

Allowing the 172.16.16.x network in VirtualBox

Since VirtualBox 6.1.28, host-only networks on Linux and macOS hosts are limited to 192.168.56.0/21 by default. With the default limit, the 172.16.16.x addresses in the example above fail with an error saying the IP address is not within the allowed ranges. Windows hosts do not have this restriction. To fix it, either allow the range in VirtualBox or change IP_PREFIX to something like 192.168.56..

sudo mkdir -p /etc/vbox
echo "* 172.16.0.0/12 192.168.56.0/21" | sudo tee /etc/vbox/networks.confCode language: PHP (php)

Common errors and fixes

Error or symptomCauseFix
IP address not within the allowed rangesVirtualBox host-only network restriction on Linux/macOSAdd the range to /etc/vbox/networks.conf or use 192.168.56.x
Shell provisioner path does not existbootstrap.sh is missing from the project directoryCreate the script shown above next to the Vagrantfile
VirtualBox name already takenAnother VM, possibly left over from an earlier run, is already called node1Delete it in VirtualBox Manager or change v.name
VT-x is not available / VM hangs at bootVirtualization is off in the BIOS, or Hyper-V is holding it on WindowsTurn on VT-x/AMD-V. On Windows, check Hyper-V, WSL2 and Virtual Machine Platform settings
Provider version not supportedVagrant is older than your VirtualBox releaseUpgrade Vagrant
Host runs out of memory6 GB of RAM in total is requestedReduce v.memory or NODE_COUNT

Best practices

  • Commit Vagrantfile and bootstrap.sh to Git. Add .vagrant/ to .gitignore, because it holds machine IDs and private keys.
  • Keep provisioning scripts idempotent so vagrant provision can run safely more than once.
  • Pin box versions with node.vm.box_version when the whole team needs identical environments.
  • Use these VMs only as local labs. They use the well-known vagrant user and an insecure default key pair, so never expose them to a public network.
  • Run vagrant destroy -f when you finish to free disk space and RAM.

Frequently asked questions

How do I SSH into a specific VM in a multi-machine Vagrant setup?

Run vagrant ssh <name>, for example vagrant ssh node2. For external clients such as PuTTY, vagrant ssh-config node2 prints the host, port, user and key path.

Why does vagrant up fail with an IP address not in the allowed ranges error?

Recent VirtualBox versions on Linux and macOS allow host-only IPs only in 192.168.56.0/21 by default. Add your range to /etc/vbox/networks.conf or switch to 192.168.56.x addresses.

How do I create more than three VMs with this Vagrantfile?

Change the node count variable and make sure each IP is built arithmetically, as in the updated version above, so addresses stay valid beyond nine nodes. Also add the new hosts to bootstrap.sh.

Does the provisioning script run every time I start the VMs?

No. It runs only when a machine is first created. Use vagrant provision or vagrant up --provision to run it again.

Can the three VMs talk to each other?

Yes. All three sit on the same private host-only network, so they can reach each other by IP, and by hostname once bootstrap.sh has added the /etc/hosts entries.

Related tutorials

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals

Related Posts

Vagrant Tutorials: command line reference

This page lists every top-level vagrant command and global option, as printed by vagrant –help. Below the reference you'll find how to use those commands day to…

Read More

Vagrant Tutorial: Basic Workflow

The basic Vagrant workflow has four parts: install a provider such as VirtualBox, install Vagrant, get a box (a base OS image), and run vagrant init, vagrant…

Read More

Virtualization in DevOps: Using Vagrant, Docker, and Cloud Platforms Together

This guide explains how VM-based workflows, containers, and cloud platforms complement each other in DevOps. It includes a practical workflow, trade-off table, and command examples for day-to-day use.

Read More

Vagrant Assignment & Excercise

Submit a assignment at https://www.debug.school/ and share your url in the comment section Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert…

Read More

Chef Tutorials: Test Kitchen – Example of kitchen.yml using vagrant

Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have…

Read More

Chef Tutorials: Test Kitchen – Example of kitchen.yml using vagrant on ubuntu

Example of .kitchen.yml for driver_plugin vagrant with ubuntu for Chef Test Kitchen Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x