国内服务器 DNS 最佳实践:SmartDNS + DoH 一键安装(自动 IPv4/IPv6)
Chatgpt做的 测试了好多台 效果尚可
只允许国内安装 国外版请看这个 https://www.nodeseek.com/post-499638-1
特点:
- 自动识别 IPv4 / IPv6
- SmartDNS 仅监听 127.0.0.1
- 上游:阿里 DoH + DNSPod DoH
- 禁用所有可能覆盖 DNS 的组件
- 安装完成后自动检测 dig 是否使用 127.0.0.1
一键安装脚本
sudo bash << 'EOF'set -eecho "SmartDNS + 国内 DoH 安装(自动 IPv4/IPv6)"################################################## 1. 检测系统#################################################if [ -f /etc/debian_version ]; then OS="debian"elif [ -f /etc/lsb-release ]; then OS="ubuntu"elif [ -f /etc/redhat-release ]; then OS="rhel"else echo "❌ 不支持的系统类型"; exit 1fi################################################## 2. 检测 IPv4 / IPv6#################################################HAS_IPV4=0HAS_IPV6=0ping -4 -c1 -W1 dns.alidns.com >/dev/null 2>&1 && HAS_IPV4=1ping -6 -c1 -W1 dns.alidns.com >/dev/null 2>&1 && HAS_IPV6=1if [ $HAS_IPV4 -eq 1 ]; then BOOT1="223.5.5.5" BOOT2="119.29.29.29"elif [ $HAS_IPV6 -eq 1 ]; then BOOT1="2402:4e00::" BOOT2="2402:4e00:1::"else BOOT1="223.5.5.5" BOOT2="119.29.29.29"fiecho "Bootstrap DNS:"echo "$BOOT1"echo "$BOOT2"################################################## 3. 禁止系统篡改 DNS#################################################systemctl disable systemd-resolved --now 2>/dev/null || truerm -f /run/systemd/resolve/* 2>/dev/nullif [[ "$OS" = "debian" || "$OS" = "ubuntu" ]]; thenmkdir -p /etc/cloud/cloud.cfg.dcat > /etc/cloud/cloud.cfg.d/99-disable-resolv.conf.cfg <<CFGmanage_resolv_conf: falsenetwork: config: disabledCFGfitouch /etc/dhcp/dhclient.confgrep -q "supersede domain-name-servers" /etc/dhcp/dhclient.conf || \echo 'supersede domain-name-servers 127.0.0.1;' >> /etc/dhcp/dhclient.conf################################################## 4. resolv.conf(bootstrap)#################################################chattr -i /etc/resolv.conf 2>/dev/null || truecat > /etc/resolv.conf <<DNSnameserver $BOOT1nameserver $BOOT2DNS################################################## 5. 安装 SmartDNS + dig 环境#################################################echo "安装 SmartDNS + dig..."if [[ "$OS" = "debian" || "$OS" = "ubuntu" ]]; then apt update -y apt install -y smartdns dnsutilselse yum install -y epel-release || true yum install -y smartdns bind-utils || dnf install -y smartdns bind-utilsfi################################################## 6. SmartDNS 配置#################################################mkdir -p /etc/smartdns-dohcat > /etc/smartdns-doh/smartdns.conf <<CONFbind 127.0.0.1:53cache-size 20480server $BOOT1server $BOOT2server-https https://dns.alidns.com/dns-queryserver-https https://doh.pub/dns-queryedns-client-subnet 0speed-check-mode ping,tcp:80CONF################################################## 7. systemd 服务#################################################cat > /etc/systemd/system/smartdns-doh.service <<SERVICE[Unit]Description=SmartDNS DoH (Local Mode Only)After=network.target[Service]Type=simpleExecStart=/usr/sbin/smartdns -f -c /etc/smartdns-doh/smartdns.confRestart=alwaysRestartSec=1[Install]WantedBy=multi-user.targetSERVICEsystemctl daemon-reloadsystemctl enable smartdns-dohsystemctl restart smartdns-doh################################################## 8. 最终 resolv.conf#################################################cat > /etc/resolv.conf <<DNSnameserver 127.0.0.1options edns0DNS################################################## 9. 安装 dig(确保存在)#################################################if [[ "$OS" = "debian" || "$OS" = "ubuntu" ]]; then apt install -y dnsutils >/dev/null 2>&1 || trueelse yum install -y bind-utils >/dev/null 2>&1 || dnf install -y bind-utils >/dev/null 2>&1 || truefi################################################## 10. 最终 DNS 检测:必须是 127.0.0.1#################################################echoecho "==> 正在验证 DNS 是否走 127.0.0.1 ..."SERVER_LINE=$(dig baidu.com | grep "SERVER:" || true)echo "dig 输出:$SERVER_LINE"if echo "$SERVER_LINE" | grep -q "127.0.0.1#53"; then echo "✔ DNS 已成功切换到 SmartDNS (127.0.0.1)"else echo "❌ DNS 未生效,请检查:journalctl -u smartdns-doh -n 50"fiechoecho "安装完成。"EOF
评论 (0)