Vagrant Networking


In previous post “Defining Multiple Virtual Machines in Vagrant”. Now we will continue with network settings of the virtual machines.

Port Forwarding:

In Vagrant networking it is possible to set port forwarding and it is very easy. The only limitation is that “The forwarded port must be bigger than 1024”. Here is an example config:

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
   config.vm.define "box1" do |box1|
      box1.vm.box = "precise64"
      box1.vm.hostname = "ubuntutest"
   end
   config.vm.define "box2" do |box2|
      box2.vm.box = "ubuntu/trusty64"
      box2.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
   end
end

In this example we can see that localhost:8080 connections in host machine will be forwarded to 80 port of virtual machine. Now lets test it and see if the forwarding works correctly:
1. Run “vagrant up” and connect box1 “vagrant ssh box2
2. Run “sudo apt-get update” and install Apache web server “sudo apt-get -y install apache2
3. Open your browser in host machine and goto “http://localhost:8080”. Below you can see the screenshot:

Vagrant Networking - Port Forwarding

Host only network:

In Vagrant networking you can also use host only networks. In this network it will be possible to communicate between virtual machines. Now we will add new lines to our config and give IPs to virtual machines:

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
   config.vm.define "box1" do |box1|
      box1.vm.box = "precise64"
      box1.vm.hostname = "ubuntutest"
      box1.vm.network "private_network", ip: "192.168.33.10"
   end
   config.vm.define "box2" do |box2|
     box2.vm.box = "ubuntu/trusty64"
     box2.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
     box2.vm.network "private_network", ip: "192.168.33.11"
  end
end

Now lets check it:
1. Use “vagrant destroy --force” to destroy the running virtual machines.
2. “vagrant up” and “vagrant ssh box1
3. “ifconfig” and see your IP settings.
4. “ping 192.168.33.11” to see if virtual machines can communicate. Below are the details:

[email protected]:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:88:0c:a6  
         inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
         inet6 addr: fe80::a00:27ff:fe88:ca6/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:647 errors:0 dropped:0 overruns:0 frame:0
         TX packets:455 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000
          RX bytes:78614 (78.6 KB)  TX bytes:59066 (59.0 KB)
eth1      Link encap:Ethernet  HWaddr 08:00:27:57:8d:a1  
         inet addr:192.168.33.10  Bcast:192.168.33.255  Mask:255.255.255.0
         inet6 addr: fe80::a00:27ff:fe57:8da1/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:2 errors:0 dropped:0 overruns:0 frame:0
         TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000
          RX bytes:354 (354.0 B)  TX bytes:468 (468.0 B)
lo        Link encap:Local Loopback  
         inet addr:127.0.0.1  Mask:255.0.0.0
         inet6 addr: ::1/128 Scope:Host
         UP LOOPBACK RUNNING  MTU:16436  Metric:1
         RX packets:0 errors:0 dropped:0 overruns:0 frame:0
         TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)[email protected]:~$ ping 192.168.33.11
PING 192.168.33.11 (192.168.33.11) 56(84) bytes of data.
64 bytes from 192.168.33.11: icmp_req=1 ttl=64 time=0.698 ms
64 bytes from 192.168.33.11: icmp_req=2 ttl=64 time=0.509 ms
64 bytes from 192.168.33.11: icmp_req=3 ttl=64 time=0.512 ms

As you can see we have the correct IP address on eth1 and we can also ping to the other virtual machine.

Bridged network:

When you use bridged networking the virtual machine is bridged to the host machine’s physical device. In this state the virtual machine seems to be like another separate machine in the network. This virtual machine can be ping by other computers on your network.

Here is the usage:

1. With DHCP enabled networks:
box1.vm.network "public_network"
2. With static IP
box1.vm.network "public_network", ip: "192.168.0.17"

We are done with Vagrant Networking…