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:

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

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:

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

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” 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:  Bcast:  Mask:
         inet6 addr: fe80::a00:27ff:fe88:ca6/64 Scope:Link
         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:  Bcast:  Mask:
         inet6 addr: fe80::a00:27ff:fe57:8da1/64 Scope:Link
         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:  Mask:
         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
PING ( 56(84) bytes of data.
64 bytes from icmp_req=1 ttl=64 time=0.698 ms
64 bytes from icmp_req=2 ttl=64 time=0.509 ms
64 bytes from 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: "public_network"
2. With static IP "public_network", ip: ""

We are done with Vagrant Networking…