Fix Vagrant/VirtualBox DNS Resolution: Persistent 10.0.2.3 Issues and Workarounds


3 views

When working with Vagrant and VirtualBox, the default DNS configuration using 10.0.2.3 often fails to resolve external domains. This becomes particularly problematic during provisioning when tools like Chef or Puppet need internet access to download packages.

First, check your current DNS configuration inside the VM:

cat /etc/resolv.conf
# Typical output showing the problematic DNS:
# nameserver 10.0.2.3

Test DNS resolution with:

ping us.archive.ubuntu.com
# If this fails, you've confirmed the DNS issue

The 10.0.2.3 address is VirtualBox's built-in DNS proxy, which should forward requests to your host's DNS servers. Common failure reasons include:

  • Host DNS misconfiguration
  • Corporate network restrictions
  • VirtualBox NAT interface issues

The most robust solution is to configure DNS directly in your Vagrantfile. Add this configuration block:

Vagrant.configure("2") do |config|
  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
    vb.customize ["modifyvm", :id, "--natdnsproxy1", "off"]
  end
  
  # For Linux guests, prevent resolv.conf overwrites
  config.vm.provision "shell",
    inline: "echo 'prepend domain-name-servers 8.8.8.8;' >> /etc/dhcp/dhclient.conf"
end

If using Ubuntu cloud images or similar, leverage cloud-init:

# In Vagrantfile
config.vm.provision "cloud-init" do |ci|
  ci.write_files = [
    {
      path: "/etc/cloud/cloud.cfg.d/99-custom-dns.cfg",
      content: "network:\n  nameservers:\n    addresses: [8.8.8.8, 8.8.4.4]"
    }
  ]
end

If problems persist, try these diagnostic commands:

# Check DNS resolution
nslookup google.com

# Verify network connectivity
ip addr show
route -n

# Test without DNS caching
systemctl stop systemd-resolved
dig @8.8.8.8 google.com

Windows systems might need additional configuration:

# In PowerShell (admin):
Set-NetAdapter -Name "VirtualBox Host-Only Network" -MacAddress "080027XXXXXX"

# Then in Vagrantfile:
config.vm.provider "virtualbox" do |vb|
  vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
end

When working with Vagrant and VirtualBox on Linux Mint (or similar Ubuntu-based systems), you might encounter DNS resolution failures with the default 10.0.2.3 nameserver. This prevents crucial provisioning operations through Chef/Puppet and package manager updates.

The VirtualBox NAT interface provides 10.0.2.3 as a DNS proxy, but it often has connectivity issues with external repositories. Testing with:

vagrant@precise64:~$ ping us.archive.ubuntu.com
ping: unknown host us.archive.ubuntu.com

While manually editing /etc/resolv.conf to use 8.8.8.8 works temporarily:

sudo nano /etc/resolv.conf
# Change to:
nameserver 8.8.8.8

This change won't persist through reboots due to network manager overwrites.

Option 1: Modify Vagrantfile

Add this to your Vagrantfile before provisioning:

config.vm.provision "shell", inline: <<-SHELL
  echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf > /dev/null
  echo "nameserver 8.8.4.4" | sudo tee -a /etc/resolv.conf > /dev/null
SHELL

Option 2: Disable DNS Overwrite (Ubuntu)

sudo nano /etc/NetworkManager/NetworkManager.conf
# Add under [main]:
dns=none

# Then:
sudo systemctl restart NetworkManager
sudo nano /etc/resolv.conf
# Set your preferred nameservers

Option 3: Use Vagrant Plugin

For more control, install the vagrant-dns plugin:

vagrant plugin install vagrant-dns
vagrant dns --install

After making changes, verify with:

vagrant ssh -c "dig google.com +short"

Should return IP addresses without errors.

If problems persist, check these fundamentals:

vagrant ssh -c "ip route show"
vagrant ssh -c "cat /etc/resolv.conf"
vagrant ssh -c "ping -c 4 8.8.8.8"