vagrant homestead - public network not working on osx - macos

I've been trying all day to get a public network setup using Vagrant 1.6.5 with VirtualBox. I really need to be able to access all my Homestead sites on my other devices (iPad, iPhone, Android phone, etc.) on my local network. The box provisions fine but the sites all timeout.
homestead.rb
config.vm.network "public_network", :bridge => "en0: Wi-Fi (AirPort)", :ip => "192.168.1.200"
My host is OS X mavericks using DHCP (192.168.1.137).
Private networking works fine, but I can't get public networking to work with any variation of the above config.

You can just grab the host's internal IP address (NOT the VM ip) and add the port :8000. Let's say your host ip is 192.168.0.5, then from any other device on your network, you can just browse to the host at 192.168.0.5:8000 and you'll hit the VM and see the Laravel home page.
This works great for me with the default homestead.rb setting:
# Configure A Private Network IP
config.vm.network :private_network, ip: settings["ip"] ||= "192.168.10.10"

To get my homestead machine to be visible from my LAN I did the following:
Edit the Homestead/scripts/homestead.rb file and set config.vm.network :public_network, ip: "192.168.1.123" (just make sure the IP is free).
Do a vagrant reload
Make sure my old local hosts file now points to the new IP.
The result is I can ping 192.168.1.123 from another machine in my LAN.

What is the error that you are getting? Make sure that 192.168.1.200 is not being used by any other device.
Network configuration for vagrant boxes

This row works fine for me:
config.vm.network :public_network, ip: settings["ip"] ||= "192.168.10.10", bridge: "eth0"
Then it uses the ip variable from ~/.homstead/Homestead.yaml and if thats not set it uses 192.168.10.10

Related

Expo and Vagrant

I am trying to build a react-native-app on a vagrant linux box.
I am trying to access the app on my phone using expo but it just does not work.
Has anyone managed to make this work with port forwarding or something similar?
You need to have the linux box on the same network as the phone.
To make this a little more specific, the vagrant file should include the following line(the ip address is optional):
config.vm.network "public_network", ip: "ip address"
for example:
config.vm.network "public_network"
config.vm.network "public_network", ip: "192.168.1.9"
Here are a couple of useful links:
Youtube tutorial by sureshkm
Vagrant docs

How configure Vagrant so doesnt block forwarded ports on host when on private network

I want a vagrant VM to behave like a box not hosted on my host machine.
I want to be able to do
curl <vagrant private ip> #nginx on the vm
and have that work from the host machine
curl localhost #e.g. iis
and have that work from the host machine
but it seems it's not possible as when you do port forwarding it always occupies the host port too on the loopback/or IP and you cant restrict it to just the private IP of the VM, basically, you cant have the VM running and start iis.
If that is the case ok, that's that.
currently, my Vagrant file has
config.vm.network "forwarded_port", guest: 80, host: 80
config.vm.network "private_network", ip: "192.168.33.11
I know the forwarded_port has guest_ip and host_ip settings but I didn't find they did anything when I tried to set to the private network IP.
Any suggestion of easy workaround also welcome.

Other computer on the same intranet can't connect to my web server on my virtual machine

I used vagrant to setup a virtual machine and then on that virtual machine, I set up a webserver with ip I config like below:
config.vm.network "private_network", ip: "192.168.33.12"
config.vm.network "public_network", ip: "192.168.33.13"
config.vm.network :public_network
And I connected successfully to my web server on my computer via this IP 192.168.33.12 but then I tried to use other computer on the same network, I couldn't connect to it (192.168.33.13 neither)
It's because your virtual machine missed default gateway for your network.
Try adding your LAN default gateway to the virtual machine.
ssh -p 2222 vagrant#localhost
sudo route add default gw 192.168.33.254
where 192.168.33.254 has to be change with correct default gateway.

How can I access a vagrant guest from another virtualbox guest?

