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.

Emba Moussa
  • 662
  • 8
  • 18

3 Answers3

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

Emba Moussa
  • 662
  • 8
  • 18
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.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • 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"` – tobuslieven Jun 25 '15 at 15:08
  • 1
    I tries the first ten and neither of them worked: `curl: (7) Failed connect to 192.168.33.1:3000; No route to host` – Emba Moussa Jun 25 '15 at 16:00
  • 1
    @tobuslieven 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. – taskinoor Jun 25 '15 at 19:39
  • @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. – taskinoor Jun 25 '15 at 19:41
  • @taskinoor This is what I got in my Vagrantfile: `config.vm.network "private_network", ip: "192.168.33.64"` Must be something with firewall settings – Emba Moussa Jun 25 '15 at 20:47
  • 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. – taskinoor Jun 25 '15 at 21:24
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)

Ashkan Sarlak
  • 7,124
  • 6
  • 39
  • 51