Vagrant communication between guest machines

2019-08-02 14:04发布

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?

2条回答
贼婆χ
2楼-- · 2019-08-02 14:29

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

查看更多
Explosion°爆炸
3楼-- · 2019-08-02 14:34

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
查看更多
登录 后发表回答