One-click setup of DoH on Linux systems to address DNS resolution pollution
DNS resolution polluted? Just switch to DoH. Cloudflare's tool is laughably simple; just use it directly with the domestic DNS upstream. One line of configuration, and it's done—not that difficult.
目录9 个章节
Preface
Recently, my swag service suddenly failed to renew the certificate.
After diagnosing, I found that it was unable to resolve the acme-v02.api.letsencrypt.org domain name.
Tried switching several DNS servers, but none worked; it should be due to DNS pollution or hijacking.
At this point, I realized that without DoH/DoT, there's probably no solution.🤣
This article records a simple method to implement DoH/DoT on the server.
DoH/DoT
A brief primer: DNS is a protocol that resolves website names to IP addresses.
Standard DNS transmits data in plaintext, making it vulnerable to tampering or hijacking.
DoH (DNS over HTTPS) uses encrypted HTTPS traffic (port 443). It appears similar to normal web browsing, making it less susceptible to DNS pollution or interception.
In addition, there is DoT (DNS over TLS) and ODoH (Oblivious DoH, with enhanced privacy). These are all more secure methods for domain name resolution.
cloudflared
https://github.com/cloudflare/cloudflared
This is an official open-source Cloudflare Tunnel client from Cloudflare, developed in the Go language, and it's very easy to install and deploy.
Introduction
This client can not only connect to Tunnel to achieve intranet penetration but also implement DoH proxy.
This article uses this tool to implement DoH configuration.
Installation
The official Ubuntu Server software repository does not include this tool.
You need to add the Cloudflare official APT repository.
# 1. Add GPG key
`curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg > /dev/null`
# 2. Add Software Repository
`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. Update and Install
sudo apt update
sudo apt install cloudflared
You can also download and install the DEB package directly.
# 1. Download the latest cloudflared deb package
wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
# 2. Installation
sudo dpkg -i cloudflared-linux-amd64.deb
# 3. Verify
cloudflared --version
### Testing
After installation, run:
```bash
sudo cloudflared proxy-dns --address 127.0.0.1 --port 5053
Test it with:
dig @127.0.0.1 -p 5053 acme-v02.api.letsencrypt.org
If it returns a resolution result, then it's successful.
By default, this uses Cloudflare's official DoH.
If it doesn't work, you can switch to a domestic DoH service.
For example, Alibaba's:
For example, Tencent's:
Example
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
If testing with tools like dig or nslookup shows no issues, you can proceed to the next step.
Add Service
Create a systemd service to run cloudflared continuously.
sudo nano /etc/systemd/system/cloudflared-dns.service
Content:
[Unit]
Description=Cloudflared DNS over HTTPS proxy
After=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-query
Restart=always
User=nobody
AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
Apply & Start
sudo systemctl daemon-reexec
sudo systemctl enable --now cloudflared-dns
Configuring DNS
Using a drop-in configuration method to set up DNS
Do not directly modify /etc/systemd/resolved.conf
sudo mkdir -p /etc/systemd/resolved.conf.d
sudo nano /etc/systemd/resolved.conf.d/dns.conf
Content:
[Resolve]
DNS=127.0.0.1:5053
FallbackDNS=223.5.5.5 223.6.6.6
DNSSEC=no
Restart the service
sudo systemctl restart systemd-resolved
Check the effect
resolvectl status
You can see the following output
$ resolvectl status
Global
Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub
DNS Servers: 127.0.0.1:5053
Fallback DNS Servers: 223.5.5.5 223.6.6.6
At this point, it's done, and Docker containers will also use this system DNS by default
Now, when you go to renew the certificate with swag, it will succeed ✌️
One-Click Script
Following the usual practice, I had AI grandpa write a one-click script for configuring DoH
By default, it uses the method of downloading and installing the deb package from the official GitHub repository
You can execute it directly
bash -c "$(curl -fsSL https://gist.github.com/Deali-Axy/8b2ad8e5a601f2c43f6e7debdfb0aa29/raw/3c57dbce7dc0e224ad4ad35606f15cbc34d4810e/install-doh.sh)"
Script Source Code
#!/usr/bin/env bash
set -e
echo "[1/6] Installing 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.deb
else
echo "cloudflared is already installed, skipping."
fi
CLOUDFLARED_BIN=$(command -v cloudflared)
echo "cloudflared path: $CLOUDFLARED_BIN"
echo "[2/6] Creating systemd service..."
sudo tee /etc/systemd/system/cloudflared-dns.service >/dev/null <<EOF
[Unit]
Description=Cloudflared DNS over HTTPS proxy
After=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-query
Restart=always
User=nobody
AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
EOF
echo "[3/6] Reloading systemd and enabling service..."
sudo systemctl daemon-reexec
sudo systemctl enable --now cloudflared-dns
echo "[4/6] Configuring systemd-resolved..."
sudo mkdir -p /etc/systemd/resolved.conf.d
sudo tee /etc/systemd/resolved.conf.d/dns.conf >/dev/null <<EOF
[Resolve]
DNS=127.0.0.1:5053
FallbackDNS=223.5.5.5 223.6.6.6
DNSSEC=no
EOF
echo "[5/6] Restarting systemd-resolved..."
sudo systemctl restart systemd-resolved
echo "[6/6] Checking DNS configuration..."
resolvectl status | grep "DNS Servers"
echo "✅ Installation complete! The system DNS is now using DoH."
Save the above code as install-doh.sh
Run
chmod +x install-doh.sh
./install-doh.sh
The script will automatically:
- Download and install
cloudflared - Write the
systemdservice file and start it - Configure
systemd-resolved - Automatically restart the relevant services and verify the setup
_EOF_
One-click setup of DoH on Linux systems to address DNS resolution pollution
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
别再手动复制SSH公钥了,Linux服务器一键从GitHub快速导入公钥
输入GitHub用户名就自动导入SSH公钥?Ubuntu这懒人功能挺省事。原理不就是爬取`https://github.com/用户名.keys`吗?手动用`curl`也能实现,装`ssh-import-id`更高效,但谁稀罕这些花里胡哨的。
2026-03-11 · 系统配置 · 约 1 分钟
小心误关了NAS服务器!修改Linux的电源键功能
NAS居然离线关机了?电源键被傻逼系统接管了。改改`logind.conf`让这玩意忽略电源键,省得误操作重启关机。谁用谁知道,别真当NAS是玩具。
2025-10-27 · 系统配置 · 约 1 分钟
设置Linux默认的文件管理器
搜索了半天,也试了很久。 用以下命令可以: 使用Xfce的文件管理器 xdg-mime default Thunar.desktop inode/directory thunar.desktop换成你想要的文件管理器名称 再不行就只能把原来的那个文件管理器卸载掉= =。 我比较喜欢用Gnome的文件管理器: xdg-mime default org.gnome.Nautilus.desktop i
2025-10-27 · 系统配置 · 约 1 分钟
Linux设置系统邮件转发到指定邮箱
前言 在Linux的操作中,有的时候每敲一下回车,就出来You have new mail in /var/spool/mail/root的提示,究竟是为什么呢? Linux 系统经常会自动发出一些邮件来提醒用户系统中出了哪些问题(收件箱位置:/var/mail/)。可是这些邮件都是发送给 root 用户的。出于系统安全考虑,通常不建议大家直接使用 root 帐户进行日常操作。所以要想点办法来让系
2025-10-27 · 系统配置 · 约 1 分钟
讨论
共 0 条加载评论中…