How to access Vagrant Box in public network

2020-05-15 07:12发布

问题:

I had created on e box inside vagrant. In the Vagrantfile, I had given the network as

     Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network :private_network, ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
   config.vm.network :public_network

I cant able to access the VagrantBox outside the VLAN. I need to access the Vagrant Box in public network. How to configure vagrantfile in such a way that I need to access in public network?

回答1:

Uncomment the line in Vagrantfile

config.vm.network :public_network

The file will look like below

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "box_name"
  config.vm.network :public_network
end

Save it, restart the VM by using vagrant reload.

For VirtualBox, it'll use Bridged mode for networking. Which means the VM will acquire an IP address from the DHCP server for the VLAN.

You can also set the VLAN IP with: config.vm.network :public_network, ip: "192.168.0.160"

Refer to => Public Network



回答2:

By default, vagrant deletes the default (working) route on additional bridged networks inside the VMs. My problem which was specific for DHCP could only be solved by configuring the bridged network as follows:

config.vm.network :public_network, :bridge => 'em1',:use_dhcp_assigned_default_route => true

Courtesy of https://groups.google.com/forum/#!msg/vagrant-up/yNhWV42pcgk/NbOck1xqtFQJ There maybe an equivalent for static IPs.



回答3:

I was unable to figure this out using anything I read (which was hours and hours of research). Instead, this is how I figured it out:

Below is my Vagrantfile. The important part for me was config.vm.network :public_network. After reloading vagrant with vagrant reload, I chose the first option of the 4 available bridged network interfaces (I’m not sure if I chose the correct one by luck, or if any would have worked, I’ll experiment), then sshed into the vagrant box with vagrant ssh, did ifconfig, chose one of the 3 ip addresses that it output, pasted that into my browser, and it worked.

The thing no one else seemed to talk about was sshing into the vagrant box and finding one of the ip addresses there. I hope maybe this helps some other networking newb in the future.

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

Vagrant.configure(2) do |config|
  config.vm.box = "bahmni-team/bahmni"
  config.vm.box_check_update = true
  config.ssh.insert_key = false
  config.vm.network :public_network

  config.vm.synced_folder "..", "/bahmni", :owner => "vagrant"
  config.vm.provider "virtualbox" do |v|
     v.customize ["modifyvm", :id, "--memory", 4092, "--cpus", 2, "--name", "Bahmni-RPM"]
  end
end


回答4:

Finally! This is years later but I couldn't find more current info. For me, the problem was that I not only had a private network defined, but also a forwarded port, and all that was working well. I then commented out the private_network, replaced it with public_network, and couldn't reach anything. Tried everything I found here and elsewhere, no go. It's only when I commented out the port forwarding that things started working again, without any of the manual bridging/dhcp configuration rigamarole suggested.



回答5:

  1. Single VM environment

    you can use command: vagrant ssh

  2. Multi VM environment

    you can use command: vagrant ssh {hostname}