如何设置debian系统DNS服务器

设置debian系统DNS服务器。

resolvconf

对于安装了resolvconf软件包的用户,请自行查看文档,这里不做讨论。

静态IP情况

通常系统默认使用/etc/resolv.conf中设置的DNS服务器来解析域名。

对于使用静态IP的用户,即:

1
2
3
4
5
6
7
## 查看网络配置
cat /etc/network/interfaces

## 显示
...
iface ens16 inet static
address x.x.x.x/24

那么只需设置/etc/resolv.conf即可。(请确保没有其他DNS软件在动态修改/etc/resolv.conf文件)

1
nameserver 1.1.1.1

DHCP情况

如果你是iface ens16 inet dhcp,OpenWrt等DHCP服务器会自动通告DNS服务器给客户端,那么/etc/resolv.conf将会被DHCP客户端动态修改。

如果你想使用自己自定义的DNS服务器,可以在/etc/dhcp/dhclient.conf中覆盖DNS相关设置。

1
2
## 在文件结尾添加
supersede domain-name-servers 1.1.1.1;

重启后检查/etc/resolv.conf文件看是否生效。

DNS over HTTPS

普通DNS查询是明文的,中间人可以查看你正在查询的域名,53端口在上游网关也很容易被劫持。可以使用 DNS over HTTPS (DoH) 进行加密查询。这里以cloudflare为例

1
2
3
4
5
6
7
8
9
##下载cloudflared
wget -O "cloudflared" "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64"

## 赋予执行权限并移动到/usr/bin下
chmod +x cloudflared
mv cloudflared /usr/bin

## 创建systemd服务文件
touch /etc/systemd/system/cloudflared-doh.service

编辑cloudflared-doh.service

1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=DNS over HTTPS (DoH) proxy client
Wants=network-online.target nss-lookup.target
Before=nss-lookup.target

[Service]
AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
ExecStart=/usr/bin/cloudflared proxy-dns --upstream https://1.1.1.1/dns-query --max-upstream-conns 0

[Install]
WantedBy=multi-user.target

运行

1
2
systemctl daemon-reload
systemctl enable --now cloudflared-doh

检查安装

1
2
3
4
5
## 查看cloudflared是否正在监听53端口
lsof -i:53

## 测试
dig A @127.0.0.1 www.microsoft.com

没问题后按照上述方法将本机DNS服务器设置为127.0.0.1