Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to Vagrant private network from host

People also ask

How do I find my vagrant IP address?

The IP address can be determined by using vagrant ssh to SSH into the machine and using the appropriate command line tool to find the IP, such as ifconfig .

How do I access vagrant from another computer?

I found how to connect to another computer locally by entering the vagrant environment, vagrant up --> vagrant ssh . And connected to another computer by typing in ssh [email protected] where 192.168. x.x is the local address to the computer.

Which interface should the network bridge to vagrant?

Default Network Interface If more than one network interface is available on the host machine, Vagrant will ask you to choose which interface the virtual machine should bridge to. A default interface can be specified by adding a :bridge clause to the network definition.


Based on the output provided, the box has 2 network interfaces, 1 is the default NAT and the other private - ask you said.

The reason why you are not able to access the web site hosted within the VM thru the private interface: it could be that host eth0 or wlan0 IP address is not in the same network as the private interface -> 192.168.50.4/24 and there is no route.

To access the the site hosted by the web server within the guest, you have the following options:

1. NAT port forwarding

Forward the web port, e.g. 80 to host's 8080 (you can't use 80 because it is a privileged port on *NIX). Add the following

Vagrant.configure("2") do |config|
  config.vm.network "forwarded_port", guest: 80, host: 8080,
    auto_correct: true
end

NOTE: auto_correct will resolve port conflicts if the port on host is already in use.

DO a vagrant reload and you'll be able to access the site via http://localhost:8080/

2. Public Network (VirtualBox Bridged networking)

Add a public network interface

Vagrant.configure("2") do |config|
  config.vm.network "public_network"
end

Get the IP of VM after it is up and running, port forwarding does NOT apply to bridged networking. So you'll be accessing the site by using http://IP_ADDR, if within the VM it binds to 80, otherwise specify the port.


One more possibility just for future reference.

Normally when you create VMs using private networking, Vagrant (Virtualbox? not sure) creates corresponding entries in the host's routing table. You can see these using

netstat -rn

Somehow my host had gotten into a state where creating the VMs did not result in new routes appearing in the routing table, with the corresponding inability to connect. Again you can see the routes not appearing using the command above.

Creating the route manually allowed me to reach the VMs. For example:

sudo route -nv add -net 10.0.4 -interface vboxnet

(Substitute the appropriate network and interface.) But I didn't want to have to do that.

Based on this question, I tried restarting my host and Vagrant started automatically creating the routing table entries again.

Not sure exactly what the issue was, but hopefully this helps somebody.


Your interface is down

I had the same issue. It was my vboxnet0 interface who was down. Within the listing of ip addr you have <BROADCAST,MULTICAST> for your interface but it should be <BROADCAST,MULTICAST,UP,LOWER_UP>.

That's mean you interface is down.

You can confirm with sudo ifconfig. The interface will not be shown but if you add -a you will see it : sudo ifconfig -a.

how to bring it up

So to bring it up you can do :

sudo ifconfig vbox

OR

sudo ip link set vboxnet0 up

Both works.


I ended up getting the private network to work as well by deleting it within Virtual Box. When I recreated it again with vagrant up, the ip config became:

vboxnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
    link/ether 0a:00:27:00:00:00 brd ff:ff:ff:ff:ff:ff
    inet 192.168.50.1/24 brd 192.168.50.255 scope global vboxnet0
       valid_lft forever preferred_lft forever

I had a similar issue on my Mac. VirtualBox uses host only for private networks. To use as an internal network I had to add this to the private network configuration:

"virtualbox__intnet: true"

Alternatively, you could use manual port forwarding via SSH (SSH tunneling):

ssh -L 80:127.0.0.1:80 [email protected] -p 2222

That binds host port 80 to VM port 80 via your SSH session to the VM.