Networking With Vagrant

In this lesson, we'll learn about networking concepts in Vagrant and how to implement them.

Networking

Private IP addresses

Some IP addresses are set aside so that they can’t be used in the public space/Internet.

These IP addresses are for internal private use only and are not routable on the public internet.

The public and private IP addresses lie in specific ranges, and we can use these ranges to identify whether a given IP is private or public.

From To
10.0.0.0 10.255.255.255
172.16.0.0 172.31.255.255
192.168.0.0 192.168.255.255

Any IP address residing between these ranges are private addresses.

Public IP addresses

This course does not require a deep understanding of public IP addresses. We will only look into classful addressing of public IPs to better understand them.

In classful addressing, different ranges of IP addresses are assigned specific classes, as shown in the table below. Each class of IP addresses serves a specific function.

Class From To
A 0.0.0.0 127.255.255.255
B 128.0.0.0 191.255.255.255
C 192.0.0.0 223.255.255.255
D 224.0.0.0 239.255.255.255
E 240.0.0.0 255.255.255.255

Classes A, B, and C are publicly available IP addresses on the internet. These classes contain unicast addresses. In the case of unicast addresses, only one single device on the internet is trying to connect to another single device. Whereas, Class D consists of multicast addresses, which means one device can connect to multiple devices. These are used in enterprises for meetings, etc. These addresses are not available on the public internet.

Lastly, Class E IP addresses are reserved for future or experimental purposes only.

Now, let’s get back to our Vagrantfile and implement some network-based settings.

Configure network settings

The Vagrantfile generated in the last lesson is shown below.

Open up your Vagrantfile.

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.hostname = "server.example.com"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = "2"
  end
end

Assign a private IP

Let’s try to assign a private IP address to our Virtual Machine.

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.hostname = "server.example.com"
  config.vm.network "private_network", ip: "192.168.10.1"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = "2"
  end
end

Remember the private IP ranges discussed above. The IP address assigned in the Vagrantfile above lies in one of those ranges.

Assign a public IP

Now, let’s try to assign a public IP address to our Virtual Machine using the Vagrantfile.

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.hostname = "server.example.com"
  config.vm.network "public_network"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = "2"
  end
end

This is a simple example of how we can assign a public network so that our Vagrant VM can be accessed by anyone.

We need to update the config.vm.network field as shown below.

config.vm.network "public_network"

This config will load the public IP address via DHCP.

Now, you can access your VM and check your assigned IP address using the following command:

vagrant ssh

Get hands-on with 1200+ tech skills courses.