Ubuntu建立公共DNS服务器脚本

curl -fsSL -u easy-script:easy-script 'https://file.surtr.nl/script/public-dns.sh' -o public-dns.sh && chmod +x public-dns.sh && sudo ./public-dns.sh

 

 

#!/bin/bash

# 确保以 root 用户运行
if [ "$(id -u)" -ne 0 ]; then
    echo "请以 root 用户或使用 sudo 运行此脚本!"
    exit 1
fi

# 更新系统和安装 BIND9
echo "更新系统并安装 BIND9..."
apt update -y && apt upgrade -y
apt install bind9 bind9utils bind9-doc ufw -y

# 配置 BIND9
echo "配置 BIND9 为公共 DNS 解析服务..."

# 编辑 BIND9 配置文件,启用递归解析和 DNS 转发
cat <<EOF > /etc/bind/named.conf.options
options {
    directory "/var/cache/bind";

    // 启用递归解析
    recursion yes;
    allow-query { any; };

    // 转发器设置
    forwarders {
        8.8.8.8;    // Google DNS
        8.8.4.4;    // Google DNS
        1.1.1.1;    // Cloudflare DNS
    };

    auth-nxdomain no;  // 按照 RFC1035
    listen-on-v6 { any; };
};
EOF

# 配置防火墙,允许 DNS 请求通过
echo "配置防火墙,允许 DNS 请求通过..."
ufw allow 53
ufw reload

# 配置 BIND9 的默认日志
echo "配置 BIND9 日志..."
cat <<EOF > /etc/bind/named.conf
logging {
    channel default_file {
        file "/var/log/named.log" versions 3 size 5m;
        severity info;
        print-time yes;
        print-severity yes;
        print-category yes;
    };
    category default { default_file; };
};
EOF

# 启用并重启 BIND9 服务
echo "重启 BIND9 服务..."
systemctl restart bind9
systemctl enable bind9

# 检查 BIND9 服务状态
echo "检查 BIND9 服务状态..."
systemctl status bind9

# 完成
echo "公共 DNS 解析服务已配置完成!"
echo "你可以通过以下命令测试 DNS 解析:"
echo "  dig @localhost example.com"
echo "  nslookup example.com 127.0.0.1"
© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享