方法1:直接编辑resolv.conf文件
- 如果是Ubnutu系统,需要先解除软连接,确保持久生效,不被systemd-resolved覆盖
unlink /etc/resolv.conf
- 关闭systemd-resolved服务避免影响
systemctl stop systemd-resolved
- 直接编辑文件/etc/resolv.conf
vim /etc/resolv.conf
写下内容:
nameserver 8.8.8.8
nameserver 114.114.114.114
- 测试
直接curl拉取常规地址,如百度和谷歌,能返回结果则设置正确
# 国内环境
curl www.baidu.com
# 外网环境
curl www.google.com
百度通常返回结果:
谷歌通常返回结果:
方法2:使用systemd-resolved服务
- 编辑文件vim /etc/systemd/resolved.conf
vim /etc/systemd/resolved.conf
- 写入内容如下:
[Resolve]
DNS=114.114.114.114 8.8.8.8
- 重启服务
sudo systemctl enable systemd-resolved
sudo systemctl restart systemd-resolved
- 检查服务状态
systemctl status systemd-resolved