Connecting my vagrant and another VM in VirtualBox - vagrant

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.

Related

Vagrantbox with Hyper-V not starting

I'm trying to setup a vagrantbox with Hyper-V on my local windows-10 mashine. My Workstation is running behind a proxy, but I configured a local cntlm proxy to get around these limitations. Proxy settings work fine since I was able to install a vagrant plugin and downloan a box image.
But now my guest linux does not start up and I am running out of ideas.
My vagrant file
Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-18.04" # ubuntu 18.04 image with support for virtual box and Hyper-V
config.vm.hostname = "skywalker"
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
config.vm.provider "hyperv" do |hv|
hv.memory = "2048"
end
config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'" # prevent tty errors
# install the vagrant plugin "vagrant-cachier" to cache downloaded artifacts
if Vagrant.has_plugin?("vagrant-cachier")
config.cache.scope = :box
end
# vagrant behing local cntlm proxy if plugin exists (= provinzial win10 workstation)
if !Vagrant.has_plugin?("vagrant-proxyconf")
config.proxy.http = "http://localhost:3128/"
config.proxy.https = "http://localhost:3128/"
config.proxy.no_proxy = "localhost, 127.0.0.1"
end
# --------------------------------------------------------------------------
# provision virtual mashine (basic setup) and install applications in VM
#config.vm.provision "shell", path: "scripts/install-ansible.sh"
config.vm.provision "shell", path: "scripts/install-ncdu.sh"
config.vm.provision "shell", path: "scripts/install-git.sh"
config.vm.provision "shell", path: "scripts/install-openjdk-11.sh"
config.vm.provision "shell", path: "scripts/install-maven.sh"
config.vm.provision "shell", path: "scripts/install-node-npm.sh"
config.vm.provision "shell", path: "scripts/install-docker.sh"
config.vm.provision "shell", path: "scripts/install-docker-compose.sh"
# npm webserver
config.vm.provision "shell", path: "apps/install-npm-apps.sh"
config.vm.network "forwarded_port", guest: 8000, host: 8000
# artifactory setup (start artifactory after vm startup)
config.vm.network "forwarded_port", guest: 8081, host: 8081 # artifactory from docker
config.vm.network "forwarded_port", guest: 8082, host: 8082 # artifactory from docker
# See README.md for Artifactory in Docker
end
Starting the box with this settings results in
C:\home\work\workspace\vagrant-boxes\skywalker (master -> origin)
λ vagrant up
Bringing machine 'default' up with 'hyperv' provider...
==> default: Verifying Hyper-V is enabled...
==> default: Verifying Hyper-V is accessible...
==> default: Importing a Hyper-V instance
default: Creating and registering the VM...
default: Successfully imported VM
default: Configuring the VM...
==> default: Starting the machine...
==> default: Waiting for the machine to report its IP address...
default: Timeout: 120 seconds
default: IP: fe80::215:5dff:fe02:8b01
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: fe80::215:5dff:fe02:8b01:22
default: SSH username: vagrant
default: SSH auth method: private key
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.
If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.
If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.
If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.
On a different mashine with VirtualBox as VM provider this Vagrantfile works fine. Sadly I cannot use any VM provider other than Hyper-V on my windows mashine due to limitations which are out of my control ...
I set up Hyper-V using Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All.
Any ideas? Thanks in advance and best regards. Sebastian
I went through this exercise last week and saw similar problems. I made lot of progress following these steps and the vm's get spun up at a reasonable time under windows 10 hyper-v. This is what i did.
Followed this blog and tried creating a vm successfully first.
https://win32.io/posts/Vagrant-Install-HyperV
Then i created a simple vagrantfile and got it to work successfully. Here is mine ,
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/bionic64"
config.vm.hostname = 'utility-server-1'
config.vm.network 'public_network', bridge: 'Internet'
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.provider "hyperv" do |vb|
vb.memory = "4024"
vb.cpus = 2
vb.vmname = 'utility-server-1'
end
end
Once this works, add more complex steps from your vagrantfile, like shell scripts and forwarded ports.

vagrant private network fails to resolve on windows 10 host

I can't get anything to work. I have windows 10 as my host and my guest VM is centos/7 box. It works great with forwarded_ports but using private_network setting nothing resolves. I'm not sure what to do or check
vagrant file look like
Vagrant.configure("2") do |config|
config.vm.box = "bento/centos-7.4"
config.vm.synced_folder "./website", "/var/www/html/", :mount_options => ["dmode=777", "fmode=777"], owner: "apache", group: "apache"
config.vm.network "private_network", ip: "192.168.50.4"
## Bootstrap script to provision box. All installation methods can go here.
config.vm.provision "shell" do |s|
s.path = "bootstrap.sh"
end
end
My ifconfig on guest looks like
My virtualbox shows two adapters
I get nothing when I browse to the IP on windows
It actually had nothing to do with vagrant. It was all set up correctly, but my magento site had a weird error. Thanks to Frederic for taking the time!

Vagrant accessing domain from another guest machine

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.

vagrant Vagrantfile mount point ignored

I have a Vagrantfile as below running under libvirt. When the box boots, the project directory is mounted under "/vagrant" not "/path/to/source". It works fine on another machine under VirtualBox.
Any ideas? Symlinking /vagrant to the actual place I want the mount is hacky.
vagrant up --debug shows that it isn't even attempting it. The port forwarding works OK though.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "mybox"
file = File.open("#{Dir.home}/.mybox_key", "rb")
key = file.read
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.synced_folder "./", "/path/to/source/", type: "nfs"
end
If you're running an older version of Vagrant that doesn't support this option (e.g. 1.3.5), it may fail silently instead of trying to setup the NFS share. Ensure you're running at least Vagrant 1.5.1.

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