如何在 Ubuntu Server 上设置静态 DNS 名称服务器地址?

Ale*_*eks 7 server dns dhcp resolv.conf static-ip

我正在尝试在作为虚拟机运行的 Ubuntu 服务器中静态设置 DNS 服务器地址。我遵循了官方 Ubuntu 支持页面上的所有建议,但我根本无法摆脱 DHCP 设置的 ISP 的 DNS 服务器。

我在我的主机静态 IP 地址上分配了 br0 接口,在虚拟机上分配了 eth0 以使用 Google DNS 和我自己在第二个虚拟机上运行的本地 DNS,方法是将其设置为/etc/network/interfaces. 试图摆弄头文件和尾文件/etc/resolvconf/resolv.conf.d/并尝试改变接口顺序,/etc/resolvconf/interface-order但是当我重新启动网络服务时,我每次都获得了 ISP 的 DNS 地址。

有什么方法可以禁用 resolvconf 并手动设置我的 resolv.conf 文件,就像我在 Red Hat 上所做的那样?或者你能告诉我哪个钩子脚本一直把 ISP DNS 放在 resolv.conf 中?我的 ISP 不允许我更改路由器上的 DHCP 设置,所以我不能那样做。

为什么设置DNS服务器这么简单的事情变得这么复杂???

jdt*_*ood 5

1:Resolvconf 将其动态 resolv.conf 文件写入/run/resolvconf/resolv.conf. /etc/resolv.conf是指向后一个位置的符号链接。如果要使用静态 resolv.conf 文件,只需将 /etc/resolv.conf 符号链接替换为文件即可。目前支持但不推荐这样做。

2:据我了解,受影响的机器运行的是 Ubuntu Server 版本。在这种情况下,它使用ifup配置文件为/etc/network/interfaces. 对于通过dhcp方法配置的接口,ifup(通常)dhclientisc-dhcp-client包中使用。Dhclient 从 DHCP 服务器接收名称服务器信息,它的钩子脚本/etc/dhcp/dhclient-enter-hooks.d/resolvconf将此信息发送resolvconfresolv.conf.

有一两件事你可以做的是编辑/etc/resolvconf/interface-order,使得eth0.dhcp之前来eth0.dhclient。(我假设相关的界面是eth0。)如果你有默认的interface-order你可以,例如,在 lineeth0.dhcp之前添加一行eth*

--- interface-order_ORIG    2012-11-06 10:12:47.630529145 +0100
+++ interface-order 2012-11-06 10:13:16.410529800 +0100
@@ -9,6 +9,7 @@
 hso*
 em+([0-9])?(_+([0-9]))*
 p+([0-9])p+([0-9])?(_+([0-9]))*
+eth0.dhcp
 eth*
 ath*
 wlan*
Run Code Online (Sandbox Code Playgroud)

然后在带有正确名称服务器地址dns-nameserversiface eth0节中添加一行/etc/network/interfaces

iface eth0 inet dhcp
    dns-nameservers 1.2.3.4
Run Code Online (Sandbox Code Playgroud)

因为eth0.dhcpcome before eth0.dhclient,正确的域名服务器地址会被包含在resolv.conf不正确的之前。

覆盖包含 DHCP 服务器提供的名称服务器地址的不需要的行为的另一种方法是编辑 dhclient 挂钩脚本。例如,您可以添加如下一行(其中 1.2.3.4 是您要丢弃的名称服务器地址)。

--- resolvconf_ORIG 2012-03-29 22:37:14.000000000 +0200
+++ resolvconf  2012-11-05 20:53:33.312681077 +0100
@@ -54,6 +54,7 @@
            fi
            shopt -s nocasematch
            for nameserver in $new_dhcp6_name_servers ; do
+               [ "$nameserver" = "1.2.3.4" ] && continue
Run Code Online (Sandbox Code Playgroud)

另一种可能性(有点粗糙,因为它是完全静态的)是将名称服务器选项添加到/etc/resolvconf/resolv.conf.d/head.

3:设置DNS名称服务器地址变得更加复杂,因为机器变得移动化,接口越来越多,静态配置逐渐被自动配置取代。

  • 即使你想要一个完全静态的`/etc/resolv.conf` 并删除那里的符号链接,你仍然不应该卸载resolvconf,因为_它的存在会导致其他程序避免覆盖`/etc/resolv.conf`_。 (2认同)