点击查看原文
在 Debian/Ubuntu 服务器上,DNS 配置不当常会带来解析延迟高、解析结果不一致、被污染/劫持、甚至 DNS 泄漏等问题。为获得更快、更稳定、更干净的解析体验,可以通过本地缓存减少重复查询、并发请求多个上游以择优返回,从而显著降低首包等待时间并降低单点故障与污染风险。
本文介绍一种简洁可靠的方案:
使用开源的 Mosdns-X 本地 DNS 加速服务,让服务器快速、干净地解析域名。
一、为什么选择 Mosdns-X?
🕒 降低解析延迟:并发查询多个上游,返回最快结果。
🧠 智能缓存:常用域名二次访问几乎瞬间响应。
🧩 轻量部署:一个可执行文件 + 一个 YAML 配置,即可运行。
⚙️ Systemd 管理:自动启动、日志轮转、可持续运行。
开源项目地址:https://github.com/pmkol/mosdns-x
二、安装 Mosdns-X
系统为 Debian / Ubuntu x86_64 (Root 用户)。
更新系统
sudo apt update -y && sudo apt install -y curl wget
创建运行目录与日志目录
sudo mkdir -p /etc/mosdns-x /var/log/mosdns-x
sudo touch /var/log/mosdns-x/mosdns-x.log
sudo chmod -R 755 /etc/mosdns-x /var/log/mosdns-x
下载最新版本的 Mosdns-X
Releases 地址:https://github.com/pmkol/mosdns-x/releases
BIN_URL=$(curl -s https://api.github.com/repos/pmkol/mosdns-x/releases/latest \ | grep browser_download_url \ | grep linux-amd64 \ | cut -d '"' -f 4 \ | head -n1)sudo wget -O /usr/local/bin/mosdns-x "$BIN_URL"sudo chmod +x /usr/local/bin/mosdns-x三、配置文件(范例)
创建 /etc/mosdns-x/config.yaml:
sudo tee /etc/mosdns-x/config.yaml > /dev/null <<'EOF'# mosdns-x 并发查询(无分流)配置log: level: info file: /var/log/mosdns-x/mosdns-x.logplugins: # 缓存插件 - tag: cache type: cache args: size: 1024 lazy_cache_ttl: 1800 # 并发上游:取最先返回的可用答案 - tag: forward_all type: fast_forward args: upstream: # 阿里 - addr: "udp://223.5.5.5" - addr: "tls://dns.alidns.com" # DNSPod / doh.pub - addr: "udp://119.29.29.29" - addr: "tls://dot.pub" # Cloudflare - addr: "udp://1.1.1.1" - addr: "tls://cloudflare-dns.com" # Google - addr: "udp://8.8.8.8" - addr: "tls://dns.google" # 主流水线:小缓存 → 并发优选 - tag: main type: sequence args: exec: - cache - forward_all# 监听(双栈 UDP/TCP 53)servers: - exec: main listeners: - addr: :53 protocol: udp - addr: :53 protocol: tcpEOF四、创建 Systemd 服务文件
sudo tee /etc/systemd/system/mosdns.service > /dev/null <<'EOF'[Unit]Description=Mosdns-X DNS AcceleratorAfter=network.target[Service]Type=simpleUser=rootGroup=rootExecStart=/usr/local/bin/mosdns-x start --as-service -d /usr/local/bin -c /etc/mosdns-x/config.yamlRestart=alwaysRestartSec=5StandardOutput=journalStandardError=journalSyslogIdentifier=mosdns[Install]WantedBy=multi-user.targetEOFsudo systemctl daemon-reloadsudo systemctl enable mosdnssudo systemctl start mosdns五、接管系统 DNS
# 备份系统 DNSsudo cp -n /etc/resolv.conf /etc/resolv.conf.mosdns-backup# 改为使用本地 Mosdns-Xecho -e "nameserver 127.0.0.1\noptions edns0" | sudo tee /etc/resolv.conf# 若 53 端口被 systemd-resolved 占用,可禁用它sudo systemctl disable --now systemd-resolved 2>/dev/null || true📌 如果想顺便加锁(防止被 DHCP 修改),加上 chattr 一起执行:
echo -e "nameserver 127.0.0.1\n" > /etc/resolv.conf && chattr +i /etc/resolv.conf✅ 这条一行命令同时做到:
修改 /etc/resolv.conf
给文件加锁保护(防止意外被改动)
📌 如果未来想解锁,恢复可修改:
一行解锁命令:
chattr -i /etc/resolv.conf(然后你可以用 DHCP 或自己再重新写 resolv.conf)
六、验证运行状态
查看进程状态
sudo systemctl status mosdns --no-pager测试解析速度(第二次命中缓存更快)
dig +stats www.google.comdig +stats www.baidu.com查看实时日志
tail -f /var/log/mosdns-x/mosdns-x.log正常情况下,你会看到 Mosdns-X 监听在 53 端口,并能解析所有域名。
国内和国际域名均可快速返回结果,解析速度显著提升。
七、维护与卸载
# 更新到最新版sudo systemctl stop mosdnsBIN_URL=$(curl -s https://api.github.com/repos/pmkol/mosdns-x/releases/latest \ | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4 | head -n1)sudo wget -O /usr/local/bin/mosdns-x "$BIN_URL"sudo chmod +x /usr/local/bin/mosdns-xsudo systemctl restart mosdns# 卸载sudo systemctl disable --now mosdnssudo rm -f /etc/systemd/system/mosdns.servicesudo rm -rf /etc/mosdns-x /var/log/mosdns-x /usr/local/bin/mosdns-xsudo mv -f /etc/resolv.conf.mosdns-backup /etc/resolv.conf 2>/dev/null || true
评论 (0)