来宾机器之间的流浪通信

Vagrant communication between guest machines

我有多个这样的 Vagrant 机器:

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 

我想要 vagrant1 机器能够与 vagrant2 机器通信。到目前为止,我可以从我的主机连接,但来宾机器彼此无法访问。

如何启用来宾计算机之间的通信?

尝试一起定义框;

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

好的,我发现了错误 - 你错误地使用了配置变量,你写成 config.vm. 的所有内容都对所有虚拟机有效,即使它在一个块内,所以在你的情况下它正在创建多个网络接口.

所以你的 Vagrantfile 的简化版本可以写成

# -*- 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

注意:当您使用静态 IP 时,您不需要转发端口,因为您可以直接从该 IP 访问