{"id":29910,"date":"2026-12-06T22:06:15","date_gmt":"2026-12-06T22:06:15","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=29910"},"modified":"2026-12-06T22:06:15","modified_gmt":"2026-12-06T22:06:15","slug":"vagrant-vagrantfile-example-for-creating-3-linux-vms","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/vagrant-vagrantfile-example-for-creating-3-linux-vms\/","title":{"rendered":"Vagrant: vagrantfile example for creating 3 linux vms"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/d92697229c4ce19af0b4aeadd866ec4e.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What the Vagrantfile does, line by line<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The file uses Vagrant&#039;s multi-machine feature. A Ruby range <code>(1..NodeCount)<\/code> loops three times, and each pass calls <code>config.vm.define<\/code> to declare a separately named machine. Settings placed on <code>config<\/code> apply to every machine. Settings placed on <code>node<\/code> apply only to the machine being defined in that pass.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Line<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code>ENV[&#039;VAGRANT_NO_PARALLEL&#039;] = &#039;yes&#039;<\/code><\/td><td>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.<\/td><\/tr><tr><td><code>Vagrant.configure(2)<\/code><\/td><td>Uses version 2 of the configuration format, which is the current one.<\/td><\/tr><tr><td><code>config.vm.provision &quot;shell&quot;, path: &quot;bootstrap.sh&quot;<\/code><\/td><td>Runs <code>bootstrap.sh<\/code> as root on <strong>every<\/strong> VM the first time it is created.<\/td><\/tr><tr><td><code>NodeCount = 3<\/code><\/td><td>The number of VMs. Change it to scale the lab up or down.<\/td><\/tr><tr><td><code>config.vm.define &quot;node#{i}&quot;<\/code><\/td><td>Gives each machine a Vagrant name (node1, node2, node3) that you use with <code>vagrant ssh<\/code>, <code>halt<\/code> and other commands.<\/td><\/tr><tr><td><code>node.vm.box = &quot;bento\/ubuntu-20.04&quot;<\/code><\/td><td>The base image, downloaded from the public box catalog on first use.<\/td><\/tr><tr><td><code>node.vm.hostname<\/code><\/td><td>Sets the guest OS hostname, for example <code>node1.example.com<\/code>.<\/td><\/tr><tr><td><code>node.vm.network &quot;private_network&quot;, ip: ...<\/code><\/td><td>Adds a host-only adapter with a static IP: 172.16.16.101, .102 and .103.<\/td><\/tr><tr><td><code>v.name<\/code>, <code>v.memory<\/code>, <code>v.cpus<\/code><\/td><td>Sets the VM name shown in VirtualBox Manager, plus 2048 MB of RAM and 2 vCPUs per VM.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Three VMs at 2 GB and 2 vCPUs each use 6 GB of RAM and 6 vCPUs on the host. Lower <code>v.memory<\/code> to 1024 if your laptop has 8 GB or less.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>VirtualBox installed, with hardware virtualization (VT-x\/AMD-V) turned on in the BIOS\/UEFI.<\/li>\n\n\n<li>A recent Vagrant release that supports your VirtualBox version. Older Vagrant releases often fail to detect newer major versions of VirtualBox.<\/li>\n\n\n<li>The bento boxes used here are x86_64. On Apple Silicon Macs, use an arm64 box and a provider that supports it.<\/li>\n\n\n<li>An empty project directory that holds the <code>Vagrantfile<\/code> and <code>bootstrap.sh<\/code>.<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">vagrant --version\nVBoxManage --version\nmkdir vagrant-3-nodes &amp;&amp; cd vagrant-3-nodes<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\">The missing bootstrap.sh script<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The example above points to <code>bootstrap.sh<\/code> but does not include it. If the file is missing, <code>vagrant up<\/code> stops with an error saying the shell provisioner&#039;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 <code>grep<\/code> check stops it from adding duplicate lines when you run provisioning again.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/usr\/bin\/env bash<\/span>\nset -euo pipefail\n\n<span class=\"hljs-comment\"># Name resolution between the nodes<\/span>\n<span class=\"hljs-keyword\">if<\/span> ! grep -q <span class=\"hljs-string\">\"node1.example.com\"<\/span> \/etc\/hosts; then\n  cat &gt;&gt; \/etc\/hosts &lt;&lt;EOF\n<span class=\"hljs-number\">172.16<\/span><span class=\"hljs-number\">.16<\/span><span class=\"hljs-number\">.101<\/span> node1.example.com node1\n<span class=\"hljs-number\">172.16<\/span><span class=\"hljs-number\">.16<\/span><span class=\"hljs-number\">.102<\/span> node2.example.com node2\n<span class=\"hljs-number\">172.16<\/span><span class=\"hljs-number\">.16<\/span><span class=\"hljs-number\">.103<\/span> node3.example.com node3\nEOF\nfi\n\n<span class=\"hljs-comment\"># Basic packages<\/span>\nexport DEBIAN_FRONTEND=noninteractive\napt-get update -y\napt-get install -y vim curl net-tools\n\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Bootstrap finished on $(hostname)\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Bringing the VMs up and managing them<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Run <code>vagrant up<\/code> in the project directory. The first run downloads the box, then creates node1, node2 and node3 in order.<\/li>\n\n\n<li>Run <code>vagrant status<\/code> to confirm all three machines are running.<\/li>\n\n\n<li>Open a shell on one node with <code>vagrant ssh node1<\/code>.<\/li>\n\n\n<li>From inside node1, test the private network with <code>ping -c 2 node2<\/code> and <code>ping -c 2 172.16.16.103<\/code>.<\/li>\n\n\n<li>Shut everything down with <code>vagrant halt<\/code>, or delete the VMs with <code>vagrant destroy -f<\/code>.<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">$ vagrant status\nCurrent machine states:\n\nnode1                     running (virtualbox)\nnode2                     running (virtualbox)\nnode3                     running (virtualbox)\n\nThis environment represents multiple VMs. The VMs are all listed\nabove <span class=\"hljs-keyword\">with<\/span> their current state. For more information about a specific\nVM, run <span class=\"hljs-string\">`vagrant status NAME`<\/span>.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Most commands take a machine name, so you can act on one node at a time:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">vagrant up node2                  <span class=\"hljs-comment\"># start only node2<\/span>\nvagrant halt node3                <span class=\"hljs-comment\"># stop only node3<\/span>\nvagrant provision node1           <span class=\"hljs-comment\"># re-run bootstrap.sh on node1<\/span>\nvagrant reload node1 --provision  <span class=\"hljs-comment\"># restart and re-provision<\/span>\nvagrant ssh-config node1          <span class=\"hljs-comment\"># SSH details for PuTTY or other clients<\/span>\nvagrant destroy -f node3          <span class=\"hljs-comment\"># delete only node3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Provisioning runs automatically only the first time a machine is created. After you edit <code>bootstrap.sh<\/code>, apply the change with <code>vagrant provision<\/code> or <code>vagrant reload --provision<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Updated Vagrantfile<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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 <code>172.16.16.1010<\/code> 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.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># -*- mode: ruby -*-<\/span>\n<span class=\"hljs-comment\"># vi: set ft=ruby :<\/span>\n\nENV&#91;<span class=\"hljs-string\">'VAGRANT_NO_PARALLEL'<\/span>] = <span class=\"hljs-string\">'yes'<\/span>\n\nNODE_COUNT = <span class=\"hljs-number\">3<\/span>\nBOX_IMAGE  = <span class=\"hljs-string\">\"bento\/ubuntu-22.04\"<\/span>\nIP_PREFIX  = <span class=\"hljs-string\">\"172.16.16.\"<\/span>\n\nVagrant.configure(<span class=\"hljs-string\">\"2\"<\/span>) <span class=\"hljs-keyword\">do<\/span> |config|\n  config.vm.provision <span class=\"hljs-string\">\"shell\"<\/span>, path: <span class=\"hljs-string\">\"bootstrap.sh\"<\/span>\n\n  (<span class=\"hljs-number\">1.<\/span>.NODE_COUNT).each <span class=\"hljs-keyword\">do<\/span> |i|\n    config.vm.define <span class=\"hljs-string\">\"node#{i}\"<\/span> <span class=\"hljs-keyword\">do<\/span> |node|\n      node.vm.box      = BOX_IMAGE\n      node.vm.hostname = <span class=\"hljs-string\">\"node#{i}.example.com\"<\/span>\n      node.vm.network <span class=\"hljs-string\">\"private_network\"<\/span>, ip: <span class=\"hljs-string\">\"#{IP_PREFIX}#{100 + i}\"<\/span>\n\n      node.vm.provider <span class=\"hljs-string\">\"virtualbox\"<\/span> <span class=\"hljs-keyword\">do<\/span> |v|\n        v.name   = <span class=\"hljs-string\">\"node#{i}\"<\/span>\n        v.memory = <span class=\"hljs-number\">2048<\/span>\n        v.cpus   = <span class=\"hljs-number\">2<\/span>\n      end\n    end\n  end\nend<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">If you increase <code>NODE_COUNT<\/code>, add the matching lines to the <code>\/etc\/hosts<\/code> block in <code>bootstrap.sh<\/code>. Before you bring up a new layout, run <code>vagrant validate<\/code> to catch Ruby syntax errors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Allowing the 172.16.16.x network in VirtualBox<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Since VirtualBox 6.1.28, host-only networks on Linux and macOS hosts are limited to <code>192.168.56.0\/21<\/code> 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 <code>IP_PREFIX<\/code> to something like <code>192.168.56.<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">sudo mkdir -p \/etc\/vbox\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"* 172.16.0.0\/12 192.168.56.0\/21\"<\/span> | sudo tee \/etc\/vbox\/networks.conf<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error or symptom<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td>IP address not within the allowed ranges<\/td><td>VirtualBox host-only network restriction on Linux\/macOS<\/td><td>Add the range to <code>\/etc\/vbox\/networks.conf<\/code> or use 192.168.56.x<\/td><\/tr><tr><td>Shell provisioner path does not exist<\/td><td><code>bootstrap.sh<\/code> is missing from the project directory<\/td><td>Create the script shown above next to the Vagrantfile<\/td><\/tr><tr><td>VirtualBox name already taken<\/td><td>Another VM, possibly left over from an earlier run, is already called node1<\/td><td>Delete it in VirtualBox Manager or change <code>v.name<\/code><\/td><\/tr><tr><td>VT-x is not available \/ VM hangs at boot<\/td><td>Virtualization is off in the BIOS, or Hyper-V is holding it on Windows<\/td><td>Turn on VT-x\/AMD-V. On Windows, check Hyper-V, WSL2 and Virtual Machine Platform settings<\/td><\/tr><tr><td>Provider version not supported<\/td><td>Vagrant is older than your VirtualBox release<\/td><td>Upgrade Vagrant<\/td><\/tr><tr><td>Host runs out of memory<\/td><td>6 GB of RAM in total is requested<\/td><td>Reduce <code>v.memory<\/code> or <code>NODE_COUNT<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Commit <code>Vagrantfile<\/code> and <code>bootstrap.sh<\/code> to Git. Add <code>.vagrant\/<\/code> to <code>.gitignore<\/code>, because it holds machine IDs and private keys.<\/li>\n\n\n<li>Keep provisioning scripts idempotent so <code>vagrant provision<\/code> can run safely more than once.<\/li>\n\n\n<li>Pin box versions with <code>node.vm.box_version<\/code> when the whole team needs identical environments.<\/li>\n\n\n<li>Use these VMs only as local labs. They use the well-known <code>vagrant<\/code> user and an insecure default key pair, so never expose them to a public network.<\/li>\n\n\n<li>Run <code>vagrant destroy -f<\/code> when you finish to free disk space and RAM.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">How do I SSH into a specific VM in a multi-machine Vagrant setup?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Run <code>vagrant ssh &lt;name&gt;<\/code>, for example <code>vagrant ssh node2<\/code>. For external clients such as PuTTY, <code>vagrant ssh-config node2<\/code> prints the host, port, user and key path.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does vagrant up fail with an IP address not in the allowed ranges error?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I create more than three VMs with this Vagrantfile?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does the provisioning script run every time I start the VMs?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. It runs only when a machine is first created. Use <code>vagrant provision<\/code> or <code>vagrant up --provision<\/code> to run it again.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can the three VMs talk to each other?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/infrastructure-as-code\/\">More Infrastructure as Code tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/operating-systems\/linux\/\">More Linux tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/how-to-login-to-linux-server-using-putty\/\">How to Login to Linux Server using Putty?<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[26145,4957],"tags":[980],"class_list":["post-29910","post","type-post","status-publish","format-standard","hentry","category-infrastructure-as-code","category-linux","tag-vagrant"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/29910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=29910"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/29910\/revisions"}],"predecessor-version":[{"id":80723,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/29910\/revisions\/80723"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=29910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=29910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=29910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}