debian ubuntu 设置DNS 永久设置 重启系统不会丢失


本解决方法在debian 11.7.0、ubuntu 20.04测试有效。其余系统版本请自行测试。

1、debian ubuntu 设置DNS 快捷步骤

编辑 /etc/systemd/resolved.conf 文件,在其中添加dns;

sudo cp /etc/systemd/resolved.conf /etc/systemd/resolved.conf.bak
sudo vi /etc/systemd/resolved.conf

加入一个新行

DNS=114.114.114.114 8.8.8.8 8.8.4.4

重启systemd-resolved.service

sudo systemctl restart systemd-resolved.service

设置systemd-resolved.service 开机自启

sudo systemctl enable systemd-resolved.service

对/etc/resolv.conf 重新做软链接

sudo mv /etc/resolv.conf /etc/resolv.conf.bak
sudo ln -s /run/systemd/resolve/resolv.conf /etc/

2、下面是命令解释

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2.1、决定系统dns的文件是/etc/resolv.conf

改变此文件就可以设置DNS。
但是此文件直接改了也没用,受其他影响这个文件会被重置,重启网络服务或系统后还是会丢配置。

在这里插入图片描述

2.2、谁能最终影响/etc/resolv.conf,就是/etc/systemd/resolved.conf

找到系统中动态变化的影响DNS的文件,/etc/resolv.conf软链接到此文件即可。
这个文件就是/run/systemd/resolve/resolv.conf。

/run/systemd/resolve/resolv.conf 的内容随 /etc/systemd/resolved.conf 改变而改变,像是link。
因此需要改变 /etc/systemd/resolved.conf 的内容,写入dns

vi /etc/systemd/resolved.conf 
DNS=114.114.114.114 8.8.8.8 8.8.4.4

在这里插入图片描述

重启systemd-resolved.service

systemctl restart systemd-resolved.service

cat /run/systemd/resolve/resolv.conf 和 /etc/systemd/resolved.conf 保持一致

在这里插入图片描述

2.3、/etc/resolv.conf 软链接到 /run/systemd/resolve/resolv.conf

原来是软链接到这个文件,这个文件不影响系统DNS文件/etc/resolv.conf,所以不起作用。

ubuntu 20.04 设置DNS

cat /run/systemd/resolve/stub-resolv.conf

ubuntu 20.04 设置DNS

做完软链接后,查看/etc/resolv.conf 已经改变。

cat /etc/resolv.conf

在这里插入图片描述

重启系统后 查看dns

cat /etc/resolv.conf

在这里插入图片描述

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值