解决 Debian 或 Ubuntu 配置 DNS 和 Hosts 重启丢失问题

104

解决配置 DNS (nameserver) 丢失

问题

  一般为系统配置 DNS 服务器地址,需要修改 /etc/resolv.conf 文件在其中添加 nameserver [DNS IP],但重启后 /etc/resolv.conf 文件会被覆盖。

解决方法

  系统使用 ifupdown 来管理网络,如果要配置dns服务器地址,一般在 /etc/network/interfaces 文件或 /etc/network/interfaces.d/ 中的子配置文件中,在某个iface 网卡下配置一个 dns-nameservers [DNS IP] ,如果要配置多个dns服务器地址,则中间用空格分隔。

操作步骤

  1. 使用 apt install resolvconf 安装 resolvconf
  2. 在 /etc/network/interfaces 文件或 /etc/network/interfaces.d/ 中的子配置文件中配置 dns-nameservers 1.1.1.1
  3. 重启网络服务 systemctl restart networking

hosts 文件被还原

问题

  修改 /etc/hosts 文件后重启被还原。

解决方法

  参照 /etc/hosts 文件头部的注释。

操作步骤

  • 方法1:文件 /etc/hosts 中的数据添加完毕后,在文件 /etc/cloud/templates/hosts.debian.tmpl 中添加同样数据
  • 方法2:修改文件 /etc/cloud/cloud.cfg ,将 manage_etc_hosts 一项的值由 true 改为 false,在 /etc/hosts 中添加数据