Because I'm working on multiple locations, I experience problems with my development servers. Because work on a variety of networks I do not want to configure the networking settings of my virtual machines every time I change network. Sometimes the network requires me to use DHCP. (public libraries and such)
Also I have to change network adapter often, I prefer ethernet but sometimes I have to work wireless.
So a lot of different networks and even different adapters.
Requirements
- Internet connection
- Access Host machine from Guest machine
- Access Guest Machine from Host machine
I am looking for a configuration so I dont have to change my (virtual machines) network settings. Because I had to changed network (adapter)
Specs
host machine: Ubuntu, Virtualbox
guest machine: debian.
wireless interface: wlp2s0
eth adapter: eth1
Attempt #1
Just use NAT.
- Added a NAT adapter to Virtualbox: (10.0.2.0/24, DHCP enabled)
- Added a NAT adapter to the Guest machine.
- Configure guest interface:
- auto eth1
- allow-hotplug eth1
- iface eth1 inet static
- address 10.0.2.5
- netmask 255.255.255.0
- gateway 10.0.2.2
- network 10.0.2.0
- broadcast 10.0.2.255
With this configuration the following works:
- Internet connection
- Access Host Machine from Guest machine
With this configuration, the following does not work:
- Access Guest Machine from Host Machine
This seems to be a limitation when using NAT.
Attempt #2
NAT in combination with Bridged connection:
- Virtualbox NAT Adapter (10.0.2.0/24, DHCP enabled)
- Virtualbox Bridged Adapter (192.168.56.1/24, DHCP enabled)
Because sometimes I have to switch from wireless to Ethernet I also tried to configure a bridged adapter for both the network adapters on the Host Machine.
Host Machine IP: 192.168.56.101
Guest Machine IP: 192.168.56.100
With this configuration, the following does work:
- Internet connection
- Access Host Machine from Guest machine
With this configuration, the following does not work:
- Access Guest Machine from Host Machine
This example has a problem because for a bridged connection the host machine IP has to be set in the range of the IP range of the bridged adapter. Sometimes I can't because i am required to use DHCP.
Given the details of my attempts, do you guys can advise me a alternative configuration for achieving my goals?
Best Answer
OK, so there are 2 ways of sort of doing this.
1) Use the VirtualBox NAT network on the client. This doesn't provide a direct route from your host machine, but you can set preferences to forward ports. Kind of a pain to set up, but if the host machine was Windows or a Mac this is what I'd probably do, mostly because I'm not sure on firewalling/natting/routing capabilities on those systems (pretty sure Mac wouldn't have an issue, but Windows is unknown to me)
2) Use the VirtualBox Host Only Network. From your description, this may be better. Be aware that you will need to change the firewall script to point to whatever device is connected.... Here's teh details.
On the host machine, first set up the Host Only Network in your vbox preferences. In the Manager, click File and then Preferences, and then select Network. Flip over to Host Only, and then add one if needed. Here's what mine looks like:
Next, on the host, configure the vboxnet0 interface. It automagically just happens for me, setting my host machine to the .1 address in the subnet. Here's output of
ifconfig
on my host, referencing my ethernet and vboxnet0 address.In your guest machine, set the network to some address in your subnet (even with the DHCP server enabled, this makes it easier to get gateway/dns server) and set a nameserver in the usual way (ie, edit
/etc/network/interfaces
and set the static address/netmask/gateway there, then edit/etc/resolv.conf
and set a name server there).At this point, your host machine and your guest machine should be able to communicate back and forth with no issues. You may want to put appropriate entries in
/etc/hosts
so you don't have to enter the IPs everywhere...The last step is to get internet going for the guest. This is a simple matter of turning the host into a router. Since the
vboxnet0
interface doesn't come up until vbox is up and running, you don't want to do this at boot time for the host.A simple firewall script to allow the guest vm to access the world, and the host machine can still access the world and the guest in the vboxonly network. Be aware that this script could be MUCH more secure. As it is, it would accept some client on whatever network the host is connected to using your host machine as a gateway to the guest vm.