Chapter 5. Networking

Table of Contents

5.1. Public networking
5.2. Private networking
5.3. Port forwarding
5.4. Using the vagrant-hostmanager plugin

One of the common reasons why we run vagrant boxes is so that we can run server applications in them, either for testing or for development or something else. No matter what the reasons are why you setup vagrant, you will need a way to access the servers that are running on your guest machine. There are three ways to do this (1) setup your vm as a public network (2) set it up as private network and lastly (3) use port forwarding. Each of these options have their advantages and disadvantages. Your general security situation and project goals will largely determine which approach is right for you.

5.1. Public networking

This is known as bridge networking. If you go for this setup, the guest computer, your vm, will actually appear as another node in the current network segment. It will appear as another computer in your network neighborhood.

Figure 5.1. Public network configuration

public network

We’ll look at two ways

[Tip]Tip

In a public network, you may be able to refer to your guest machine via its hostname because your DNS server might be able to pick up the hostname of the vm when the DHCP server gives it an IP address. But not when you assign a static IP address

Example 5.1. Vagrantfile for public networking, static ip

Vagrant.configure(2) do |config|
   config.vm.network "public_network", ip: "192.168.1.210"
end

This type of configuration will boot up your machine and assign itself a static IP address of 192.168.1.210. It will appear as another node in the network segment.

[Warning]Warning

Consult your network administrator if it is okay to use any specific IP addresses. Most network admins configure the workstation endpoints to get their IP addresses dynamically from a DHCP server

Example 5.2. Vagrantfile for public networking, dynamic ip

Vagrant.configure(2) do |config|
   config.vm.network "public_network", type: "dhcp"
end

In this type of configuration, the guest machine will act like it is another node of the network segment. It will listen to any DHCP broadcast, if it finds any, it will fetch an IP address from the server.