Vagrant accessing domain from another guest machine - vagrant

Now, i have 2 vm machine
First vm running api server.
Second vm acting like client that perform request to the api endpoint.
The api server name is http://api.dev
The client server name is http://client.dev
Here is vagrant file for the api server:
Vagrant.configure(2) do |config|
config.vm.box = "lemp.dev"
config.vm.network "private_network", ip: "192.168.10.10"
config.vm.synced_folder "www", "/var/www/html"
end
Here is vagrant file for the client:
Vagrant.configure(2) do |config|
config.vm.box = "lemp.dev"
config.vm.network "private_network", ip: "192.168.10.11"
config.vm.synced_folder "www", "/var/www/html"
end
Here is host file on my host machine:
192.168.10.11 client.dev
192.168.10.10 api.dev
I can access the http://api.dev from my host machine.
Problem is, i cant access the http://api.dev from my client machine.When i run curl api.dev i got 404 page
What i want is to be able to access to the http://api.dev from another vm instance( the second vm ).

You would need to update the hosts file on the Guests (192.168.10.11 and 192.168.10.10) with the same thing you put on your Host, or you could just use the IPs and not have to worry about doing that.

Related

Consul dev mode unable to see UI using Vagrant

I started a consul agent in dev mode by using the following inside a VM using Vagrant. The command I used is consul agent -dev
I confirmed that the UI is working fine by doing a curl on 8500. curl http://localhost:8500/ui
However when I am testing from the host machine using curl http://172.28.128.3:8500/ui I get the following error.
curl: (7) Failed to connect to 172.28.128.3 port 8500: Connection refused
What could be the error. Following is my VagrantFile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.network "private_network", type: "dhcp"
end
I can see the following when I do vagrant ssh.
System load: 0.07 Processes: 99
Usage of /: 13.5% of 9.63GB Users logged in: 0
Memory usage: 12% IP address for enp0s3: 10.0.2.15
Swap usage: 0% IP address for enp0s8: 172.28.128.3
When I do the following nc -vz 172.28.128.3 8500 on my host mac I get the error
nc: connectx to 172.28.128.3 port 8500 (tcp) failed: Connection refused
There are a number of ways you can expose the running Consul agent UI in the box to the host.
Configure running service port to be forwarded to the host.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.network "private_network", type: "dhcp"
config.vm.network "forwarded_port", guest: 8500, host: 8500
end
The service would be available at http://localhost:8500/ui.
Configure a public network.
Tread with care as this exposes the entire box to the host.
Vagrant.configure("2") do |config|
config.vm.network "public_network", ip: "192.168.0.17"
end
Use vagrant connect command
vagrant connect
The resulting Static IP can be used together with the service port.

Vagrant : why can I not access nginx with domain name?

I am using Vagrant on Windows 10.
I have installed centos 7 vagrant box,it is running correctly.
Vagrantfile is like this:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "dev"
config.vm.hostname = "vagrant-dev"
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "private_network", ip: "192.168.33.3"
config.vm.synced_folder "d:/vagrant_env/www/", "/var/www"
end
I installed nginx in centos 7 vagrant box, and I can access nginx welcome page with ip 192.168.33.3.
I want to access it with domain name,so I configure C:\Windows\System32\drivers\etc\hosts
file in windows:
192.168.33.3 www.example.vagrant //add this line into hosts file of windows
and configure /etc/nginx/conf.d/default.conf file in centos 7:
server {
listen 80;
server_name www.example.vagrant;
//...
}
I try accessing www.example.vagrant on windows host,but it does not work.
what should I do?
There's two better solutions to this:
Use xip.io for your address. In this case http://example.192.168.31.3.xip.io/ will route to your address.
Set up a DNS entry for that which can be resolved. An inexpensive .com or .info test domain at a registrar which makes updating easy (e.g. Amazon Route 53) is always handy for this.
Some programs stubbornly refuse to read entries in /etc/hosts or the equivalent in Windows.
Navigate to 192.168.31.3:8080 in your browser. If you can see your server, then just make your redirect like that: 192.168.31.3:8080 www.example.vagrant
config.vm.network "forwarded_port", guest: 80, host: 8080
At this point you are saying "whatever happens inside Vagrant on port 80, forward it to port 8080 on host machine". You have to communicate with your Vagrant machine on port 8080.
Also your IP is wrong 192.168.33.3 vs 192.168.31.3

Connecting my vagrant and another VM in VirtualBox

