Score:-4

Best Way To Configure Ubuntu 20.04 Dekstop Host & Ubuntu 20.04 Server Guest To Have Internet Access, SSH, & Host to Guest Communication

tr flag

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.

in flag
Looks like you’re trying too hard. With VirtualBox, all you should need to do is set the network interface (in VirtualBox settings) to “Bridged”, click the Advanced checkbox, then set “Promiscuous Mode” to “Allow All”. This is all I do with my Ubuntu Server VMs, and they’re fully connected and appear properly on the network for the host and any other machine that might need access
nightwatch avatar
tr flag
You are correct.. I was trying way too hard... I figured it... will post a solution in a bit.
nightwatch avatar
tr flag
@matigo I updated my post... my solution is "almost" good... I just cannot ssh over NAT and there is no apparent reason.
in flag
The new question really should be a *new* question ...
nightwatch avatar
tr flag
fair enough.. I will edit... and make a new question... I just it as a continuation of my original goal...
nightwatch avatar
tr flag
@matigo are you any good with IP tables and ip masquerade?
in flag
I have never needed to mess around with IP tables or masquerade when using VirtualBox VMs, and I've been developing this way for ... (counts fingers) ... at least a decade. Perhaps I should just write a step-by-step for how I configure my VirtualBox machines, which are quite portable as I tend to ship them off to the NAS when they're not being used and import them on various machines (Ubuntu & MacOS) when required ...
nightwatch avatar
tr flag
A clear step-by-step would be great for folks new to local vbox web development. However, I have gotten my host-only adapter permanent internet access by adding my step 4 commands to /etc/rc-local. So, fundamentally the problem is solved. I just don't like hacks. In any case, I can ssh, create static / dynamic ip addresses for multiple boxes, instantly have host-guest communication, and do my work on any wi-fi network without adjusting the firewall, by simply using vboxnet0 as the adapter. I will post my /etc/rc.local config a little later.
nightwatch avatar
tr flag
@matigo I cracked step 4 earlier today... working one last piece of the puzzle and I will publish a full-blown step-by-step... to a very awesome dev environment using a single host only adapter.. Step 4 was a simple one liner in a config file..
nightwatch avatar
tr flag
@matigo see any problems / issues with my answer below?
Score:2
tr flag

This solution is meant to make the life of ubuntu developers a lot more convenient & secure. I hope an admin and/or others will see fit vote my original question back up as it is a pretty common question for folks who want to use Ubuntu for web development.

In any case, I recently had the software requirement for a host-only adapter for virtualbox with internet access. Meaning the widely used dual-adapter method wouldn't suffice. While solving this problem, I essentially stumbled upon a super secure, super convenient, and super efficient ubuntu/virtualbox dev environment. I am posting all of my steps below to help other web developers who are seeking a more efficient web dev environment.

First off, a quick overview:

Bridged adapter & NAT adapter Combo Method

Pros:

  • Easy & quick to set up, if you are proficient with UFW

Cons:

  • requires 2 network interfaces to be configure inside guest
  • guest with static ip addresses might have issues changing wi-fi networks due to gateway values
  • requires more advanced configuration of firewall
  • encourages use of /etc/hosts to resolve local domain names (gets cumbersome over time)

NAT adapter & host-only adapter

Pros:

  • Easy to set-up
  • Minimal firewall configuration

Cons:

  • requires 2 network interfaces to be configured inside guest
  • some software packages utilize default ip on installation, so default 10.X.X.X addresses not suitable
    (in my situation creating a custom NAT address through virtualbox preferences wouldn't help, as my software only installs on the default NAT address and NAT address isn't routeable, accessible to the host)
  • requires more advanced configuration of firewall
  • encourages use of /etc/hosts to resolve local domain names (gets cumbersome over time)

The Most Efficient Solution:
Single host-only adapter with Internet access.

host-only adapter w. internet access method

Pros:

  • single adapter for all guests
  • simplified firewall config
  • simplified networking of guests
  • guest behave more like prod servers that usually have a single network interface "eth0"
  • complete elimination of /etc/hosts use

Cons:

  • more difficult initial configuration.

With the above understood, lets get started:

I will assume:

  1. you have the latest version of virtualbox installed
  2. you have a UFW firewall with VPN KillSwitch already configured. (Pay attention to the IP Masquerade section, we will re-visit it.)
  3. you have have already created a guest server in virtualbox & added an ssh pub key for your user
  4. A web project ready for web access inside your guest with a domain ending with ".test" (i.e. www.example.test)

Step 1

In Virtualbox > Files > Host Network Manager create a host-only adapter for your machine (i.e. vboxnet0) For this guide mine will have the IP address of 192.168.51.1

Step 2

In Virtualbox > Settings > Network, click "Enable Adapter" for Adapter 1 and for "Attached to" select the vboxnet0 name that you created in "Step 1"

Step 3

Create a static ip config for your guest server (mine is Ubuntu 20.04 server). This is necessary for ssh and domain name resolution, but you can use dynamic ip if you don't need ssh/dns.

