How does Vagrant create a private network?
Back in Vagrant 1.0.x it is called Host-only Networking, it is a feature of VirtualBox, which allows multiple virtual machines to communicate with each other through a network via the host machine. The network created by host-only networking is private to the VMs involved and the host machine. The outside world cannot join this network.
Behind the scene, VirtualBox creates a new virtual interface ("loopback") on the host which appears next to the existing network interfaces.
VirtualBox even provide a built-in DHCP server for host-only networking (Private Networking) if no static IPs have been assigned. It can be configured in File - Preferences - Network.
See more at =>
- Host-only networking
- Networking in VirtualBox
On the host side, Vagrant does nothing. As far as I know Vagrant never touches host network configuration.
On the guest side, the current provider implements the network configuration logic. Here is what the VirtualBox provider does:
https://github.com/mitchellh/vagrant/blob/master/plugins/providers/virtualbox/action/network.rb
Basically the implementation is composed of two sequential steps:
- Enable all the needed network adapters on the virtual machine, using hypervisor commands;
- Configure the IP address on the guest OS, using guest capabilities, in this case the
configure_networks
capability.
As an example, here is the implementation for the configure_networks
capability on Debian-based Linux OS.