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,所以不起作用。

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

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

重启系统后 查看dns
cat /etc/resolv.conf

本文介绍如何在Debian和Ubuntu系统中永久设置DNS,确保即使系统重启后设置也不会丢失。通过编辑/etc/systemd/resolved.conf文件添加DNS服务器,并设置systemd-resolved服务开机自启,最后将/etc/resolv.conf软链接到/run/systemd/resolve/resolv.conf。

342

被折叠的 条评论
为什么被折叠?



