VirtualBox DHCP nameserver for NAT network


VirtualBox LogoVirtualBox is a great environment for testing networking tools such as opDNS. However, I did not get the VirtualBox DHCP server for NAT network to provide nameserver information, even though the client asks for it. Here’s a simple solution to this problem.Having updated VirtualBox to 4.3.24, I tried to set up a NAT network, where all guests attached to it and the host can communicate freely as well as the guests being able to access the Internet via NAT – the perfect setup for network tests. However, My OSX Yosemite 10.10.2-based Mac did not provide the address of the local DNS name server to the guest virtual machines.

As a result, /etc/resolv.conf remained empty (except for the “automatically generated — do not change”-style comments).

Even attempts at fiddling with the VirtualBox DNS proxy and resolver did not succeed.

First attempt

While the Linux guest machines (in this case, Ubuntu 14.04 LTS) could have their /etc/network/interfaces file changed to include

[...]
auto eth0
iface eth0 inet dhcp
dns-nameservers 192.168.1.1 # Change IP address as needed

this would not work well with my laptop, which is associated to different networks over time. (Yes, I know that a public DNS server could be entered there, but then local network addresses would no longer resolve and it might change the results of my DNS-related experiments.)

NAT network vs. NAT

An important observation is that switching the guest interface adapter from “NAT network” to plain “NAT” results in the nameserver being communicated fine, host names could be resolved.

While in both cases, the guest’s DHCP client (dhclient in Ubuntu) asks for DNS-related information (domain name server, domain name, and domain search list; DHCP options 6, 15, and 119, respectively), only in the plain NAT case does VirtualBox’ DHCP server provide any results (domain name server and domain name). This can easily be observed with Wireshark listening on port bootpc.

When domain name server and domain name information is received the DHCP client, it passes these through the resolvconf framework, which write the information into /etc/resolv.conf, from where the applications pick it up, typically through the resolver library or anything calling it, such as gethostbyname().

Preferred solution

My favorite solution is to

  1. keep the first virtual adapter set to “NAT network” and
  2. add a second adapter set to plain “NAT”.

The first adapter will provide the desired connectivity (among the guests and with the host, plus the default route) while the second one will provide the automatic nameserver configuration, as described above. The second interface will not normally be used for traffic, not even to the nameserver, but this is exactly what we wanted.

  1. In the guest, edit /etc/network/interfaces as follows:
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

auto eth1            # New
iface eth1 inet dhcp # New
  1. Save the file and stop the guest.
  2. In the VirtualBox application, you just add the second adapter and mark it as “NAT”.
  3. Restart the guest.
  4. Enjoy!
,

Let’s stay in touch!

Receive a mail whenever I publish a new post.

About 1-2 Mails per month, no Spam.

Follow me on the Fediverse

Web apps


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.