access private VM from other computer over wifi

2019-03-12 19:01发布

问题:

I have a private network VM for developing on my mac. I'd like for my android device to be able to communicate with the VM on my mac. Currently I can visit the IP defined in my Vagrantfile, 10.10.10.10, on my mac and access it just fine but I can't access it via my phone on the same wifi.

What do I need to do to make it available across my local network and visible to my phone over wifi?

Here's my Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "precise64"
  config.vm.box_url = "http://files.vagrantup.com/precise64.box"

  config.vm.network :private_network, ip: "10.10.10.10"
    config.ssh.forward_agent = true

  config.vm.provider :virtualbox do |v|
    v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
    v.customize ["modifyvm", :id, "--memory", 1024]
    v.customize ["modifyvm", :id, "--name", "PHPBoxWith54"]
  end

  nfs_setting = RUBY_PLATFORM =~ /darwin/ || RUBY_PLATFORM =~ /linux/
  config.vm.synced_folder "./", "/var/www", id: "vagrant-root" , :nfs => nfs_setting
  config.vm.provision :shell, :inline =>
    "if [[ ! -f /apt-get-run ]]; then sudo apt-get update && sudo touch /apt-get-run; fi"


  config.vm.provision :shell, :inline => 'echo -e "mysql_root_password=root
controluser_password=awesome" > /etc/phpmyadmin.facts;'

  config.vm.provision :puppet do |puppet|
    puppet.manifests_path = "manifests"
    puppet.module_path = "modules"
    puppet.options = ['--verbose']
  end
end

回答1:

You are using a Private Network IP which is only accessible by the Host machine (NOT visible to other machines even they are in the same WLAN).

In your case, the best choice is to use Public Network (bridged) so that your Android device can access it.

add config.vm.network "public_network" in your Vagrant file in the config block.

BTW: the default NAT mode is fine but you'll have to set proper port forwarding rules for each service you want to access (e.g. SSH, HTTP, HTTPS etc...).



回答2:

You can easily access your Vagrant from any machine on your network, by simply ensuring that the port forwarding rules aren't bound to your localhost (127.0.0.1).

If you're using VirtualBox as your provider, you can change this on the fly, so you can have it be private by default (which is of course more secure), and then you can go change it in VirtualBox while your VM is running to expose the port to other machines on your network (and possibly the internet, so be careful!)

To expose the port:

  • Start Virtual Box
  • Select your VM in the left hand side bar
  • Click Settings | Network | Advanced | Port Forwarding
  • Find the port you want to expose in the port list
  • Set it's Host IP to an empty string, and click OK.

The port is now available to other machines on your network, and possibly the internet, so don't do this unless you're positive you're ok with opening up the port!

Of course to revert it, just do the same process and set the Host IP to 127.0.0.1 again.



回答3:

This can be just a small problem with port forwarding / setting a dmz. Since it's on the same machine; you can interact since it's physically on the same adapter. However if you are trying to access outside of that, you should try tinkering with the ports.



回答4:

A little expansion on the accepted answer: after adding the public network option in your vagrant file and reloading it, ssh into it the old way and then run ifconfig to get the IP you can use to access the vm in your local network from any other device.

Docs: https://www.vagrantup.com/docs/networking/public_network.html