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.
- Default NAT, enables internet access;
- HostOnly network, enables communication host-to-guest and guest-to-guest.
Steps:
- Create hostonly network with DHCP (Virtualbox comes with a default one, I customized it)
- add network adapters to the guest
- Start the VM, check with
ip a and ip r:
- 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.