Optimizing Vagrant Guest Network Performance: Fixing Slow Internet and DNS Resolution


1 views

The current Vagrantfile shows a mix of networking approaches:

config.vm.share_folder("v-root", "/vagrant", ".", :nfs => true)
config.vm.network :bridged, :bridge => "eth0"
config.vm.network :hostonly, "192.168.20.50"
config.vm.forward_port 80, 8080

This combination of NFS, bridged networking, host-only networking, and port forwarding can create complex routing scenarios that impact performance.

The routing table shows potential conflicts:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.2.2        0.0.0.0         UG    0      0        0 eth0
0.0.0.0         10.0.2.2        0.0.0.0         UG    100    0        0 eth0
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.20.0    0.0.0.0         255.255.255.0   U     0      0        0 eth2

Here's an improved Vagrant configuration that maintains all required functionality while optimizing network performance:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  
  # Use private network for host-only communication
  config.vm.network "private_network", ip: "192.168.20.50"
  
  # For better NFS performance
  config.vm.synced_folder ".", "/vagrant", type: "nfs", nfs_udp: true
  
  # Optional: Public network for direct internet access
  config.vm.network "public_network", bridge: "eth0", use_dhcp: true
  
  # DNS configuration for faster resolution
  config.vm.provision "shell", inline: <<-SHELL
    echo "nameserver 8.8.8.8" | sudo tee /etc/resolvconf/resolv.conf.d/base
    echo "nameserver 8.8.4.4" | sudo tee -a /etc/resolvconf/resolv.conf.d/base
    sudo resolvconf -u
  SHELL
  
  # Other provisioning remains the same
  config.vm.provision "chef_solo" do |chef|
    chef.cookbooks_path = "cookbooks"
    chef.add_recipe "apt"
    chef.add_recipe "base"
    chef.add_recipe "mongodb::default"
    chef.add_recipe "nginx"
  end
end

For further optimization, consider these adjustments:

# In Vagrantfile
config.vm.provider "virtualbox" do |vb|
  vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
  vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
  vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
  vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
end

If issues persist, try these diagnostic commands inside the guest:

# Check active connections
sudo lsof -i

# Test DNS resolution speed
time host example.com
time dig example.com

# Check network throughput
sudo apt install iperf
iperf -c <host-ip>

When inspecting your routing table with route -n, I notice two default routes pointing to the same gateway (10.0.2.2) via the same interface (eth0):

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.2.2        0.0.0.0         UG    0      0        0 eth0
0.0.0.0         10.0.2.2        0.0.0.0         UG    100    0        0 eth0

While this isn't the root cause of slow performance, it does indicate suboptimal network configuration. The duplicate routes occur because Vagrant automatically configures NAT networking while you've manually added bridged networking.

Your current Vagrantfile mixes three networking modes:

config.vm.share_folder("v-root", "/vagrant", ".", :nfs => true)
config.vm.network :bridged, :bridge => "eth0"
chefs_config.vm.network :hostonly, "192.168.20.50"

The performance bottleneck typically comes from:

  • NFS sharing requiring host-only networking
  • Bridged networking competing with NAT for internet access
  • Potential DNS resolution delays

Modify your Vagrantfile to use separate interfaces for different purposes:

Vagrant.configure("2") do |config|
  config.vm.network "private_network", ip: "192.168.20.50" # For NFS
  config.vm.network "public_network", bridge: "eth0" # For internet
  
  config.vm.synced_folder ".", "/vagrant",
    type: "nfs",
    nfs_udp: false, # TCP is more reliable
    nfs_version: 3 # Better compatibility
    
  # ... rest of your configuration
end

Slow DNS resolution is a common issue. Add these provisioning commands:

config.vm.provision "shell", inline: <<-SHELL
  echo "nameserver 8.8.8.8" > /etc/resolv.conf
  echo "nameserver 8.8.4.4" >> /etc/resolv.conf
  chattr +i /etc/resolv.conf # Prevent Vagrant from overwriting
SHELL

After applying these changes, test your connection:

# Test download speed
curl -o /dev/null http://speedtest.wdc01.softlayer.com/downloads/test100.zip

# Test DNS resolution time
dig google.com | grep "Query time"

For persistent performance issues, consider adjusting TCP window scaling:

sudo sysctl -w net.ipv4.tcp_window_scaling=1
sudo sysctl -w net.core.rmem_max=16777216
sudo sysctl -w net.core.wmem_max=16777216