2018-10-20 | 服务器运维 | UNLOCK

Ubuntu下apt “无法解析域名”解决方案

我的原因是DNS出了错误

百度了一波,发现解决方案很简单,就是手动加入DNS

sudo vim /etc/resolv.conf

加入DNS

nameserver 119.29.29.29

//当然可以自己去百度其他家的DNS,我这是DNS PUBLIC+的地址

按照百度说的我保存重启

结果发现并没有什么卵用...

仔细观摩一下文件

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.

nameserver 127.0.0.53

被覆盖了

而且说明也写着“DO NOT EDIT THIS FILE BY HAND”

我按照其他的方法修改了一下编辑resolv.conf.d目录下的base文件

不过也没有效果

但是按照第二篇看的文章找到了问题所在:

重启系统后DNS设置不但不会被应用还会被覆盖,应该重启网卡来解决

 sudo /etc/init.d/networking restart

完美解决


参考:

https://blog.csdn.net/u011954647/article/details/49965319

https://blog.csdn.net/tsq292978891/article/details/75039926

https://blog.csdn.net/u012207345/article/details/78339218

解决


不过刚刚重启了好像又被覆盖了,很是绝望

干脆写了个脚本,然后开机自动运行脚本得了...

#!/bin/bash

BEGIN INIT INFO

Provides: run.sh

Required-start: $local_fs $remote_fs $network $syslog

Required-Stop: $local_fs $remote_fs $network $syslog

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: starts the run.shh daemon

Description: starts run.sh using start-stop-daemon

END INIT INFO

sudo echo "nameserver 119.29.29.29" >> /etc/resolv.conf
sudo /etc/init.d/networking restart
sudo echo "[INFO][date +%Y-%m-%d date +%H:%M:%S]DNS设置完成"

然后复制到init.d中,再

update-rc.d run.sh defaults 99

评论加载中