How can I access a vagrant guest from another virtualbox guest?
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 like192.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 isLinux Mint
Vagrant box is onUbuntu Guest 1
. OpenC:\Windows\System32\drivers\etc\hosts
file as admin and do the above IP's entry in there like192.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 it192.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
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
.