97

I use VirtualBox for my VMs. My office network setup is wireless, i.e. I connect to my company's WiFi network, which has a local DNS to resolve local names (such as something.mycompany.com going to 123.45.67.89). When I build a new VM, it can connect to the outside internet inheriting the host's connection but it doesn't resolve local names using the local DNS. So I have to go into /etc/hosts on the VM and manually make an entry after I resolve the name on the host, which is annoying.

Is there a way to have VirtualBox automatically connect the guest to the host's DNS so that I do not have to do this manual step? My host is OSX Mountain Lion, the VMs are typically Ubuntu but I doubt that should matter.

amphibient
  • 2,243
  • 10
  • 33
  • 44

5 Answers5

131

To enable DNS Proxy Mode using the host's resolver, run the following command:

VBoxManage modifyvm "<VM name>" --natdnshostresolver1 on

As a result, guest OS DNS requests will be intercepted and resolved using host DNS API, rather than having guest OS connect to external DNS servers as with --natdnsproxy1 on.

You can get the name of the VM by running VBoxManage list runningvms.

Josiah
  • 1,952
8

For me it helped to add another adapter with host-only adapter while keeping the first (NAT mode) there as well.

[screenshot1]

7

This was a top Google result, so I wanted to clarify for others. Josiah's solution worked for me with the addition of adding the line:

hosts: files dns to /etc/nsswitch.conf

As others pointed out, the original solution does not work above Ubuntu 16.04. My guest VM is Ubuntu 16.04.

Reference Creating Linux Server

6

Yes it is possible. There are many modes available in VirtualBox to establish networking between the guest and the host. Rather than using the NAT mode (which is default), you can use the bridge-mode in which your guest machine can be treated as entirely separate entity on your network. So, not only your host, but any other machine (such as your DNS server) will see your guest as a separate machine.

Once you setup the bridge-mode, just go to your ubuntu guest and get it to use your company's DNS server name or ip. Read this tutorial for more info: http://prahladyeri.wordpress.com/2012/08/02/how-to-setup-a-virtual-lan-on-your-machine-using-oracle-virtualbox/

Prahlad Yeri
  • 1,015
3

I've also noticed my VPN connection interferes with vagrant internet connection.

Thanks to this stackexchange, I was able to resolve by embedding into my Vagrantfile

config.vm.provider "virtualbox" do |vb|
  vb.cpus = 1
  vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
  vb.memory = "2048"
end

And avoided the tedium of having to shutdown box to modify via command line, then reprovision

export PATH=$PATH:/c/Program\ Files/Oracle/VirtualBox/
VBoxManage list runningvms
export vbox=`VBoxManage list runningvms | cut -d '"' -f 2`
VBoxManage controlvm $vbox poweroff
VBoxManage modifyvm $vbox --natdnshostresolver1 on
vagrant.exe up --provision