1

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?

6
  • What did you try so far for yourself? I have a similair issue: stackoverflow.com/questions/43202371/… If you point out what you have tried so far I could check if I am able to help. Commented Apr 4, 2017 at 9:36
  • I'm just trying to modify guest machine's network settings manually on virtualbox by now. VirtualBox provides several network configurations: NAT, NAT network, bridge, internal network, host-only. I don't quite figure out which one should I choose... Commented Apr 4, 2017 at 10:17
  • For me NAT does not work out of the box. Bridge did work without any further configuration. Did you test any of the options? Let me know if bridge does work. Commented Apr 4, 2017 at 10:27
  • Bridge works. Nevertheless, I'm able to ping from host to guest but I'm not able to creach the host ip from guest... Commented Apr 4, 2017 at 10:55
  • Check your firewall and/or other programs first. Commented Apr 4, 2017 at 11:06

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to push network management to vagrant as well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.