6

I have vagrant 1.2.2 installed in my development machine; a laptop. I use same laptop in three different environments with different networking settings.

My VagrantFile looks like:

Vagrant.configure("2") do |config|
    config.vm.box = "lucid32"
    config.vm.network :forwarded_port, guest: 80, host: 88
    config.vm.network :forwarded_port, guest: 56789, host: 56789
    config.vm.network :public_network, :bridge => 'Intel(R) 82579V Gigabit Network Connection'
    config.vm.hostname = "web"
end

It successfully sets hostname as web in headless Ubuntu.

But when I try to ping web from host OS (Windows), it doesn't resolve the IP address.

I cannot change the hosts file in Windows every time as class C subnet mask and IP ranges are different when I move to other office.

Is there a way to access guest from host machine via hostname web?

vulcan raven
  • 215
  • 1
  • 2
  • 10

3 Answers3

5

Why not also specify a private network that your host can use to connect to the VM?

http://docs.vagrantup.com/v2/networking/private_network.html

The private IP will stay the same wherever you go. You could then add a hosts entry for this private IP.

Andy Shinn
  • 4,131
  • 8
  • 38
  • 55
  • Thanks. But by doing that, I would lose the ability to share my webserver, running in VM, with other users of the network. Is there a way to bridge the local network with my system internal one, so any traffic coming to http://web/ should go to VM? – vulcan raven Jun 16 '13 at 01:02
  • You would have to be dynamically modifying the DNS resolvers the other clients are using at each of those locations to add the `web` name to them. Is that a feasible task? If so, then it might be possible to run some sort of dynamic DNS announcement on the VM. – Andy Shinn Jun 16 '13 at 01:16
  • +1, thanks for the clarification. On second thought I figured out how to do that and it works! Check out my answer. – vulcan raven Jun 16 '13 at 08:05
4

Following Andy's advise, I created a private network with a static IP address. Then in the Windows host, I configured hostname with that IP address. This way, I am able to access the webserver from local/host OS.

Now, in VagrantFile, as my host computer's port 88 is forwarded to guest's 80(config.vm.network :forwarded_port, guest: 80, host: 88), I can access webserver from local LAN computers with: http://host-computer-name:88/.

vulcan raven
  • 215
  • 1
  • 2
  • 10
1

To access 'web' from any other computer, you will need to add a DNS entry in your DNS server on your LAN.

However, from what you have described, this could become problematic, because you will need to update your dns entry every time your laptop gets a new ip address.

If you want to occasionally share this vm with a few other people on the network, then you should just tell them the ip address every time it changes.
If a lot of people will access the vm, or your ip changes often, then you should really just move the vm off your laptop and onto a dedicated server, or get an ec2 instance.

Vagrant is great at spinning up vm's on your own workstation, but not great for sharing vm's with other people.

spuder
  • 1,695
  • 2
  • 25
  • 42