» Public Networks

Network identifier: public_network

Vagrant public networks are less private than private networks, and the exactmeaning actually varies from provider to provider,hence the ambiguous definition. The idea is that whileprivate networks should never allow thegeneral public access to your machine, public networks can.

Confused? We kind of are, too. It is likely thatpublic networks will be replaced by :bridged in afuture release, since that is in general what should be done withpublic networks, and providers that do not support bridging generallydo not have any other features that map to public networks either.

Warning! Vagrant boxes are insecure by defaultand by design, featuring public passwords, insecure keypairsfor SSH access, and potentially allow root access over SSH. Withthese known credentials, your box is easily accessible by anyone onyour network. Before configuring Vagrant to use a public network,consider all potential security implicationsand review the default boxconfiguration to identify potential security risks.

» DHCP

The easiest way to use a public network is to allow the IP to be assignedvia DHCP. In this case, defining a public network is trivially easy:

  1. Vagrant.configure("2") do |config|
  2. config.vm.network "public_network"
  3. end

When DHCP is used, the IP can be determined by using vagrant ssh toSSH into the machine and using the appropriate command line tool to findthe IP, such as ifconfig.

» Using the DHCP Assigned Default Route

Some cases require the DHCP assigned default route to be untouched. In these cases onemay specify the use_dhcp_assigned_default_route option. As an example:

  1. Vagrant.configure("2") do |config|
  2. config.vm.network "public_network",
  3. use_dhcp_assigned_default_route: true
  4. end

» Static IP

Depending on your setup, you may wish to manually set the IP of yourbridged interface. To do so, add a :ip clause to the network definition.

  1. config.vm.network "public_network", ip: "192.168.0.17"

» Default Network Interface

If more than one network interface is available on the host machine, Vagrant willask you to choose which interface the virtual machine should bridge to. A defaultinterface can be specified by adding a :bridge clause to the network definition.

  1. config.vm.network "public_network", bridge: "en1: Wi-Fi (AirPort)"

The string identifying the desired interface must exactly match the name of anavailable interface. If it cannot be found, Vagrant will ask you to pickfrom a list of available network interfaces.

With some providers, it is possible to specify a list of adapters to bridgeagainst:

  1. config.vm.network "public_network", bridge: [
  2. "en1: Wi-Fi (AirPort)",
  3. "en6: Broadcom NetXtreme Gigabit Ethernet Controller",
  4. ]

In this example, the first network adapter that exists and can successfully bebridge will be used.

» Disable Auto-Configuration

If you want to manually configure the network interface yourself, youcan disable auto-configuration by specifying auto_config:

  1. Vagrant.configure("2") do |config|
  2. config.vm.network "public_network", auto_config: false
  3. end

Then the shell provisioner can be used to configure the ip of the interface:

  1. Vagrant.configure("2") do |config|
  2. config.vm.network "public_network", auto_config: false
  3. # manual ip
  4. config.vm.provision "shell",
  5. run: "always",
  6. inline: "ifconfig eth1 192.168.0.17 netmask 255.255.255.0 up"
  7. # manual ipv6
  8. config.vm.provision "shell",
  9. run: "always",
  10. inline: "ifconfig eth1 inet6 add fc00::17/7"
  11. end

» Default Router

Depending on your setup, you may wish to manually override the defaultrouter configuration. This is required if you need access the Vagrant box fromother networks over the public network. To do so, you can use a shellprovisioner script:

  1. Vagrant.configure("2") do |config|
  2. config.vm.network "public_network", ip: "192.168.0.17"
  3. # default router
  4. config.vm.provision "shell",
  5. run: "always",
  6. inline: "route add default gw 192.168.0.1"
  7. # default router ipv6
  8. config.vm.provision "shell",
  9. run: "always",
  10. inline: "route -A inet6 add default gw fc00::1 eth1"
  11. # delete default gw on eth0
  12. config.vm.provision "shell",
  13. run: "always",
  14. inline: "eval `route -n | awk '{ if ($8 ==\"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`"
  15. end

Note the above is fairly complex and may be guest OS specific, but wedocument the rough idea of how to do it because it is a common question.