域名批量解析的常见错误有哪些(在Linux系统上一键配置DoH)

域名批量解析的常见错误有哪些(在Linux系统上一键配置DoH)

admin 2025-10-11 主营业务 59 次浏览 0个评论
前言

最近我的 swag 服务突然证书 renew 失败

诊断了一下发现原来是无法解析 acme-v02.api.letsencrypt.org 域名

换了几个 DNS 都不行,应该是 DNS 被污染或者劫持了

这时我才意识到不上 DoH/DoT 怕是没办法了

本文记录一下用一种简单的方法在服务器上实现 DoH/DoT

DoH/DoT

简单科普一下,DNS 是用来把网站解析到IP地址的协议

正常的 DNS 是明文传输,很容易被污染或者劫持

DoH 是 DNS over HTTPS,走加密的 HTTPS 流量(443 端口),看起来就像访问网页一样,不容易被污染或者劫持

除此之外还有 DoT(DNS over TLS)、ODoH(Oblivious DoH,隐私更强),都是更加安全的域名解析方式

cloudflared

https://github.com/cloudflare/cloudflared

这是 Cloudflare 官方开源的一个 Cloudflare Tunnel 客户端,用 go 语言开发的,非常容易安装部署。

简介

这个客户端不仅可以接入 Tunnel 实现内网穿透,还可以实现 DoH 代理

本文使用这个工具来实现 DoH 配置

安装

Ubuntu Server 的官方软件源没有这个工具

需要添加 Cloudflare 官方 APT 源

# 1. 添加 GPG keycurl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg > /dev/null# 2. 添加软件源echo "deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared jammy main" | sudo tee /etc/apt/sources.list.d/cloudflared.list# 3. 更新并安装sudo apt updatesudo apt install cloudflared

也可以直接下载 DEB 包安装

# 1. 下载 cloudflared 最新 deb 包wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb# 2. 安装sudo dpkg -i cloudflared-linux-amd64.deb# 3. 验证cloudflared --version 测试

安装好以后,运行:

sudo cloudflared proxy-dns --address 127.0.0.1 --port 5053

测试一下:

dig @127.0.0.1 -p 5053 acme-v02.api.letsencrypt.org

如果能返回解析结果,就说明成功了

这里默认使用的是 Cloudflare 官方的 DoH

如果不行的话,可以换成国内的 DoH 服务

比如阿里:

https://223.5.5.5/dns-query

https://223.6.6.6/dns-query

比如腾讯:

https://doh.pub/dns-query

https://dns.pub/dns-query

示例

sudo cloudflared proxy-dns --address 127.0.0.1 --port 5053 \  --upstream https://223.5.5.5/dns-query \  --upstream https://223.6.6.6/dns-query

用 dig 或者 nslookup 之类的工具测试没问题的话,可以进入下一步。

添加服务

创建 systemd service,让 cloudflared 常驻运行

sudo nano /etc/systemd/system/cloudflared-dns.service

内容:

[Unit]Description=Cloudflared DNS over HTTPS proxyAfter=network.target[Service]ExecStart=/usr/local/bin/cloudflared proxy-dns --address 127.0.0.1 --port 5053 \  --upstream https://223.5.5.5/dns-query \  --upstream https://223.6.6.6/dns-query \  --upstream https://doh.pub/dns-query \  --upstream https://dns.pub/dns-queryRestart=alwaysUser=nobodyAmbientCapabilities=CAP_NET_BIND_SERVICE[Install]WantedBy=multi-user.target

应用 & 启动

sudo systemctl daemon-reexecsudo systemctl enable --now cloudflared-dns 配置DNS

使用 drop-in 配置的方式来设置 DNS

不要直接改 /etc/systemd/resolved.conf

sudo mkdir -p /etc/systemd/resolved.conf.dsudo nano /etc/systemd/resolved.conf.d/dns.conf

内容:

[Resolve]DNS=127.0.0.1:5053FallbackDNS=223.5.5.5 223.6.6.6DNSSEC=no

