Vagrant ping or curl from guest to host machine

6.5k Views Asked by At

I wonder how I can run the command, using the terminal, from my vagrant machine:

$ ping localhost:3000

or

$ curl http://localhost:3000

In host machine (OSX) I have a rails server running in localhost:3000, so I expect something to show in the rails log.

3

There are 3 best solutions below

0
Emba Moussa On BEST ANSWER

When I run in the VM:

$ ip route show

In the output there is a line like:

default via 10.0.2.2 dev enp0s3 proto static metric 1024

That is the IP to ping from the guest:

curl http://10.0.2.2:3000

6
taskinoor On

Inside the vagrant machine localhost refers to the guest vagrant machine, i.e. localhost doesn't refer to host machine. One way to access host machine from guest is to configure a private network. You can specify a static private IP for vagrant, like this:

Vagrant.configure("2") do |config|
  config.vm.network "private_network", ip: "192.168.50.4"
end

After this guest is accessible from host via 192.168.50.4 and host is accessible from guest via 192.168.50.1, i.e. the end octate for host's IP will be 1 inside guest machine.

After vagrant up, you can do this from inside guest machine:

$ ping 192.168.50.1
$ curl http://192.168.50.1:3000

Note that, if you have some strict firewall setup then you have to allow connection from 192.168.50.4.

0
Ashkan Sarlak On

You can also set network to "public_network" in the guest machine's config file.

In the Vageantfile just uncomment the line:

config.vm.network "public_network"

Restart the machine:

vagrant halt
vagrant up

Do the ip addr show and your ip on the public network is the one listed under eth1 (instead of the usual eth0)