linux如何使用DNS over TLS

前置准备

# 版本支持,注意 centos7.9 yum源中自带的 unbound Version 1.6.6 版本过低,不支持 DoT

# 1、为了兼容ipv4 和 ipv6 配置,需确保系统 同时在 lo 网卡 开启ipv4 和ipv6
    sysctl net.ipv6.conf.lo.disable_ipv6    # 如果输出是 1,说明 IPv6 被禁用了
    # 临时开启
    sysctl -w net.ipv6.conf.lo.disable_ipv6=0
    # 永久开启
    sed -i '/net.ipv6.conf.lo.disable_ipv6/d' /etc/sysctl.conf
    echo "net.ipv6.conf.lo.disable_ipv6 = 0" >> /etc/sysctl.conf
    sysctl -p

# 2、系统必须安装 ca-certificates 程序包(需要他提供的证书)
    dnf install -y ca-certificates
    apt install -y ca-certificates

# 附、安装 ca-certificates 后,各大主流系统 证书的路径
    # Debian / Ubuntu
        /etc/ssl/certs/ca-certificates.crt
    # CentOS / RHEL / Rocky / AlmaLinux
        /etc/pki/tls/certs/ca-bundle.crt
    # Fedora(实测 rockylinux9也有)
        /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
    # Arch Linux、Alpine Linux 、Gentoo
        /etc/ssl/certs/ca-certificates.crt
    # SUSE / openSUSE
        /var/lib/ca-certificates/ca-bundle.pem

# 附、部署方案实测通过的系统
    Rocky9、Debian12、

安装并配置unbound

# 安装unbound
    dnf install -y unbound
    apt install -y unbound

# 配置unbound(为了兼容不同系统 conf.d 配置文件路径的不同,直接使用主配置文件unbound.conf)
    cat > /etc/unbound/unbound.conf <<"EOOF"
server:
  # 仅监听本地接口,防止外部直连(不用担心某些系统自带的 systemd-resolve 监听53,因为他们是监听的 127.0.0.53%lo:53 这个地址)
  interface: 127.0.0.1@53
  interface: ::1@53

  # 本地回环
  access-control: 127.0.0.0/8 allow
  access-control: ::1/128 allow

  # IPv4 私网地址段
  access-control: 10.0.0.0/8 allow
  access-control: 172.16.0.0/12 allow
  access-control: 192.168.0.0/16 allow

  # IPv6 本地链路地址段(fe80::/10)不适合用于 DNS 请求,一般不加
  # IPv6 唯一本地地址(ULA)fc00::/7
  access-control: fc00::/7 allow

  # 缓存大小、并发等可选优化
  verbosity: 1
  num-threads: 2
  cache-min-ttl: 1200
  cache-max-ttl: 7200

  # 用于校验上游 DoT 服务器证书的 CA 证书捆绑
  tls-cert-bundle: "/etc/ssl/certs/ca-certificates.crt"

  # 禁用 DNSSEC 验证(用于 forward 模式,关闭验证,非常重要)
  module-config: "iterator"

# 将根区域(".")下的所有查询通过 DoT 转发
forward-zone:
  name: "."
  forward-tls-upstream: yes
  # 阿里 DNS over TLS 上游
  # #dns.alidns.com 这种写法是表示一个SNI地址为dns.alidns.com,在抓包中会有体现
  forward-addr: 223.5.5.5@853#dns.alidns.com
  forward-addr: 223.6.6.6@853#dns.alidns.com
  forward-addr: 2400:3200::1@853#dns.alidns.com
  forward-addr: 2400:3200:baba::1@853#dns.alidns.com
  forward-addr: 101.226.4.6@853#dot.360.cn
EOOF

修正配置文件中 CA 文件的名称和路径,并修改 /etc/resolv.conf、启动 unbound 并测试

#!/bin/bash

# 获取发行版 ID,例如:debian、ubuntu、centos、rocky、almalinux
DISTRO_ID=$(source /etc/os-release && echo "$ID")
UNBOUND_CONF="/etc/unbound/unbound.conf"

# 输出识别到的系统
echo "Detected distribution: $DISTRO_ID"