重启服务

sudo systemctl restart systemd-resolved

检查生效情况

resolvectl status

可以看到以下输出

$ resolvectl statusGlobal Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported    resolv.conf mode: stub         DNS Servers: 127.0.0.1:5053Fallback DNS Servers: 223.5.5.5 223.6.6.6

这时候就搞定了,docker容器也会默认使用系统的这个 DNS

此时再去 swag renew 证书,就成功了✌️

一键脚本

老规矩,我让大模型爷爷帮忙写了一个一键配置DoH的脚本

默认使用从官方 GitHub 仓库下载 deb 包安装的方式

可以直接执行

bash -c "$(curl -fsSL https://gist.github.com/Deali-Axy/8b2ad8e5a601f2c43f6e7debdfb0aa29/raw/3c57dbce7dc0e224ad4ad35606f15cbc34d4810e/install-doh.sh)" 脚本源码 #!/usr/bin/env bashset -eecho"[1/6] 安装 cloudflared..."if ! command -v cloudflared >/dev/null 2>&1; then  wget -q https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -O /tmp/cloudflared.deb  sudo dpkg -i /tmp/cloudflared.deb || sudo apt-get install -f -y  rm -f /tmp/cloudflared.debelseecho"cloudflared 已安装,跳过。"fiCLOUDFLARED_BIN=$(command -v cloudflared)echo"cloudflared 路径: $CLOUDFLARED_BIN"echo"[2/6] 创建 systemd service..."sudo tee /etc/systemd/system/cloudflared-dns.service >/dev/null [Unit]Description=Cloudflared DNS over HTTPS proxyAfter=network.target[Service]ExecStart=${CLOUDFLARED_BIN} proxy-dns --address 127.0.0.1 --port 5053 \  --upstream https://223.5.5.5/dns-query \  --upstream https://223.6.6.6/dns-query \  --upstream https://doh.pub/dns-query \  --upstream https://dns.pub/dns-queryRestart=alwaysUser=nobodyAmbientCapabilities=CAP_NET_BIND_SERVICE[Install]WantedBy=multi-user.targetEOFecho"[3/6] 重新加载 systemd 并启用服务..."sudo systemctl daemon-reexecsudo systemctl enable --now cloudflared-dnsecho"[4/6] 配置 systemd-resolved..."sudo mkdir -p /etc/systemd/resolved.conf.dsudo tee /etc/systemd/resolved.conf.d/dns.conf >/dev/null [Resolve]DNS=127.0.0.1:5053FallbackDNS=223.5.5.5 223.6.6.6DNSSEC=noEOFecho"[5/6] 重启 systemd-resolved..."sudo systemctl restart systemd-resolvedecho"[6/6] 检查 DNS 配置..."resolvectl status | grep "DNS Servers"echo"✅ 安装完成!现在系统 DNS 已经走 DoH。"

将以上代码保存为 install-doh.sh

运行

chmod +x install-doh.sh./install-doh.sh

脚本会自动:

下载并安装  cloudflared 写入  systemd  服务文件并启动 配置  systemd-resolved

自动重启相关服务并检查生效

解锁AI驱动的生产力跃迁

程序设计实验室 专注前沿技术落地,每周解析代码级解决方案。

关注获取:

《DeepSeek极速上手手册》24页干货:零基础3天玩转智能编码

清华独家课程三部曲:

❶《DeepSeek从入门到精通》104页精讲(附30+代码实例)

❷《职场效能革命指南》35页实战:7大行业应用场景深度拆解

❸《AI红利捕获手册》65页秘籍:普通人快速构建竞争壁垒的5种路径

与万千技术人共建智能开发新范式。

在Linux系统上一键配置DoH,解决DNS解析被污染问题

转载请注明来自海坡下载,本文标题:《域名批量解析的常见错误有哪些(在Linux系统上一键配置DoH)》

每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,59人围观)参与讨论

还没有评论,来说两句吧...