【问题标题】:Ubuntu keeps losing resolv.conf settings?Ubuntu 不断丢失 resolv.conf 设置?
【发布时间】:2017-05-11 13:53:37
【问题描述】:

每次我重新启动我的 ubuntu 服务器时,它都会丢失它的名称服务器设置。我必须跑:

echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf 

每次我重新启动它以使其再次正常工作。

我尝试直接编辑 resolv.conf 仍然无法正常工作。

有什么建议吗?

【问题讨论】:

标签: linux ubuntu


【解决方案1】:

编辑以下文件以在每次重新启动时生效 这是在resolve.conf中添加的resolve conf的头部分

/etc/resolvconf/resolv.conf.d/head

【讨论】:

    【解决方案2】:

    在 Ubuntu 18.04 中运行:

    Sudo rm /etc/resolv.conf
    sudo echo "nameserver xxx.xxx.xxx.xxx" >> /etc/systemd/resolve/resolv.conf
    sudo ln -s /etc/systemd/resolve/resolv.conf /etc/resolv.conf
    

    但我认为更好的选择是编辑 /etc/network/interfaces 文件并正确配置系统,包括您需要的 dns,例如:

    auto lo
    iface lo inet loopback 
    
    auto enp0s3 
    iface enp0s3 inet dhcp
    dns-nameservers 8.8.8.8 8.8.4.4 
    

    在这个例子中,前两行定义本地接口,不要碰它,第三行说当计算机同时启动网卡enp0s3时(你可以找到你的网卡名称命令 ifconfig -a),第四行告诉 enp0s3 卡监听 dhcp 服务器并从那里获取其网络配置所需的数据,最后一行告诉它您要使用哪个 dns。 如果你知道你的网络配置,它总是一样的,或者你没有 dhcp 服务器,文件会是这样的:

        auto lo
        iface lo inet loopback 
    
        auto enp0s3 
        iface enp0s3 inet static
        address 10.10.5.67
        netmask 255.255.255.0
        gateway 10.10.5.1
        broadcast 10.10.5.255
        dns-nameservers 8.8.8.8 8.8.4.4 
    

    另一种可能是配置 netplan 文件,新的默认模式从 ubuntu 17.10 Artful 管理网络。 Here you can see more 关于这个主题,特别是我不喜欢它,但我知道它在复杂情况下的威力。

    【讨论】:

      【解决方案3】:

      您可以通过编辑基本文件来保留设置。

      安装resolvconf:

      sudo apt-get install resolvconf
      

      编辑/etc/resolvconf/resolv.conf.d/base:

      sudo vi /etc/resolvconf/resolv.conf.d/base
      

      添加您的域名服务器:

      nameserver 8.8.8.8
      

      启动resolvconf:

      sudo /etc/init.d/resolvconf start
      

      检查/etc/resolv.conf 是否包含以下行:

      nameserver 8.8.8.8
      

      然后尝试重新启动服务器并再次检查/etc/resolv.conf

      【讨论】:

        【解决方案4】:

        这是因为resolvconf。正如手册页所述,它允许其他程序更改 DNS 解析器配置。您的网络上可能有一个 DHCP 服务器,它为您的主机提供其 IP 地址和 DSN 服务器。

        您可以更改 DHCP 配置或强制 resolv.conf@sahilKataria 建议的第一行。使用你的命令:

        echo "nameserver 8.8.8.8" | sudo tee /etc/resolvconf/resolv.conf.d/head
        

        【讨论】:

        • 运行 Ubuntu 17.10,这个解决方案不再适用于我。出于某种原因,/etc/resolv.conf 总是被 127.0.1.1 覆盖,这在我的世界中是无用的。所以我必须每次重新启动,手动编辑 /etc/resolv.conf --- 即使通过“systemd-resolve --status”显示除了 ubuntu 的最新更新之外没有任何问题。而且我真的不想安装“未绑定”来防止这种情况发生。
        • 是的,新版本的 Ubuntu 在 systemd 中使用了嵌入式 DNS 解析器,所以 resolv.conf 总是指向 127.0.1.1。
        【解决方案5】:

        编辑您的网络计划文件。示例:

        sudo vim /etc/netplan/00-installer-config.yaml
        
        # This is the network config written by 'subiquity'
        network:
          ethernets:
            enp0s31f6:
              dhcp4: true # Using DHCP
              dhcp4-overrides: # Override DHCP
                use-dns: false # Disable DHCP DNS
              routes:
                - to: x.x.x.x/24
                  via: x.x.x.x
                  metric: 100
                - to: x.x.x.x/24
                  via: x.x.x.x
                  metric: 100
              nameservers: # Name Server section
                  search: [somename.ddns.net] # Set your nameserver search
                  addresses: [x.x.x.x] # Set your DNS Server
          version: 2
        

        完成后,运行:

        netplan apply
        

        并确保你有这个:

        $ ls -ltra /etc/resolv.conf 
        /etc/resolv.conf -> /run/systemd/resolve/stub-resolv.conf
        

        如果bind安装在同一台机器上:

        $ cat /etc/hosts
        127.0.1.1       somename.ddns.net somename # example
        bind-ipaddress  somename.ddns.net somename # example
        

        【讨论】:

          猜你喜欢
          • 2014-06-15
          • 2018-01-01
          • 2010-11-04
          • 1970-01-01
          • 1970-01-01
          • 2021-02-28
          • 2023-03-20
          • 1970-01-01
          • 2021-09-27
          相关资源
          最近更新 更多