Your use case for Vagrant will determine what sort of networking setup is appropriate. The good news is that there are plenty of options. Vagrant defines three high-level options for networking that are then implemented in a specific way by the provider. The three options – which are a generic description of a type of access – are:
- Port Forwarding
- Private Network
- Public Network
All networking configuration is specified with the method
config.vm.network.The identifiers for each type of network are
:forwarded_port, :private_network, and :public_network. Following this is a set of options which change based on the provider and the type of network being specified. An example Vagrantfile line for a port forward to port 80 which the host can access on 8080 would be as follows:
config.vm.network "forwarded_port", guest: 80, host: 8080
Multiple networks can be configured simply by adding multiple lines of this same sort of specification. After updating networking in a Vagrantfile, a
vagrant reload will make the settings take effect.
Vagrant Networking Options
Port forwarding may be the simplest option if only local development is taking place and other local environments don’t need to interact with the one in question. It’s probably less confusing to use a private network in a multi-machine configuration, but in a single machine environment, port forwarding is a simple way to access the guest machine. Do take care to specify whether the forward needs to be TCP or UDP. The forward uses TCP by default, so it will need to be changed if UDP is required. Also, if the port forward needs TCP AND UDP both, you must specify two separate port forwards – one for each. The full list of configuration options for a port forward (as taken directly from the documentation) is as follows:
guest(int) – The port on the guest that you want to be exposed on the host. This can be any port.
guest_ip(string) – The guest IP to bind the forwarded port to. If this is not set, the port will go to the every interface. By default, this is empty.
host(int) – The port on the host that you want to use to access the port on the guest. This must be greater than port 1024 unless Vagrant is running as root (which is not recommended).
host_ip(string) – The IP on the host you want to bind the forwarded port to. If not specified, it will be bound to every IP. By default, this is empty.
protocol(string) – Either “udp” or “tcp”. This specifies the protocol that will be allowed through the forwarded port. By default this is “tcp”.
Vagrant is intelligent enough to not let you spin up two machines with port forwards using the same host port (8080, for example). By default, this will simply not be allowed. But you can also specify
:auto_correct to have Vagrant resolve the conflict on its own.
A private network configuration will allow the machine (or multiple machines) to be assigned an IP address in private address space accessible only from the host machine. How this is accomplished depends on the provider; for instance, VMware Fusion would create vmnet? and install a virtual adapter on the host machine connected to this network. This may be less confusing to use in a multi-machine deployment. It does, however, require that at setup time the user has permissions on the host machine to install a virtual network adapter. The private networking mechanism of the provider should have the ability to hand out addresses via DHCP, and that’s likely the easiest way to use this configuration. A static IP can be set if necessary, though.
A public network configuration is much the same as a private network, with the exception that the network adapter of the guest machine is actually bridged to an adapter that is connected to the upstream network. You can either specify the adapter to bridge to in the Vagrantfile like this:
config.vm.network "public_network", bridge: "en1: Wi-Fi (AirPort)"
Or, you can leave it unspecified, and when you do a
vagrant up, a wizard will prompt you for the adapter to bridge to, like this:
==> default: Available bridged network interfaces: 1) en0: Wi-Fi (AirPort) 2) en1: Thunderbolt 1 3) en2: Thunderbolt 2 4) bridge0 5) p2p0 6) awdl0 ==> default: When choosing an interface, it is usually the one that is ==> default: being used to connect to the internet. default: Which interface should the network bridge to?
One word of wisdom when using this wizard – @vmcutlip and I found out after fiddling with it a bit that the wizard is asking for the menu number, not the interface name. So specifying ‘en0’ will bomb, and not be very specific about why. If you want en0, just type ‘1.’
As mentioned, the implementation of these three options will differ depending on the underlying provider. So be sure to do some research into the exact provider in use for your case. Next up in the series, Teardown!