如何在 Linux 系统中配置 DNS over HTTPS
本指南将帮助您在 Linux 系统中设置和配置 DNS over HTTPS (DoH),涵盖多种配置方法和最佳实践。
前置条件
- Linux 操作系统
- 管理员(root)权限
- 基本的命令行知识
- 稳定的网络连接
使用 systemd-resolved
步骤 1:检查 systemd-resolved 版本
systemctl --version
systemd-resolve --version
确保版本在 247 或更高,这些版本支持 DoH。
步骤 2:配置 resolved.conf
编辑 /etc/systemd/resolved.conf
:
[Resolve]
DNS=1.1.1.1 1.0.0.1
DNSOverTLS=yes
DNSSEC=yes
Cache=yes
DNSStubListener=yes
对于 DoH,添加以下配置:
[Resolve]
DNS=https://cloudflare-dns.com/dns-query
https://dns.google/dns-query
DNSOverTLS=no
DNSSEC=yes
Cache=yes
DNSStubListener=yes
步骤 3:重启服务
sudo systemctl restart systemd-resolved
sudo systemctl status systemd-resolved
使用 NetworkManager
步骤 1:检查 NetworkManager 版本
nmcli --version
确保版本支持 DoH 功能。
步骤 2:配置连接
使用 nmcli 配置 DoH:
# 列出当前连接
nmcli connection show
# 修改指定连接
sudo nmcli connection modify "连接名称" ipv4.dns "1.1.1.1,1.0.0.1" ipv4.dns-over-tls yes
sudo nmcli connection modify "连接名称" ipv6.dns "2606:4700:4700::1111,2606:4700:4700::1001" ipv6.dns-over-tls yes
步骤 3:应用更改
sudo nmcli connection up "连接名称"
使用独立客户端
dnscrypt-proxy
- 安装
# Debian/Ubuntu
sudo apt install dnscrypt-proxy
# Fedora
sudo dnf install dnscrypt-proxy
# Arch Linux
sudo pacman -S dnscrypt-proxy
- 配置
编辑 /etc/dnscrypt-proxy/dnscrypt-proxy.toml
:
server_names = ['cloudflare', 'google']
listen_addresses = ['127.0.0.1:53']
max_clients = 250
ipv4_servers = true
ipv6_servers = true
dnscrypt_servers = true
doh_servers = true
require_dnssec = true
require_nolog = true
require_nofilter = true
force_tcp = false
timeout = 2500
keepalive = 30
- 启动服务
sudo systemctl enable dnscrypt-proxy
sudo systemctl start dnscrypt-proxy
sudo systemctl status dnscrypt-proxy
Stubby
- 安装
# Debian/Ubuntu
sudo apt install stubby
# Fedora
sudo dnf install stubby
# Arch Linux
sudo pacman -S stubby
- 配置
编辑 /etc/stubby/stubby.yml
:
resolution_type: GETDNS_RESOLUTION_STUB
dns_transport_list:
- GETDNS_TRANSPORT_TLS
tls_authentication: GETDNS_AUTHENTICATION_REQUIRED
tls_query_padding_blocksize: 128
edns_client_subnet_private: 1
round_robin_upstreams: 1
idle_timeout: 10000
listen_addresses:
- 127.0.0.1@53
- 0::1@53
upstream_recursive_servers:
- address_data: 1.1.1.1
tls_auth_name: "cloudflare-dns.com"
- address_data: 1.0.0.1
tls_auth_name: "cloudflare-dns.com"
- 启动服务
sudo systemctl enable stubby
sudo systemctl start stubby
sudo systemctl status stubby
验证配置
方法 1:使用 dig
# 测试 DNS 解析
dig @127.0.0.1 example.com
# 检查 DNSSEC
dig @127.0.0.1 example.com +dnssec
# 验证 DoH 服务器
dig @127.0.0.1 whoami.cloudflare CH TXT
方法 2:使用 resolvectl
# 查看当前 DNS 设置
resolvectl status
# 测试 DNS 解析
resolvectl query example.com
# 检查 DNSSEC
resolvectl query --dnssec example.com
方法 3:在线测试
- 访问 DNS 泄露测试网站
- 运行完整测试
- 验证 DNS 请求路由
故障排除
常见问题
-
解析失败
- 检查网络连接
- 验证配置文件
- 检查服务状态
- 查看系统日志
-
性能问题
- 选择就近服务器
- 优化缓存设置
- 监控响应时间
- 检查系统资源
-
兼容性问题
- 更新软件包
- 检查依赖关系
- 验证系统要求
- 测试备用配置
日志分析
- 系统日志
# 查看 systemd-resolved 日志
journalctl -u systemd-resolved
# 查看 NetworkManager 日志
journalctl -u NetworkManager
# 查看 dnscrypt-proxy 日志
journalctl -u dnscrypt-proxy
- 网络诊断
# 测试网络连接
ping 1.1.1.1
# 检查端口
ss -tulpn | grep 53
# 追踪路由
traceroute 1.1.1.1
最佳实践
-
提供商选择
- 评估隐私政策
- 检查服务可用性
- 考虑地理位置
- 测试连接速度
-
安全建议
- 启用 DNSSEC
- 使用可信提供商
- 定期更新系统
- 监控异常活动
-
性能优化
- 配置本地缓存
- 优化超时设置
- 使用负载均衡
- 监控系统资源
企业部署
配置管理
-
使用配置管理工具
# Ansible 示例 - name: 配置 systemd-resolved template: src: resolved.conf.j2 dest: /etc/systemd/resolved.conf notify: restart resolved
-
监控设置
- 部署监控工具
- 设置告警规则
- 收集性能指标
- 自动化报告
自动化部署
- 脚本部署
#!/bin/bash
# 安装和配置 DoH
install_doh() {
# 安装必要包
apt update
apt install -y dnscrypt-proxy
# 备份配置
cp /etc/dnscrypt-proxy/dnscrypt-proxy.toml /etc/dnscrypt-proxy/dnscrypt-proxy.toml.bak
# 应用配置
systemctl enable dnscrypt-proxy
systemctl start dnscrypt-proxy
}
- 容器化部署
FROM ubuntu:latest
RUN apt-get update && apt-get install -y dnscrypt-proxy
COPY dnscrypt-proxy.toml /etc/dnscrypt-proxy/
EXPOSE 53/udp 53/tcp
CMD ["dnscrypt-proxy", "-config", "/etc/dnscrypt-proxy/dnscrypt-proxy.toml"]
下一步
补充资源
注意事项
-
系统兼容性
- 检查系统版本
- 验证软件依赖
- 测试配置兼容
- 保持更新
-
网络影响
- 监控网络延迟
- 评估带宽使用
- 考虑故障转移
- 优化路由设置
-
安全考虑
- 保护配置文件
- 限制访问权限
- 监控系统日志
- 定期安全审计
-
维护建议
- 定期更新软件
- 备份配置文件
- 监控系统状态
- 制定应急预案