user2439278
user2439278

Reputation: 1224

How to access Vagrant Box in public network

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?

Upvotes: 34

Views: 58475

Answers (6)

MacTruck3Code
MacTruck3Code

Reputation: 1

Based on the Vagrant docs for Public Networks, I added route add default gw [private ip of router] to my shell provisioner in the Vagrantfile. This tells my VM to use my router as the default gateway. Then I deleted the previously created one with ip route del default via 10.0.2.2. I opened the required ports on the VM. With my ISP gateway set to IP passthrough mode to my router, I port forwarded the required ports to my VM. Curl your public IP and voilà!

Upvotes: 0

Jason
Jason

Reputation: 534

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

Upvotes: 2

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.

Upvotes: 0

gs_za
gs_za

Reputation: 381

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.

Upvotes: 7

Dan Du
Dan Du

Reputation: 1

  1. Single VM environment

    you can use command: vagrant ssh

  2. Multi VM environment

    you can use command: vagrant ssh {hostname}

Upvotes: -8

Terry Wang
Terry Wang

Reputation: 13920

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

Upvotes: 24

Related Questions