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.
| Line | Purpose |
|---|---|
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 = 3 | The 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.hostname | Sets 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.cpus | Sets 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
Vagrantfileandbootstrap.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
- Run
vagrant upin the project directory. The first run downloads the box, then creates node1, node2 and node3 in order. - Run
vagrant statusto confirm all three machines are running. - Open a shell on one node with
vagrant ssh node1. - From inside node1, test the private network with
ping -c 2 node2andping -c 2 172.16.16.103. - Shut everything down with
vagrant halt, or delete the VMs withvagrant 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 symptom | Cause | Fix |
|---|---|---|
| IP address not within the allowed ranges | VirtualBox host-only network restriction on Linux/macOS | Add the range to /etc/vbox/networks.conf or use 192.168.56.x |
| Shell provisioner path does not exist | bootstrap.sh is missing from the project directory | Create the script shown above next to the Vagrantfile |
| VirtualBox name already taken | Another VM, possibly left over from an earlier run, is already called node1 | Delete it in VirtualBox Manager or change v.name |
| VT-x is not available / VM hangs at boot | Virtualization is off in the BIOS, or Hyper-V is holding it on Windows | Turn on VT-x/AMD-V. On Windows, check Hyper-V, WSL2 and Virtual Machine Platform settings |
| Provider version not supported | Vagrant is older than your VirtualBox release | Upgrade Vagrant |
| Host runs out of memory | 6 GB of RAM in total is requested | Reduce v.memory or NODE_COUNT |
Best practices
- Commit
Vagrantfileandbootstrap.shto Git. Add.vagrant/to.gitignore, because it holds machine IDs and private keys. - Keep provisioning scripts idempotent so
vagrant provisioncan run safely more than once. - Pin box versions with
node.vm.box_versionwhen the whole team needs identical environments. - Use these VMs only as local labs. They use the well-known
vagrantuser and an insecure default key pair, so never expose them to a public network. - Run
vagrant destroy -fwhen 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
- More Infrastructure as Code tutorials
- More Linux tutorials
- How to Login to Linux Server using Putty?
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 worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals