UPDATE
In any case, should anyone stumble upon this post. The solution below does, in fact work, vote downs for this question without any feedback / constructive criticism or error identification seem unjustified.
Goal
I need the best way to setup ubuntu server vbox6.1 guest to have internet access and enable ubuntu 20.04 desktop host to ssh into the box.
Progress / Research
While it is a very common solution to use 2 network adapters combinations with virtualbox servers to establish host-guest communications & internet access for the virtualbox, these common methods have certain caveats.
bridged adapter & NAT adapter combo method - by far the easiest & fastest method, but it comes with the caveat that if you use a killswitch with your UFW firewall / VPN, then very often you have to adjust firewall settings based on which wi-fi network you are connected to because gateway addresses change (common problem for those using static ip addresses on vbox). If you are very, good with firewall rules, then this method is for you.
NAT adapter & host-only adapter combo method - The nat adapter provides easy internet access inside the box and the host-only adapter provides host guest communications for apps like apache. The caveat here is that some applications configure themselves based on the primary adapter ip address upon installation (this is my current problem) and as NAT ip address can't communicate with the host, this creates a problem.
The solution that I am working on, which perhaps is a little hard to set-up initially, is configuring a host-only adapter to have internet access via the host's primary network adapter.
MEANING
ONE vboxnet0 host-only adapter to provide ssh, host-guest communication, and outside internet access for as many vboxes that you want with either static or dynamic ips... no more vbox connectivity headaches.
Here's the Pro's & Cons along with my progress steps.
Step1
Create a host-only adapter for your machine (vboxnet0)
Mine has the IP address of 192.168.51.1
PRO:
you have the option to enable DHCP server if you plan to have multiple vboxes on the same host-only adapter
Step 2:
Create a static ip config for your vbox (mine is Ubuntu 20.04 server). You can optionally skip this step and just use a dynamic ip
I am using
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
addresses: ['192.168.51.47/24']
gateway4: 192.168.51.1
nameservers:
addresses: [127.0.0.1, 192.168.51.1, 8.8.8.8]
Pro
With a static ip on your machine you can SSH into the box, without the use of a second NAT adapter.
Step Three
Create Easy, Minimal UFW firewall rule for host-guest communications
This is all that is required to configure a host-only adapter to work with UFW:
ufw allow allow in|out on vboxnet0
Pro
- Using this method, you can instantly create host-guest communications for multiple vboxes using a single firewall rule as long as each box is on vboxnet0 (great if you plan to make many vboxes that need host-guest communication)
Step Four
Create permanent internet access for vboxnet0 by sharing host machine's primary network adapter using MASQUERADE
This is my current PROBLEM. I can only create temporary internet access for vboxnet0 because I am just not familiar enough with ip tables and MASQUERADE.
As a temporary solution I am using these 4 commands inside of a bash script:
sudo iptables -A FORWARD -o wg0 -i vboxnet0 -s 192.168.51.0/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -A POSTROUTING -o wg0 -j MASQUERADE
Where wg0 is my host machine's primary network adapter.
At this point you should be able to ping 8.8.8.8 from inside the vbox to be sure it has internet access.
I got the above commands from:
https://help.ubuntu.com/community/Internet/ConnectionSharing
Can someone finish / polish the host-only adapter method of creating internet access for one or multiple vboxes?
Seems like this could help lots of ubunutu users. With this method your vboxes are "network portable" and have internet access with minimal effort after initial configuration.
The only cons here are: Difficult initial set-up & vboxes are not accessible to other machines on your LAN, which is negligible if you are just doing local web development.