Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 980
Adrian Garner Avatar asked Feb 09 '15 01:02

Adrian Garner


People also ask

Can vagrant work without VirtualBox?

Getting Started With Vagrant Before you start, make sure you already have a virtualization solution on your system. Solutions that work with Vagrant include VirtualBox, VMware, Docker, Hyper-V, and custom solutions.

What is the difference between VirtualBox and vagrant?

VirtualBox is basically inception for your computer. You can use VirtualBox to run entire sandboxed operating systems within your own computer. Vagrant is software that is used to manage a development environment.

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 .


2 Answers

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.

like image 67
Terry Wang Avatar answered Oct 06 '22 19:10

Terry Wang


In case you don't want to go with public_network just like me then you should do the steps below using private_network:

  1. Open Vagrantfile from your project root
  2. Search for config.vm.network
  3. 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.
  4. Reload your vagrant using vagrant reload.
  5. 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/.
  6. 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.

like image 24
Imran Zahoor Avatar answered Oct 06 '22 17:10

Imran Zahoor