ubuntu 20.04 设置DNS

ubuntu 20.04 设置DNS 快捷步骤

修改 /etc/systemd/resolved.conf 文件,在其中添加dns;
sudo vi /etc/systemd/resolved.conf;
加入一个新行
DNS=114.114.114.114 8.8.8.8 8.8.4.4;
systemctl restart systemd-resolved.service;
sudo mv /etc/resolv.conf /etc/resolv.conf.bak;
sudo ln -s /run/systemd/resolve/resolv.conf /etc/;

下面是命令解释

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

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

改变此文件就可以设置DNS。
此文件改了也没用,受其他影响,重启服务后还是会丢配置。
在这里插入图片描述

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保持一致
在这里插入图片描述

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

原来是软链接到这个文件,这个文件不影响系统DNS,所以不起作用。
ubuntu 20.04 设置DNS
cat /run/systemd/resolve/stub-resolv.conf
ubuntu 20.04 设置DNS
做完软链接后,查看/etc/resolv.conf 已经改变。
cat /etc/resolv.conf
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/OceanWaves1993/article/details/128217710