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.
#!/bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables=`which iptables`
$iptables -F
/sbin/iptables -t nat -A POSTROUTING -o enp2s0 -j MASQUERADE
$iptables -A FORWARD -i enp2s0 -o vboxnet0 -j ACCEPT
$iptables -A FORWARD -i vboxnet0 -o enp2s0 -j ACCEPT
Best Answer
Since you change your network connection often, it makes little sens to use a static IP configuration. You see, when you change configuration in /etc/network/interfaces, you certainly need to specify the network and the gateway, besides mask and your own address (plus, if you wish, broadcast and dns-nameservers). The first three specs (gateway, network, mask) change from LAN to LAN, so it is possible that your chosen IP address belongs to a completely different network: in this case, you would be left incommunicado.
Also, it is possible that your chosen address is already taken, which would cause a number of difficult-to-diagnose malfunctions (just try it once, for pure fun, you'll see it).
As for syntax, a correct, simple configuration is:
to which you may add pre-up/post-up/pre-down/post-down to specify actions to be executed before/after connection/disconnection.
If however you insist on retaining your xxx.xxx.xxx.145 address, you will have to learn how to listen to a network without having a proper IP address, to discover whether a 145 address has been given to someone already.