Vagrant ping or curl from guest to host machine
Asked Answered
E

3

12

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.

Emmi answered 24/6, 2015 at 22:2 Comment(0)
E
24

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

Emmi answered 27/6, 2015 at 13:51 Comment(0)
J
10

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.

Jobey answered 25/6, 2015 at 6:32 Comment(6)
That's interesting about the host's ip from the guest ending with 1. Is it documented anywhere? And what happens if you set the guest ip to something ending with a 1? Would the following just not work? config.vm.network "private_network", ip: "192.168.50.1"Okay
I tries the first ten and neither of them worked: curl: (7) Failed connect to 192.168.33.1:3000; No route to hostEmmi
@Okay this is described in the book Vagrant: Up and Running written the by original creator of Vagrant. If I remember correctly, it was present in the documentation too. However, I could not find it now on the current version of the documentation.Jobey
@embasbm what is the IP that you placed in Vagrantfile. Is this 192.168.50.4 or 192.168.33.4? Please provide the exact line of the Vagrantfile that you are using. Also you should check your firewall settings.Jobey
@Jobey This is what I got in my Vagrantfile: config.vm.network "private_network", ip: "192.168.33.64" Must be something with firewall settingsEmmi
Can you ping guest from host $ ping 192.168.33.64? If you can ping guest from host but not the opposite then this is probably due to firewall settings. You can disable the firewall temporarily and then check whether it works or not.Jobey
M
0

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)

Munos answered 15/1, 2022 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.