Jordi
Jordi

Reputation: 23277

VirtualBox networking using vagrant

I need to configure a vm into using vagrant and virtualbox as provider. The issue is related to how to allow network comunication between the virtual machine and the host machine.

Concretly, I need:

  1. Each developer is using a log viewer in his host machine in order to be able see the log messages generated into vm applications. So I need to send log messages to an network address(host) from guest.
  2. Each developer machine has its own ip address, so I'm not able to reference to a single ip since each developer has its host ip address.

Any ideas?

Upvotes: 1

Views: 1543

Answers (1)

user14168816
user14168816

Reputation:

This is how I've set up my VMs lately, hopefully it points you in the right direction.

My requirements were:

  • enable internet access on a single guest basis
  • enable access guest to guest
  • enable access host to guest
  • avoid IP changes when changing physical network (lan home, lan work, random wlans)
  • avoid IP clash with various clients VPNs
  • avoid showing machines on the physical network

To do this use 2 network interfaces on all guests.

  1. Default NAT, enables internet access;
  2. HostOnly network, enables communication host-to-guest and guest-to-guest.

Steps:

  1. Create hostonly network with DHCP (Virtualbox comes with a default one, I customized it)
  1. add network adapters to the guest
  1. Start the VM, check with ip a and ip r:
  1. Create Vagrantfile to provision VM configured like above (I'm using bento/ubuntu-20.04).
  • NAT is on by default
  • join the hostonly-network with "modifyvm" (avoid :private_network as it creates a new network that will clash with the one already available), add to Vagrantfile:
config.vm.provider "virtualbox" do |vb|
  vb.customize ["modifyvm", :id, "--nic2", "hostonly"]
  vb.customize ["modifyvm", :id, "--hostonlyadapter2", "vboxnet1"] #use proper network name here
  vb.customize ["modifyvm", :id, "--cableconnected2", "on"]
end
  • vagrant up
  • vagrant ssh
  • ip a will show eth1 as down
  • try sudo ifup eth1, in my case this failed
  • edit /etc/network/interfaces and add this:
allow-hotplug eth1
iface eth1 inet dhcp
  • sudo ifup eth1 should work now and get an ip from the DHCP of the host-only network

Probably these last steps could be added to vagrant shell provisioning but I'm still new to it.
Edit: add this section to Vagrantfile for the steps above:

   config.vm.provision "shell", inline: <<-SHELL
     if ! ifquery eth1 > /dev/null 2>&1; then 
       sudo echo "allow-hotplug eth1" >> /etc/network/interfaces
       sudo echo "iface eth1 inet dhcp" >> /etc/network/interfaces
       sudo ifup eth1
       ip -4 a show dev eth1
     fi
   SHELL
  • At this point the vagrant vm should be accessible, able to access the web, able to access the hostonly network (other guests and the host through the configured ip, 192.168.178.1 in my case), these should all work:
ping 8.8.8.8       #web
ping 192.168.178.1 #host
ping 192.168.178.3 #other guest

Final result

Hopefully this enables all relevant communication for your use case also.

Upvotes: 3

Related Questions