Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 36 ยท Multi-machine Environments

Networking Between Machines

Let Vagrant machines talk to each other over a private network, use hostnames, forward ports from the right machine, and wait for services before depending on them.

Defining two machines is the easy part. Making them talk to each other reliably is where multi-machine setups succeed or fail. In this lesson you give each machine a fixed address on a private network, make names resolve, expose only what you need to your host, and handle the "database is not ready yet" problem.

A private network for the cluster

The private_network type creates a host-only network. Your computer and every machine on it can reach each other, but nothing outside your computer can reach them. That is exactly what you want for a development cluster.

Ruby
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"

  config.vm.define "db" do |db|
    db.vm.hostname = "db"
    db.vm.network "private_network", ip: "192.168.56.11"
  end

  config.vm.define "web" do |web|
    web.vm.hostname = "web"
    web.vm.network "private_network", ip: "192.168.56.10"
    web.vm.network "forwarded_port", guest: 80, host: 8080
  end
end

From inside web, ping 192.168.56.11 reaches db. From your laptop, http://localhost:8080 reaches the web server. The database is deliberately not forwarded: only the web machine needs to be visible from the host.

Choose addresses inside one subnet. VirtualBox accepts host-only ranges inside 192.168.56.0/21 by default (newer versions restrict this); use that range unless you edit /etc/vbox/networks.conf.

Making names resolve

IP addresses in config files are fragile. You would rather write db than 192.168.56.11. Vagrant sets each machine's hostname, but it does not add other machines to /etc/hosts by itself. You have three options.

1. Write the hosts file in a shell provisioner. Simple and dependency-free.

Ruby
HOSTS = {
  "web" => "192.168.56.10",
  "db"  => "192.168.56.11"
}

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"

  HOSTS.each do |name, ip|
    config.vm.define name do |node|
      node.vm.hostname = name
      node.vm.network "private_network", ip: ip

      hosts_lines = HOSTS.map { |n, a| "#{a} #{n}" }.join("\n")
      node.vm.provision "shell", inline: <<~SH
        sed -i '/# vagrant-cluster/,$d' /etc/hosts
        printf '# vagrant-cluster\n%s\n' "#{hosts_lines}" >> /etc/hosts
      SH
    end
  end
end

2. Use the vagrant-hostmanager plugin, which maintains hosts files on guests and optionally on your host (see Vagrant plugins).

3. Use a DNS helper such as an mDNS service on the guests. This is more setup and rarely worth it for small clusters.

Checking the wiring

After vagrant up, test connectivity from the machine that depends on the other:

TEXT
$ vagrant ssh web -c "ping -c 1 db"
PING db (192.168.56.11) 56(84) bytes of data.
64 bytes from db (192.168.56.11): icmp_seq=1 ttl=64 time=0.5 ms

$ vagrant ssh web -c "nc -zv db 5432"
Connection to db (192.168.56.11) 5432 port [tcp/postgresql] succeeded!

vagrant ssh machine -c "command" runs one command and returns, which makes it perfect for smoke tests in scripts.

Public and NAT networks

Every VirtualBox machine also has a NAT interface (the first network adapter) that gives it internet access. You do not configure it; it is why apt-get works inside the guest.

A public_network (bridged) attaches the machine to your real LAN so other devices can reach it. Use it sparingly: the address comes from your router, it breaks on a different Wi-Fi, and it exposes the machine to your whole network.

Type Reachable from host Reachable from LAN Typical use
NAT (default) via forwarded ports no internet access for the guest
private_network yes no machine-to-machine traffic
public_network yes yes demos to other devices

Waiting for a service

Vagrant brings machines up one after another, but when web provisions, the database server on db may still be initializing. Make provisioning tolerant by retrying:

Ruby
config.vm.provision "shell", inline: <<~SH
  for i in $(seq 1 30); do
    nc -z db 5432 && exit 0
    sleep 2
  done
  echo "db never came up" >&2
  exit 1
SH

This waits up to a minute and fails clearly if the database never appears. Prefer this to a blind sleep 30: it is faster when the service is quick and safer when it is slow.

A stand-in: planning an address table

The Ruby below imitates the first line of defense against network mistakes: validating that every machine has a unique address in one subnet before you run vagrant up. This is plain Ruby, not Vagrant, but you can paste the same check at the top of a real Vagrantfile.

Ruby Validate the address plan

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

In a real Vagrantfile you would raise instead of printing, so a bad plan stops the run before any VM is touched.

Common mistakes

  • Forwarding the same host port twice. Two machines cannot both claim host: 8080. Vagrant normally detects the collision and picks another port only if you set auto_correct: true.
  • Using localhost between guests. Inside web, localhost is web itself. Use the other machine's name or address.
  • Binding a service to 127.0.0.1 only. If the database listens on loopback, web cannot reach it. Configure it to listen on the private address.
  • Assuming a bridged address is stable. It changes with your network.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which network type lets machines talk to each other and your host but hides them from the rest of your LAN?

    A private (host-only) network is visible to the host and to other guests on it, but not to other devices on your LAN.
  2. 2. From inside the web guest, what does localhost refer to?

    localhost always means the machine you are on. Use the other machine's name or private IP.
  3. 3. Why prefer a retry loop over sleep 30 when waiting for a database?

    A loop adapts to fast and slow starts and gives a clear failure; a fixed sleep is either wasteful or too short.
  4. 4. Does Vagrant automatically add other machines to each guest's /etc/hosts?

    Vagrant sets each machine's own hostname only. Use a shell provisioner or a plugin such as vagrant-hostmanager for cross-machine names.

Summary

  • Give every machine a fixed IP on a private_network; forward ports only for what the host needs.
  • Names between guests need a hosts file written by a provisioner or a plugin.
  • Validate address plans and wait for services with retry loops instead of fixed sleeps.
  • Never use localhost to mean another machine, and bind services to the private address.

Next: Data-driven machine definitions