How to get VirtualBox VMs to use host's DNS?

73

42

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

Posted 2013-09-06T15:48:39.823

Reputation: 1 613

Answers

105

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

Posted 2013-09-06T15:48:39.823

Reputation: 1 662

11For further information for others: this needs to be run from the host. If your host is Windows, open a Command Prompt, cd to "C:\Program Files\Oracle\VirtualBox", and run that command. There doesn't seem to be any option in the VirtualBox Manager GUI (at least in all versions from 4.* to 5.0.20) which lets you set this any other way. – Graham – 2016-06-03T12:55:47.900

I'd like to add that you need to run this command as administrator. I tried it from my cmd and it didn't work for me, but then I ran cmd as administrator and it all worked perfectly fine. – SergeyOvchinnik – 2016-12-19T11:24:43.593

3This used to work for me, but not with Ubuntu 16.04 and the latest virtualbox for Windows – Shanteva – 2017-06-28T12:26:18.153

Not working for me as well :( – Pavel Durov – 2017-12-29T13:24:42.017

1This still works with MacOs High Sierra as the host, VirtualBox 5.2.6, running Windows 7 in the VM. – John Eikenberry – 2018-01-25T20:12:52.293

In my setting (host Windows 10, guest Ubuntu 16.04), this works with VirtualBox 5.2.8 but not with VirtualBox 5.2.14. – mkorvas – 2018-07-10T11:42:41.707

Works great for me with VirtualBox 5.2.22, OS X 10.14 host, Windows guests. – Ryan LaBarre – 2019-01-08T00:10:36.863

This is especially useful when using docker-machine on windows hosts! – Tamas Hegedus – 2019-08-13T13:20:11.273

1I get the following errors: VBoxManage.exe: error: The machine 'DebianServer64' is already locked for a session (or being unlocked) VBoxManage.exe: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component MachineWrap, interface IMachine, callee IUnknown VBoxManage.exe: error: Context: "LockMachine(a->session, LockType_Write)" at line 531 of file VBoxManageModifyVM.cpp – Ryan Augustine – 2019-11-18T12:09:15.127

1@RyanAugustine Chances are you need to shut down the Guest vm before this will work – evanr – 2020-01-01T00:39:45.733

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

Posted 2013-09-06T15:48:39.823

Reputation: 841

5

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

pretzel11

Posted 2013-09-06T15:48:39.823

Reputation: 51

2Welcome to Super User! You could propose that as an edit to Josiah's answer, which would help those looking :) – bertieb – 2018-04-02T17:48:25.417

Thanks for the tip! I tried adding just a comment, but it wasn't allowed. I submitted the edit instead. – pretzel11 – 2018-04-02T17:57:49.757

1

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

Valdis Vitayaudom

Posted 2013-09-06T15:48:39.823

Reputation: 11

0

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

[screenshot1]

Michel Samia

Posted 2013-09-06T15:48:39.823

Reputation: 121