# 根据系统选择替换路径
case "$DISTRO_ID" in
    debian|ubuntu|arch)
        # Debian / Ubuntu / Arch Linux 使用这个路径
        sed -i 's|^[[:space:]]*tls-cert-bundle:.*|  tls-cert-bundle: "/etc/ssl/certs/ca-certificates.crt"|' ${UNBOUND_CONF}
        ;;
    centos|rhel|rocky|almalinux)
        # RHEL / CentOS / Rocky / AlmaLinux 使用这个路径
        sed -i 's|^[[:space:]]*tls-cert-bundle:.*|  tls-cert-bundle: "/etc/pki/tls/certs/ca-bundle.crt"|' ${UNBOUND_CONF}
        ;;
    *)
        echo "Unsupported or unknown distribution: $DISTRO_ID"
        exit 1
        ;;
esac

echo -e "# MHM备注 \nnameserver     127.0.0.1\nnameserver     ::1" > /etc/resolv.conf
echo "Update completed for ${UNBOUND_CONF}"

systemctl restart unbound

centos7.9 通过dnsproxy 实现 DoT

## 项目地址:这是 AdguardTeam 开发的
    https://github.com/AdguardTeam/dnsproxy

## 下载安装
    # wget https://github.com/AdguardTeam/dnsproxy/releases/download/v0.75.5/dnsproxy-linux-amd64-v0.75.5.tar.gz
    curl -4 -s -o /usr/local/bin/dnsproxy https://qiniu.wsfnk.com/bokefiles/dnsproxy-linux-amd64-v0.75.5 ; chmod +x /usr/local/bin/dnsproxy
    # curl -4 -s -o /usr/local/bin/dnsproxy https://qiniu.wsfnk.com/bokefiles/dnsproxy-linux-arm64-v0.75.5 ; chmod +x /usr/local/bin/dnsproxy

## 调测、及编写 systemd 服务单元文件
    # 调测
    dnsproxy --listen=127.0.0.1 --listen=::1 --port=53 --upstream=tls://dot.pub:853 --bootstrap=223.5.5.5 --bootstrap=8.8.8.8 --upstream-mode=parallel --cache --cache-size=2097152 --cache-max-ttl=7200 --cache-min-ttl=1200

    # 编写服务单元文件
    cat > /etc/systemd/system/dnsproxy.service  <<"EOOF"
[Unit]
Description=DNS Proxy with DNS-over-TLS Support
After=network.target

# 开启缓存、缓存2MB、最大缓存2小时、最小缓存20分钟
[Service]
ExecStartPre=sysctl -w net.ipv6.conf.lo.disable_ipv6=0
ExecStart=/usr/local/bin/dnsproxy --listen=127.0.0.1 --listen=::1 --port=53 \
  --upstream=tls://dns.alidns.com:853 --upstream=tls://dot.pub:853 \
  --bootstrap=223.5.5.5 --bootstrap=119.29.29.29 \
  --bootstrap=2400:3200:baba::1 --bootstrap=2402:4e00:: \
  --upstream-mode=parallel \
  --cache --cache-size=2097152 --cache-max-ttl=7200 --cache-min-ttl=1200 
#  --verbose
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOOF

## 启动 dnsprooxy
    systemctl daemon-reload
    systemctl enable --now dnsproxy
    echo -e "# MHM备注 \nnameserver     127.0.0.1\nnameserver     ::1" > /etc/resolv.conf

附、dnsproxy 使用介绍及注意事项

## --help 帮助文档

## Tips
    sni是tls握手的识别码,host是http协议的host值,ip是连接地址,这三个值是不一样的。
    stamp 只能设置host=sni,不能单独设置host和sni。

file

声明:本文为原创,作者为 辣条①号,转载时请保留本声明及附带文章链接:https://boke.wsfnk.com/archives/1481.html
谢谢你请我吃辣条谢谢你请我吃辣条

如果文章对你有帮助,欢迎点击上方按钮打赏作者

最后编辑于:2025/5/25作者: 辣条①号

目标:网络规划设计师、系统工程师、ceph存储工程师、云计算工程师。 不负遇见,不谈亏欠!

暂无评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

arrow grin ! ? cool roll eek evil razz mrgreen smile oops lol mad twisted wink idea cry shock neutral sad ???

文章目录