35

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 can't 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?

langlauf.io
  • 3,009
  • 2
  • 28
  • 45
user2439278
  • 1,222
  • 7
  • 41
  • 75

5 Answers5

24

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

John Veldboom
  • 2,049
  • 26
  • 29
Terry Wang
  • 13,840
  • 3
  • 50
  • 43
  • Any in local network can access to my VM ? with vagrant user and password? – Robert Jan 29 '14 at 14:22
  • Yes, with `ssh vagrant@` – Ricardo García May 27 '14 at 08:08
  • For me it only worked with `config.vm.network "public_network", :bridge => "eth0", auto_config: false` and then using `shell` provisioners to add manually the settings for eth1. Any kind of combination that includes the 'ip' in the `config.vm.network` does not work here. ? – Nikos Alexandris Dec 19 '15 at 20:19
7

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.

gs_za
  • 361
  • 5
  • 13
  • Is this [drop_nat_interface_default_route => false](https://github.com/nickrw/vagrant/commit/a0b684a4ab4a447d408b083c7d62d5f6b5e7d817#diff-0be8decc07f9990a7603eeca8a88b77cR126) relevant? – Nikos Alexandris Dec 19 '15 at 14:43
  • @Nikos I am not sure but it still worked without it. – gs_za Jan 07 '16 at 08:34
2

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
Jason
  • 514
  • 5
  • 21
0

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.

-8
  1. Single VM environment

    you can use command: vagrant ssh

  2. Multi VM environment

    you can use command: vagrant ssh {hostname}

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
Dan Du
  • 1
  • 1