For Ubuntu Server, I am using:

network:
  version: 2
  renderer: networkd
  ethernets:
        enp0s3:
            addresses: ['192.168.51.47/24']
            gateway4: 192.168.51.1  #**NOTE** Your vboxnet0 ip address is the gateway for your static ip address.
            nameservers:
                addresses: [127.0.0.1, 192.168.51.1, 8.8.8.8]

Remember
Run #netplan apply and/or #netplan --debug generate to find your typos & errors.

Reboot your guest to be sure settings take affect.

Step Four

Create easy, minimal UFW firewall rule enable host-guest communications. This is all that is required to configure a host-only adapter to work with UFW:

$sudo ufw allow in on vboxnet0 && sudo ufw allow out on vboxnet0

At this point you should ping your guest's static ip from the host and ssh in into host.

Test with: ssh -p 22 -i ~/.ssh/id_rsa root@your-guest's-static-ip address

ping -c 5 your-guest's-static-ip address

Step Five

Create permanent internet access for vboxnet0 by sharing host machine's primary network adapter using MASQUERADE. When you configured your vpn killswitch you had to use ip masquerading for your vpn's address by editing /etc/ufw/before.rules, we will edit this file again.

sudo nano /etc/ufw/before.rules

In this section of the file:

#NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]

Add:

-A POSTROUTING -s 192.168.51.1/24 -j MASQUERADE #NOTEthis is the ip address of your vboxnet0 host-only adapter, which acts as the gateway for your guest's static ip address.

Close & Save the file

Now all traffic 192.168.51.1 will be sent through the host's primary network interface even if it changes (i.e. wi-fi vs lan)

You can test this by accessing your vbox guest and on the command line, running:
ping -c 5 8.8.8.8

If successful, your host-only adpater now has internet access.

Step 6

This is the final step and by far the most most complex. At this point, the only thing that needs to be done for your virtualbox web dev environment to be ready for use is to install and configure dnsmasq so that domain names from your guest server can be resolved locally.Sidebar dnsmasq can obviously be used with the combo network adapter methods mentioned above as to eliminte use of /etc/hosts, but when using a single host-only adapter, dnsmasq is required to resolve local domain names.

  1. Disable & stop systemd resolved

    $ sudo systemctl disable systemd-resolved
    $ sudo systemctl stop systemd-resolved

  2. Remove symlink on /etc/resolv.conf & remove the file

    $ ls -lh /etc/resolv.conf
    $ sudo rm /etc/resolv.conf

  3. Create a new /etc/resolv.con with the following values:

    $ sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolv.conf' #(host machine resolution of doman names)

    $ sudo bash -c 'echo "nameserver 208.67.222.222" >> /etc/resolv.conf' #(OpenDNS public dns server ip for outside internet)

    $ sudo bash -c 'echo "nameserver your-guest's-static-ip address" >> /etc/resolv.conf'

  4. Install dnsmasq

    $ sudo apt install dnsmasq

  5. Add .test to the dnsmasq config file:

    $ sudo bash -c 'echo "address=/.test/your-guest's-static-ip address" >> /etc/dnsmasq.conf'

NOTE: Folks developing wordpress multisites that need wildcard domain resolution can use:

$ sudo bash -c 'echo "address=/example.test/your-guest's-static-ip address" >> /etc/dnsmasq.conf'
  1. Create a directory resolver for the guest's static ip address:

    $sudo mkdir -v /etc/resolver && sudo bash -c 'echo "nameserver your-guest's-static-ip address" > /etc/resolver/test'

  2. Restart dnsmasq and network-manager

    $ sudo systemctl restart dnsmasq $ sudo systemctl restart network-manager

  3. Test your dnsmasq set-up

a.) open your browser and confirm that you still have outside internet access

b.) run dig example.test you should be able to see the static ip address of your guest server

c.) in your host machine's browser open example.test

BANG! Done.

With this web dev environment you can change wi-fi newtworks at will without ufw/guest ip address issues, you have only one host-only adapter that you use on all future guests you create, more easily network guests together, and completely stop using the /etc/hosts file to resolve local domain names for your projects, since it is done automatically by dnsmasq.

My sources:
https://unix.stackexchange.com/questions/304050/how-to-avoid-conflicts-between-dnsmasq-and-systemd-resolved
https://brunopaz.dev/blog/setup-a-local-dns-server-for-your-projects-on-linux-with-dnsmasq/
https://www.stevenrombauts.be/2018/01/use-dnsmasq-instead-of-etc-hosts/#configure-dnsmasq
https://linuxize.com/post/how-to-setup-a-firewall-with-ufw-on-ubuntu-20-04/
https://superuser.com/questions/1237463/virtualbox-host-only-with-internet
https://www.stevenrombauts.be/2018/01/use-dnsmasq-instead-of-etc-hosts/#configure-dnsmasq https://medium.com/soulweb-academy/docker-local-dev-stack-with-traefik-https-dnsmasq-locally-trusted-certificate-for-ubuntu-20-04-5f036c9af83d

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.