解决 Debian 10 nameserver 不持久的问题

解决 /etc/resolv.conf里面的nameserver自动变更、重启后失效的问题。


方法一

1
2
3
4
5
6
7
8
9
10
11
## 安装 resolvconf 
apt update && apt install resolvconf

## 添加 nameserver
echo -e "nameserver 8.8.8.8 \nnameserver 1.1.1.1" >> /etc/resolvconf/resolv.conf.d/head

## 重启
systemctl restart resolvconf

## 查看本机DNS状态
cat /etc/resolv.conf

方法二

如果安装了resolvconfresolv.conf仍然会变,可能是 dhclient 问题,dhclient 会从上游网络通过 dhcp 自动获取nameserver并更新。

1
2
3
4
5
## 备份
cp /etc/dhcp/dhclient-enter-hooks.d/resolvconf /etc/dhcp/dhclient-enter-hooks.d/resolvconf.bak

## 编辑
vi /etc/dhcp/dhclient-enter-hooks.d/resolvconf

将代码改为以下,使此功能不执行任何操作。

1
2
3
make_resolv_conf(){
:
}

方法三

配置本机网络/etc/network/interfaces,使用静态 IP 地址和静态 DNS。

1
2
3
4
5
6
7
8
9
10
11
## 原配置
iface eth0 inet dhcp

## 更改为以下
iface eth0 inet static
address [your ip]
netmask [your netmask]
network [your network]
gateway [your gateway]
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers [dns server1] [dns server2]