技术小黑屋

树莓派修改 DNS 服务器配置

修改配置文件

1
sudo vim /etc/dhcpcd.conf

增加如下内容,我们使用Google DNS(北京联通测试速度不错)

1
static domain_name_servers=8.8.8.8 8.8.4.4

完整的示例文件部分

1
2
3
4
interface eth0
        static ip_address=192.168.1.3/24
        static routers=192.168.1.1
        static domain_name_servers=8.8.8.8 8.8.4.4

设置配置生效

1
sudo service dhcpcd restart

验证是否生效

我们使用dig命令验证dns是否生效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
dig droidyue.com

; <<>> DiG 9.11.5-P4-5.1-Raspbian <<>> droidyue.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 54841
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;droidyue.com.            IN  A

;; ANSWER SECTION:
droidyue.com.     299 IN  A   104.27.129.205
droidyue.com.     299 IN  A   104.27.128.205

;; Query time: 44 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sun Apr 19 17:07:11 CST 2020
;; MSG SIZE  rcvd: 73

重点看这个信息

1
2
3
4
;; Query time: 44 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sun Apr 19 17:07:11 CST 2020
;; MSG SIZE  rcvd: 73

注意

不要直接修改这个文件/etc/resolv.conf

1
2
3
4
5
# 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
nameserver 8.8.8.8
nameserver fe80::1%eth0
You have new mail in /var/mail/pi

正如上面英文说得,这个文件会被冲掉,未来某个时间你还需要重新设置。所以按照文章中的修改即可。

推荐阅读