Homestead Vagrant Virtualbox无法在客户操作系统内解析DNS

3

我的Homestead Vagrant虚拟机无法在客户操作系统中解析DNS,尽管网络流量正常通过。

vagrant@homestead:~$ ping google.com
ping: google.com: Temporary failure in name resolution
vagrant@homestead:~$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=121 time=23.0 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=121 time=21.9 ms
^C
--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 21.956/22.516/23.077/0.580 ms

相关问题 已经 指向 修改Virtualbox的natdnshostresolver1标志(尽管我被告知Homestead已经启用此功能); 我已更新下面的Vagrantfile,但无济于事:

# -*- mode: ruby -*-
# vi: set ft=ruby :

require 'json'
require 'yaml'

VAGRANTFILE_API_VERSION ||= "2"
confDir = $confDir ||= File.expand_path(File.dirname(__FILE__))

homesteadYamlPath = confDir + "/Homestead.yaml"
homesteadJsonPath = confDir + "/Homestead.json"
afterScriptPath = confDir + "/after.sh"
customizationScriptPath = confDir + "/user-customizations.sh"
aliasesPath = confDir + "/aliases"

require File.expand_path(File.dirname(__FILE__) + '/scripts/homestead.rb')

Vagrant.require_version '>= 2.1.0'

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

    config.vm.provider :virtualbox do |v|
        v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
    end

    if File.exist? aliasesPath then
        config.vm.provision "file", source: aliasesPath, destination: "/tmp/bash_aliases"
        config.vm.provision "shell" do |s|
            s.inline = "awk '{ sub(\"\r$\", \"\"); print }' /tmp/bash_aliases > /home/vagrant/.bash_aliases && chown vagrant:vagrant /home/vagrant/.bash_aliases"
        end
    end

    if File.exist? homesteadYamlPath then
        settings = YAML::load(File.read(homesteadYamlPath))
    elsif File.exist? homesteadJsonPath then
        settings = JSON::parse(File.read(homesteadJsonPath))
    else
        abort "Homestead settings file not found in #{confDir}"
    end

    Homestead.configure(config, settings)

    if File.exist? afterScriptPath then
        config.vm.provision "shell", path: afterScriptPath, privileged: false, keep_color: true
    end

    if File.exist? customizationScriptPath then
        config.vm.provision "shell", path: customizationScriptPath, privileged: false, keep_color: true
    end

    if Vagrant.has_plugin?('vagrant-hostsupdater')
        config.hostsupdater.aliases = settings['sites'].map { |site| site['map'] }
    elsif Vagrant.has_plugin?('vagrant-hostmanager')
        config.hostmanager.enabled = true
        config.hostmanager.manage_host = true
        config.hostmanager.aliases = settings['sites'].map { |site| site['map'] }
    end


end

还有什么可能会阻止虚拟机解析DNS?或者,我可以在该箱中设置持久化的名称服务器吗?


编辑: 目前,我可以通过手动编辑/etc/resolv.conf并在每次启动时添加名称服务器来使其正常工作。但是,在启动过程中,composer仍然需要DNS,因此这不是一个解决方案。

这是我的Homestead.yaml文件,供感兴趣的人参考:

---
ip: "127.0.0.1"
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/code
      to: /home/vagrant/code

sites:
    - map: erich.test
      to: /home/vagrant/code/snowman/public

databases:
    - homestead

听起来你还没有在虚拟机上设置DNS服务器。通常情况下,这是通过使用DHCP完成的。尝试运行dig google.com @8.8.8.8命令。如果可以正常工作,那么你只需要编辑resolv.conf文件即可。 - Aron
我目前正在做这件事,每次我加载虚拟机时都要这样做。如何保持这个更改?即使如此,这不是最佳解决方案--没有DNS也会影响vagrant up,因为Composer也需要DNS。 - Erich
1
似乎在Windows上使用Vagrant会遇到很多问题,特别是涉及到npm时更是如此,不值得花费这么多精力。 - Erich
@erich 出于好奇,你尝试过使用 --natdnsproxy1 而不是 --natdnshostresolver1 吗?代理机制更简单一些(它只是转发请求),因此如果有其他原因导致客户机无法访问主机 DNS API,它可能可以解决这个问题。 - justbeez
@justbeez 是的,我也尝试过 --natdnsproxy1。Homestead.yaml 文件是最基本的配置,没有设置任何标志。 - Erich
显示剩余2条评论
2个回答

0

我用那行代码解决了这个问题。

 sudo ufw allow out 53,113,123/udp

-2

我早就放弃了解决Homestead/Vagrant的问题。

相反,我建议尝试使用新的Laravel Sail包(Laravel 8.x+),它使用Docker。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接