Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 36 ยท Networking and Synced Folders

Private and Public Networks

Give Vagrant VMs their own IP addresses: private host-only networks with static or DHCP addresses, public bridged networks, adapters, hostnames and troubleshooting.

Forwarded ports are fine for one web server, but real projects have several machines that must talk to each other, services on standard ports, and names like app.test. For this Vagrant offers three network types: the always-present NAT with forwarded ports, a private network (the VM gets an IP address that only your computer and other VMs can reach), and a public network (the VM joins your real LAN like another computer).

How the adapters fit together

Every machine has a first network adapter that uses NAT. That adapter is how Vagrant connects over SSH and how the VM reaches the Internet. When you add a private or public network, Vagrant adds a second adapter.

flowchart TB
    subgraph HOST["Your computer (host)"]
        H["Host: 192.168.56.1 (host-only)"]
    end
    subgraph VM1["VM web"]
        N1["eth0: NAT (10.0.2.15) -> Internet and SSH"]
        P1["eth1: 192.168.56.10"]
    end
    subgraph VM2["VM db"]
        N2["eth0: NAT"]
        P2["eth1: 192.168.56.11"]
    end
    H --- P1
    H --- P2
    P1 --- P2

The NAT adapter is always there; the private network is an extra interface shared by host and VMs.

Private networks

A private network (called a host-only network in VirtualBox) creates an isolated virtual network between your computer and the VMs. Nothing outside your computer can reach it, so it is safe by default.

Static IP:

Ruby
Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-24.04"
  config.vm.network "private_network", ip: "192.168.56.10"
end

Now http://192.168.56.10 works from the host with no port forwarding and on standard ports, and other VMs on the same network reach 192.168.56.10 as well. You can add the name to your hosts file (192.168.56.10 shop.test) to use a hostname, or let a plugin do it, see the Providers and Plugins chapter.

DHCP: config.vm.network "private_network", type: "dhcp" picks an address automatically. You then discover it with vagrant ssh -c "ip -4 addr show eth1". A fixed address is better for documentation and for multi-machine setups.

Choosing the address range

Use a private range that does not collide with your real network. Recent VirtualBox versions (6.1.28 and later, on Linux and macOS) only allow host-only networks inside 192.168.56.0/21 unless you list others in /etc/vbox/networks.conf:

TEXT
* 192.168.56.0/21
* 10.20.30.0/24

Choosing an address outside the allowed ranges fails with an error similar to "The IP address configured for the host-only network is not within the allowed ranges". The same file allows IPv6 ranges. Avoid addresses that coincide with your office or home network (192.168.0.x or 192.168.1.x are very common) because routing becomes ambiguous.

Other options are netmask:, auto_config: false (do not let Vagrant configure the interface inside the guest, you do it yourself), virtualbox__intnet: true (an internal network without access from the host, for VM-to-VM traffic only) and ip: "fde4:..." with type: "static6"-style IPv6 settings.

Public networks (bridged)

A public network bridges the VM to a physical interface of your computer. The VM appears as another device on your LAN, with an address from your router, and colleagues can reach it.

Ruby
config.vm.network "public_network"
config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)"
config.vm.network "public_network", ip: "192.168.1.200"

If you have several network cards, Vagrant asks which to bridge, unless you pass bridge: with a name (or a list of names to try in order). Use this when:

  • you want another physical device (a phone, a Raspberry Pi) to test against the VM,
  • the VM needs to be a full citizen of the LAN (a DHCP or DNS lab),
  • a colleague must reach your VM.

The downsides: the address can change, corporate or hotel networks may block extra MAC addresses, and you expose the VM to everyone on that network. Treat a bridged VM with the default vagrant password and the insecure SSH key as an open door; change the credentials, or use a private network instead. See also Forwarded ports for the host_ip warning.

Comparing the three

NAT + forwarded ports Private network Public network
Reachable from host Through ports Directly by IP Directly by IP
Reachable from other computers Only if host_ip allows No Yes
VM-to-VM Awkward Yes Yes
Needs router/DHCP No No Yes
Works behind strict networks Yes Yes Often not
Security Medium Best Weakest

Name resolution

The guest's hostname (config.vm.hostname = "web") only names the machine. To reach it by name from the host you need an entry in the host's hosts file (/etc/hosts, or C:\Windows\System32\drivers\etc\hosts) or a plugin that edits it. Between VMs of a multi-machine setup, a shell provisioner can write each other's name into /etc/hosts in the guest.

Ruby
config.vm.hostname = "shop"
config.vm.network "private_network", ip: "192.168.56.10"
config.vm.provision "shell", inline: "echo '192.168.56.11 db' >> /etc/hosts"

(Remember idempotence: that line adds a duplicate each time you provision. Guard it with grep -q.)

Validating an address plan

The stand-in below uses Ruby's IPAddr to check a network plan: that every address belongs to the allowed host-only range, that no two machines share an address, and that none collides with the host-side gateway. It is plain Ruby, a helper you could even call from a real Vagrantfile.

Ruby Check a private network plan (Ruby)

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.

Troubleshooting networks

  • Cannot reach the private IP from the host: the host-only adapter may be down or its address wrong. Check ip addr in the guest and the VirtualBox Host-Only Network list. After VirtualBox upgrades, a vagrant reload often repairs it.
  • Vagrant hangs configuring a network on a Linux guest: some boxes use Netplan or NetworkManager; modern Vagrant handles them, but very old Vagrant versions may not. Update Vagrant.
  • VPN breaks the VM's Internet access: tools like corporate VPNs change routing and DNS. Try vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"].
  • Two machines have the same IP: the second silently fails. Keep an address table.
  • Windows with Hyper-V: private networks use the Hyper-V "Default Switch" and addresses are assigned by the host; static IPs are not supported by that provider the same way.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which adapter is always present and carries the SSH connection?

    Adapter 1 is NAT; private and public networks are extra adapters on top.
  2. 2. What is the key difference between a private and a public network?

    Private (host-only) is isolated; public (bridged) exposes the VM on your real network.
  3. 3. VirtualBox complains that your private IP is not within the allowed ranges. What do you do?

    VirtualBox restricts host-only ranges; the networks.conf file whitelists additional ones.
  4. 4. Why can a bridged VM be a security risk?

    A bridged VM is a full network citizen; default passwords and keys make it an easy target.

Summary

  • Every VM has a NAT adapter; private networks add an isolated host-only network, public networks bridge to your LAN.
  • Use static IPs in 192.168.56.0/21 (or a range allowed in /etc/vbox/networks.conf) for predictable multi-machine setups.
  • Public networks expose the VM; prefer private networks for development.
  • Hostnames inside the guest do not resolve on the host without a hosts-file entry or plugin.
  • Keep an address table, and validate it.

Next: Synced Folders