Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Vagrant create a private network?

What is Vagrant doing behind the scenes to the host and guest machine when it sets up a private network with a fixed IP (http://docs.vagrantup.com/v2/networking/private_network.html)?

Vagrant.configure("2") do |config|
  config.vm.network "private_network", ip: "192.168.50.4"
end
like image 766
Matt Harrison Avatar asked Sep 22 '13 19:09

Matt Harrison


2 Answers

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
like image 61
Terry Wang Avatar answered Oct 31 '22 02:10

Terry Wang


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:

  1. Enable all the needed network adapters on the virtual machine, using hypervisor commands;
  2. 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.

like image 43
Emyl Avatar answered Oct 31 '22 01:10

Emyl