Linux 配置 DNS

如果你在 /etc/resolv.conf 文件中看到了“不要修改”或“这是链接”的提示,这通常意味着你的系统正在使用 resolvconfsystemd-resolved 管理 DNS 设置。

方法 1:使用 resolvconf 更新 DNS

如果你的系统使用 resolvconf

  1. 编辑 /etc/resolvconf/resolv.conf.d/base 文件(如果不存在,就创建它):
    1
    sudo nano /etc/resolvconf/resolv.conf.d/base
  2. 添加 DNS 服务器地址,例如:
    1
    2
    nameserver 8.8.8.8
    nameserver 8.8.4.4
  3. 保存文件并退出编辑器。
  4. 更新 resolvconf
    1
    sudo resolvconf -u

方法 2:使用 systemd-resolved 配置 DNS

如果你的系统使用 systemd-resolved

  1. 编辑 /etc/systemd/resolved.conf 文件:
    1
    sudo nano /etc/systemd/resolved.conf
  2. [Resolve] 部分添加或修改 DNS 设置,例如:
    1
    DNS=8.8.8.8 8.8.4.4
  3. 保存文件并退出编辑器。
  4. 重启 systemd-resolved 服务:
    1
    sudo systemctl restart systemd-resolved