1

I have multiple Vagrant machines like so:

config.vm.define 'vagrant1' do |vagrant1|
    config.vm.provider :virtualbox do |vb|
        vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
    end
    vagrant1.vm.box = 'ubuntu/trusty64'
    vagrant1.vm.network 'forwarded_port', guest: 80, host: 8080 
    vagrant1.vm.network 'forwarded_port', guest: 443, host: 8443
    vagrant1.vm.network 'forwarded_port', guest: 27017, host: 27017
    # Create a private network, which allows host-only access to the machine
    # using a specific IP.
    config.vm.network 'private_network', ip: '192.168.56.11'

    ENV['LC_ALL']='en_US.UTF-8'

end

config.vm.define 'vagrant2' do |vagrant2|
    config.vm.provider :virtualbox do |vb|
        vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
    end
    vagrant2.vm.box = 'ubuntu/trusty64'
    vagrant2.vm.network 'forwarded_port', guest: 80, host: 8081
    vagrant2.vm.network 'forwarded_port', guest: 443, host: 8444
    vagrant2.vm.network 'forwarded_port', guest: 27017, host: 27018
    # Create a private network, which allows host-only access to the machine
    # using a specific IP.
    config.vm.network 'private_network', ip: '192.168.56.12'

    ENV['LC_ALL']='en_US.UTF-8'
end 

What I would like is for vagrant1 machine to be able to communicate with vagrant2 machine. So far, I can connect from my host machines but the guest machines are unreachable to each other.

How do I enable communication between guest machines?

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • whats your virtualbox version and can you confirm you have guest additions on guest ? I just quickly test using vbox 5.1.10 and vagrant 1.9.2 - it works as expected, can `ping 192.168.56.12` from vagrant1 and vice versa – Frederic Henri May 18 '17 at 16:14
  • @FrédéricHenri 5.1.10 VBox and Vagrant Installed Version: 1.9.0. I used `https://github.com/dotless-de/vagrant-vbguest` to install guest additions right after I read your comments. Didnt help. – An SO User May 18 '17 at 17:36

2 Answers2

4

ok I spotted the error - you have wrong use of the config variable, everything you write as config.vm. is valid for all VMs even when its within a block so in your case it was creating multiple network interfaces.

So a simplified version of your Vagrantfile could be written as

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

    config.vm.box = 'ubuntu/trusty64'
    config.vm.provider :virtualbox do |vb|
        vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
    end

    config.vm.define 'vagrant1' do |vagrant1|
        vagrant1.vm.network 'private_network', ip: '192.168.56.11'
        ENV['LC_ALL']='en_US.UTF-8'
    end

    config.vm.define 'vagrant2' do |vagrant2|
        vagrant2.vm.network 'private_network', ip: '192.168.56.12'
        ENV['LC_ALL']='en_US.UTF-8'
    end 

end

Note: you dont need to forward port when you're using a static IP as you can access directly from the IP

Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
0

Try defining the boxes together;

Vagrant.configure("2") do |config|
    config.vm.define 'vagrant1' do |vagrant1|
        config.vm.provider :virtualbox do |vb|
            vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
        end
        vagrant1.vm.box = 'ubuntu/trusty64'
        vagrant1.vm.network 'forwarded_port', guest: 80, host: 8080 
        vagrant1.vm.network 'forwarded_port', guest: 443, host: 8443
        vagrant1.vm.network 'forwarded_port', guest: 27017, host: 27017
        # Create a private network, which allows host-only access to the machine
        # using a specific IP.
        config.vm.network 'private_network', ip: '192.168.56.11'

        ENV['LC_ALL']='en_US.UTF-8'

    end

    config.vm.define 'vagrant2' do |vagrant2|
        config.vm.provider :virtualbox do |vb|
            vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
        end
        vagrant2.vm.box = 'ubuntu/trusty64'
        vagrant2.vm.network 'forwarded_port', guest: 80, host: 8081
        vagrant2.vm.network 'forwarded_port', guest: 443, host: 8444
        vagrant2.vm.network 'forwarded_port', guest: 27017, host: 27018
        # Create a private network, which allows host-only access to the machine
        # using a specific IP.
        config.vm.network 'private_network', ip: '192.168.56.12'

        ENV['LC_ALL']='en_US.UTF-8'
    end 
end
Tom
  • 4,257
  • 6
  • 33
  • 49
  • oh I forgot to mention that these are defined inside the same `Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| ` block. – An SO User May 18 '17 at 15:52
  • Oh! Well, then I'm not sure. The [docs](https://www.vagrantup.com/docs/networking/private_network.html) say this is possible in the way you've configured. How are you actually testing it? – Tom May 18 '17 at 15:56
  • pinging the static IP of the second machine after SSH-ing into the first – An SO User May 18 '17 at 15:58