Linux: Enable Virtualbox Host / Guest communication through Host-only-network with Guest access to internet

When setting up a Virtualbox development environment (for example a readily-accessible HTTP server), the default NAT networking works fine to allow Guest to access the Internet through Host. However, if Guest will be an HTTP server with a static IP, it is inaccessible to Host. If Guest is set up with Host-only networking, Host / Guest communications work fine, but Guest has no access to the Internet (which makes it very difficult to install or update software.)

This article shows how to set up Host-only networking so that Host and Guest can talk to each other while allowing Guest to access the Internet. Host is Manjaro / Arch Linux, and Guest is Ubuntu. Assuming you have set up vboxnet0 as a Host-only Network using the Virtualbox Host Network Manager with the Adapter configured manually at IP 192.168.56.1 and DHCP turned off.

1) determine Host's Internet network. In the example, it is wlo1

2) determine Host / Guest network IP. In the example, it is 192.168.56.0/24

3) on Host, use iptables to forward packets between Guest and Internet:

iptables -A FORWARD -o wlo1 -i vboxnet0 -s 192.168.56.0/24 -m conntrack --ctstate NEW -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A POSTROUTING -t nat -j MASQUERADE

4) on non-Manjaro Linux, you may need to enable and start dnsmasq (see reference).

5) add a nameserver to Guest's resolve.conf. Add the following line to Guest's /etc/resolv.conf:

nameserver 1.1.1.1

6> tell Host sysctl to forward packets. On Host, add the following lines to /etc/sysctl.conf

net.ipv4.ip_forward = 1
net.ipv4.conf.all.proxy_arp = 1

7) reload Host sysctl. On Host, type sysctl -p

Reference: https://jackal777.wordpress.com/2012/02/13/internet-access-in-virtualbox...

9 / 2022