15042895833
首页 >> 新闻案例

电脑远程云服务器在Linux系统上一键配置DoH,解决DNS解析被污染问题

作者:云服务器网 | 2026-01-12 04:00:38

云服务器bae

前言

最近我的 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/null2. 添加软件源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.list3. 更新并安装sudo apt updatesudo apt install cloudflared

也可以直接下载 DEB 包安装

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

测试

安装好以后,运行:

sudocloudflaredproxy-dns--address127.0.0.1--port5053

测试一下:

dig@127.0.0.1-p5053acme-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

示例

学生专享云服务器

sudocloudflaredproxy-dns--address127.0.0.1--port5053--upstreamhttps://223.5.5.5/dns-query--upstreamhttps://223.6.6.6/dns-query

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

添加服务

创建 systemd service,让 cloudflared 常驻运行

sudonano /etc/systemd/system/cloudflared-dns.service

内容:

[Unit]Description=Cloudflared DNS over HTTPS proxyAfter=network.tarGET@[Service]ExecStart=/usr/local/bin/cloudflaredproxy-dns --address127.0.0.1--port5053--upstreamhttps://223.5.5.5/dns-query --upstreamhttps://223.6.6.6/dns-query --upstreamhttps://doh.pub/dns-query --upstreamhttps://dns.pub/dns-queryRestart=alwaysUser=nobodyAmbientCapabilities=CAP_NET_BIND_SERVICE[Install]WantedBy=multi-user.target

应用 & 启动

sudo systemctl daemon-reexecsudo systemctlenable--now cloudflared-dns

配置DNS

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

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

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

内容:

[Resolve]DNS=127.0.0.1:5053FallbackDNS=223.5.5.5223.6.6.6DNSSEC=no

重启服务

sudosystemctl restart systemd-resolved

检查生效情况

resolvectlstatus

可以看到以下输出

$resolvectl statusGlobalProtocols:-LLMNR-mDNS-DNSOverTLSDNSSEC=no/unsupportedresolv.confmode: stubDNS Servers: 127.0.0.1:5053Fallback DNS Servers:223.5.5.5223.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; thenwget -q https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -O /tmp/cloudflared.debsudo dpkg -i /tmp/cloudflared.deb || sudo apt-get install -f -yrm -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 systemctlenable--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 +xinstall-doh.sh./install-doh.sh

脚本会自动:

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

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

解锁AI驱动的生产力跃迁

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

关注获取:

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

清华独家课程三部曲:

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

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

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

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

云服务器 云引擎

上一篇:安卓连接云服务器豆包打响第一枪,超级Agent和超级APP开战了
下一篇:阿里云服务器技术CentOS7yum源修改为阿里,配置阿里epel源
联系我们