For my web development, I use a vagrant install to run a local instance of the website with the following config:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "scotch/box"
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.network :forwarded_port, host: 4567, guest: 80
config.vm.hostname = "some-website"
config.vm.synced_folder ".", "/var/www/public", :mount_options => ["dmode=777", "fmode=666"]
config.vm.provision :shell, path: "bootstrap.sh", :args => "some-website", privileged: false
end
This website is accessible in the browser on the specified IP.
However, I also have VM's for cross browser testing and these VM's don't have access to the IP address of the Vagrant box.
I need them to connect, because I want to do cross browser testing on that locally running vagrant box.
A couple of StackOverflow searches gave me the option of changing the private_network to a public_network, but then I can't access the IP anymore.
Also adding virtualbox__intnet: true doesn't work.
Would anyone be able to come up with a solution for this? It would be great if I could access the IP from the IE9 VM, directly into the Vagrant VM.
So, to clarify, currently:
Laptop can access Vagrant (laptop -> VM)
IE9 VM can't access Vagrant (VM -!> VM)
What I want:
Laptop can access Vagrant (laptop -> VM)
IE9 VM can access Vagrant (VM -> VM)
As Frédéric pointed out: the private_network is actually accessible from another VM on the same host.
My problem seemed to be WordPress itself and not related to Vagrant or the VM setup.

Changing domain mapping on Mac OSX etc/hosts to Vagrant Virtual Machine

I've been trying to change the mapping for a domain and forward it to a Vagrant machine that I've forwarded a port to.
The config in Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.network :forwarded_port, host: 4567, guest: 80
end
I've updated /etc/hosts on my Mac OSX with the following:
127.0.0.1:4567 example.com
Yet when I access from my browser example.com nothing changes, I have also tried to map example.com to go to the Google IP without any luck.
What am I overlooking?
I would just add a static ip, or try removing the port number:
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.50.4"
end
Then in your hosts file change it to read:
192.168.50.4 example.com
More info is available here:
https://docs.vagrantup.com/v2/networking/private_network.html

How to access Vagrant Box in public network

I had created on e box inside vagrant. In the Vagrantfile, I had given the network as
Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network :private_network, ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
config.vm.network :public_network
I can't access the VagrantBox outside the VLAN. I need to access the Vagrant Box in public network. How to configure vagrantfile in such a way that I need to access in public network?
Uncomment the line in Vagrantfile
config.vm.network :public_network
The file will look like below
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "box_name"
config.vm.network :public_network
end
Save it, restart the VM by using vagrant reload.
For VirtualBox, it'll use Bridged mode for networking. Which means the VM will acquire an IP address from the DHCP server for the VLAN.
You can also set the VLAN IP with: config.vm.network :public_network, ip: "192.168.0.160"
Refer to => Public Network
By default, vagrant deletes the default (working) route on additional bridged networks inside the VMs. My problem which was specific for DHCP could only be solved by configuring the bridged network as follows:
config.vm.network :public_network, :bridge => 'em1',:use_dhcp_assigned_default_route => true
Courtesy of https://groups.google.com/forum/#!msg/vagrant-up/yNhWV42pcgk/NbOck1xqtFQJ
There maybe an equivalent for static IPs.
I was unable to figure this out using anything I read (which was hours and hours of research). Instead, this is how I figured it out:
Below is my Vagrantfile. The important part for me was config.vm.network :public_network. After reloading vagrant with vagrant reload, I chose the first option of the 4 available bridged network interfaces (I’m not sure if I chose the correct one by luck, or if any would have worked, I’ll experiment), then sshed into the vagrant box with vagrant ssh, did ifconfig, chose one of the 3 ip addresses that it output, pasted that into my browser, and it worked.
The thing no one else seemed to talk about was sshing into the vagrant box and finding one of the ip addresses there. I hope maybe this helps some other networking newb in the future.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "bahmni-team/bahmni"
config.vm.box_check_update = true
config.ssh.insert_key = false
config.vm.network :public_network
config.vm.synced_folder "..", "/bahmni", :owner => "vagrant"
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", 4092, "--cpus", 2, "--name", "Bahmni-RPM"]
end
end
Finally! This is years later but I couldn't find more current info. For me, the problem was that I not only had a private network defined, but also a forwarded port, and all that was working well. I then commented out the private_network, replaced it with public_network, and couldn't reach anything. Tried everything I found here and elsewhere, no go. It's only when I commented out the port forwarding that things started working again, without any of the manual bridging/dhcp configuration rigamarole suggested.
Single VM environment
you can use command: vagrant ssh
Multi VM environment
you can use command: vagrant ssh {hostname}

Resources