Resolve Guest Nat IP Address on Host

Discussions related to using VirtualBox on Linux hosts.
yupthatguy
Posts: 73
Joined: 4. Jul 2015, 07:48

Re: Resolve Guest Nat IP Address on Host

Post by yupthatguy »

Sorry, for the flood of posts... but I think I am near a Vbox solution for the local DNS resolution problem that I currently have...

For my host-only network vboxnet0 to resolve guest domain names I need to enable the DHCP server option, then modify the DHCP server

Pulling directly for Vbox manual pages:

https://www.virtualbox.org/manual/UserM ... ingdetails
dhcpserver modify

VBoxManage dhcpserver modify < --network=netname | --interface=ifname >..................

This modifies an existing DHCP server configuration. It takes the same options as the add command with the addition of the following on scope configuration:

Common DHCP Options:

5 - NameServers

Space separated list of IPv4 name server (IEN 116) addresses.
6 - DomainNameServers

Space separated list of IPv4 DNS addresses.

44 - NetBIOSNameServers

Space separated list of IPv4 NetBIOS name server (NBNS) addresses (RFC1001,RFC1002).

119 - DomainSearch

Domain search list, see RFC3397 and section 4.1.4 in RFC1035 for encoding. Only accessible using --set-opt-hex.
can you guys help me devise the correct syntax?

Thanks
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Resolve Guest Nat IP Address on Host

Post by scottgus1 »

yupthatguy wrote:Is there a host-only version of this command?
I don't think so, though you could search the PDF manual:

https://download.virtualbox.org/virtual ... Manual.pdf

Change Z.Y.X for the Virtualbox version you want to read about, like:

https://download.virtualbox.org/virtual ... Manual.pdf

Host-Only is a basic Ethernet network, an unmanaged switch with an optional DHCP server on it. Personally, I don't know if there is a way to tweak the DHCP server to do what you want. The options you posted seem to be static, pointing to a 3rd-party DNS server (which Virtualbox doesn't supply), resulting the same as if you manually added the domain names to the OSs' hosts files. Maybe setting up a minimal Linux VM with a DNS server on the Host-Only network, which you can point the VMs at, will help your setup. (And no I don't know how to set up DNS in Linux, just in case... :) )
yupthatguy
Posts: 73
Joined: 4. Jul 2015, 07:48

Re: Resolve Guest Nat IP Address on Host

Post by yupthatguy »

I cracked it... Here's a complete guide to configuring an ubuntu 20.04 Desktop Host with a virtualbox ubuntu 20.04 Server guest using a host-only adapter:

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][3] 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. http://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:

Code: Select all

  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:

Code: Select all

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

Code: Select all

`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.

Code: Select all

`sudo nano /etc/ufw/before.rules`
In this section of the file:

Code: Select all

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

Code: Select all

`-A POSTROUTING -s 192.168.51.1/24 -j MASQUERADE`
#**NOTE**this 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 guest and running:

Code: Select all

`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.

On ubuntu 20.04 there's a [conflict between dnsmasq and the default resolvconf installation][2]. To avoid this conflict, first edit:

Code: Select all

`/etc/systemd/resolved.conf`  
and change this line:

Code: Select all

    DNSStubListener=no
Close & Save the file

After that, `sudo systemctl restart resolvconf && sudo apt -y install dnsmasq`

After installation check that dsnmasq is running properly
`sudo systemctl status dnsmasq` (there shouldn't be any port 53 conflict problems)

Then configure dnsmasq as follows:

1. Tell ubuntu host to use dnsmasq as the default dns server
a.)

Code: Select all

`sudo nano /etc/NetworkManager/conf.d/dnsmasq.conf`
& add:

Code: Select all

    [main]
    dns=dnsmasq 
Close & Save the file

b.)

Code: Select all

 `sudo systemctl restart NetworkManager`
2. Tell dnsmasq to resolve the domains related your guest's static ip address.
**NOTE:** this will tell dnsmasq to resolve all domains ending with ".test" locally, feel free to change ".test to whatever you like

a.)

Code: Select all

`sudo nano /etc/dnsmasq.d/development.conf`
* add:

Code: Select all

nameserver your-guest's-static-ip-address-here
Close & Save the file

b.) `sudo systemctl restart NetworkManager`

3. Test your dnsmasq set-up

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

b.) run

Code: Select all

`dig example.test`
you should 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 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/question ... d-resolved
https://brunopaz.dev/blog/setup-a-local ... h-dnsmasq/
https://www.stevenrombauts.be/2018/01/u ... re-dnsmasq
https://linuxize.com/post/how-to-setup- ... ntu-20-04/
https://superuser.com/questions/1237463 ... h-internet
https://www.stevenrombauts.be/2018/01/u ... re-dnsmasq
Post Reply