The scenario is that my dev environment is on a Vagrant box on my laptop (host) and I would like to do browser testing in a vitualbox vm, so I need to see one vm from another.
The vagrant box's port is :8080 which is forwarded to the host on the same port :8080. So I can see the server from the host at localhost:8080
Which address should I be using for the browser testing vm?
The testing vm's default gateway?
The vagrant vm's ip?
The host's virtual network ip?
And should I be using a NAT or host only adapter on the browser testing vm?
That makes for a lot of combinations, all of which I believe I have tried. What else do I need to understand here?
In your use case, you should be using Bridged networking (Public Network in Vagrant). If the VMs reside on the same host, you can even use internal (Private Network in Vagrant).
If using Public Network, the VM's 2nd NIC will be able to obtain an IP address from the DHCP server in your network (e.g. your home router).
Simply add the following code block in your Vagrantfile and do a vagrant reload
Vagrant.configure("2") do |config|
config.vm.network "public_network"
end
You should be able to get the IP address by using vagrant ssh and ifconfig / ip addr show.
In case you don't want to go with public_network just like me then you should do the steps below using private_network:
Open Vagrantfile from your project root
Search for config.vm.network
Add this line config.vm.network "private_network", ip: "192.168.33.10". Remember this is not the IP of your base machine it's a virtual-box IP address and your machine IP should be different. You can say it's a fake IP address so change it to anything else like 192.168.30.20.
Reload your vagrant using vagrant reload.
Now go to your other virtual guest in my case it's the Windows Guest 2. My base is Linux Mint Vagrant box is on Ubuntu Guest 1. Open C:\Windows\System32\drivers\etc\hosts file as admin and do the above IP's entry in there like 192.168.33.10 local.youralias.com. And save the file, after that you can now browse the site now at http://local.youralias.com/.
In case your guest 2 is also Linux just edit this file sudo vi /etc/hosts, and add this line at top of it 192.168.33.10 local.youralias.com. Now save and exit and browse the URL :)
Enjoy! Happy coding.
Adding to accepted answer, you can actually set IP and specify which network interface to use.
My setup on linux box via wifi and static IP:
You can find your wifi interface name by running ifconfig command.
Vagrant.configure("2") do |config|
config.vm.network "public_network", :bridge => 'wlp8s0', ip: "192.168.1.199"
end
This may have many source cause. In my case, I use vagrant fedora boxe.
I tried:
First using the private_network that I attached to a host only adapter and launched httpd service to test the connection between guest and host
config.vm.network "private_network", type: "dhcp", name: "vboxnet2"
config.vm.network "forwarded_port", guest:80, host:7070
but I was not able to ping my guest machine from the host and could no telnet the httpd service opened
Second using public_network and launched httpd service to test connectivity
config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)", use_dhcp_assigned_default_route: true
I could ping my guest from my host but I could not telnet the httpd service.
For this two use case, the issue was that the port 80 on the fedora guest host was blocked by the firewall. Here is what fixed the issue and get all working for both privat_network and public_ntwork:
firewall-cmd --permanent --add-port 80/tcp #open the port permanently
firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --list-port # list to check if the port was opened
systemctl stop firewalld # stop and open the firewall service
systemctl start firewalld
Old question, new answer: [disclaimer: i am not a vagrant expert]
both solutions might work but the solution in the "vagrant way of thinking" is that some component in your guest (rinetd?) should forward any requests to unknown ports to the host. From the host the request could then be mapped (via vagrant port forwarding) to a services that is running in the other guest.
So, to resume:
1.in guest-1 we do localhost:1234. Guest-1 will detect that this port is not available and forward to host
2. the host will check the vagrant port forwarding and forward to guest-2
3. in guest-2 we have some nice service listening to post 1234
4. done.

Setting a VM's mac address in Vagrant

The documentation lists that the mac address of a VM can be set in the Vagrantfile, however everything I add seems to end up being a syntax error. Anyone successfully done this?
I used this:
config.vm.network :bridged , :mac => "080027XXXXXX"
and got what I wanted.
The docs are unclear on what the syntax for the options hash were, and there seemed to be no example on what this should look like. So, here it is! Bridged with a mac address (edited of course). This brings up eth1 with the mac specified, which makes my DHCP server happy, and gives it a proper fqdn on my network.
This is an old question, but I had the same issue just now. Vagrant documentation v2 still seems incomplete. In the end I used this line in the Vagrantfile with vagrant 1.2.7:
config.vm.network "public_network", :bridge => 'enp4s0', :mac => "5CA1AB1E0001"
This:
sets the host interface named 'enp4s0' as the bridge interface,
which as 'eth0' on the guest is then assigned an ip address by the same DHCP the host uses
Also sets 5C:A1:AB:1E:00:01 as the guest's mac address
hmm, the network config didn't help in my case. After defining the MAC Address directly in the Vagrantfile via config.vm.base_mac = "MyEth0MacAddressWithoutSlashes" my machine started =)
On Vagrant version 2.0.1, I write in the Vagrantfile for a private_network (provider = VirtualBox ; version 5.2.0) :
config.vm.network "private_network", ip: "X.X.X.X", mac: "080027xxxxxx"
The information provided below is outdated. As per documentation to allow IP to be assigned via DHCP simply use:
config.vm.network "public_network"
This way you don't need to deal with mac address, it will be generated on its own.
If you need custom mac address attached to the network device then:
config.vm.network "public_network", :mac=> "080027xxxxxx"

Resources