这篇文章上次修改于 201 天前,可能其部分内容已经发生变化,如有疑问可询问作者。
声明:本篇文章仅供学习研究,禁止用于非法途径,如不听劝告,产生的一切后果由使用者承担

一键安装

以下是经过测试的操作系统

  • Debian 11 64位
  • Ubuntu Server 22.04 64位
  • RockyLinux 8 64位
  • CentOS 8 Stream 64位
wget https://www.xlxbk.cn/unboundInstall.sh && chmod +x unboundInstall.sh && ./unboundInstall.sh

脚本内容

name=""
path="/root/unbound"
isCon=""
isTar='command -v tar'
isWget='command -v wget'
isRun() {
    echo -e "\033[36m请选择是否继续\033[0m"
    echo -e "\033[31m选择y会删除/root/unbound目录,请确认/root/unbound目录中是否存在重要文件\033[0m"
    read -p "y or n:" isCon
    if [ $isCon == "y" ]; then
        echo -e "\033[36m清理目录并重新创建\033[0m"
        rm -rf ${path}
        mkdir unbound
    else
        echo -e "\033[31m已停止运行\033[0m"
        exit
    fi
}
if [ $(whoami) != "root" ]; then
    echo -e "\033[31m 请使用root权限执行安装命令! \033[0m"
    exit
fi
cd /root
if [ -d "${path}" ]; then
    echo -e "\033[36m${path} 目录已经存在\033[0m"
    isRun
else
    mkdir unbound
fi
echo -e "\033[36m> > > > > > > > > >开始下载最新版unbound源码< < < < < < < <\033[0m"
if [ -f "unbound-latest.tar.gz" ]; then
    rm -rf unbound-latest.tar.gz
fi
if [ "${isWget}" == "" -a -f "/etc/debian_version" ]; then
    echo -e "\033[36m开始安装wget\033[0m"
    apt install -y wget
    wget https://nlnetlabs.nl/downloads/unbound/unbound-latest.tar.gz
elif [ "${isWget}" == "" -a -f "/etc/redhat-release" ]; then
    echo -e "\033[36m开始安装wget\033[0m"
    yum install -y wget
    wget https://nlnetlabs.nl/downloads/unbound/unbound-latest.tar.gz
else
    echo -e "\033[36m无需安装wget,开始下载\033[0m"
    wget https://nlnetlabs.nl/downloads/unbound/unbound-latest.tar.gz
fi
if [ $? == 0 ]; then
    echo -e "\033[36m下载成功\033[0m"
else
    echo -e "\033[31m下载失败\033[0m"
    echo -e "\033[31m请自行百度wget的安装命令,安装成功后再执行本脚本即可\033[0m"
fi
echo -e "\033[36m> > > > > > > > > >开始解压< < < < < < < <\033[0m"
if [ "${isTar}" == "" -a -f "/etc/debian_version" ]; then
    echo -e "\033[36m开始安装tar\033[0m"
    apt install -y tar
    tar xzf unbound-latest.tar.gz -C ${path}
elif [ "${isTar}" == "" -a -f "/etc/redhat-release" ]; then
    echo -e "\033[36m开始安装tar\033[0m"
    yum install -y tar
    tar xzf unbound-latest.tar.gz -C ${path}
else
    echo -e "\033[36m无需安装tar,开始解压\033[0m"
    tar xzf unbound-latest.tar.gz -C ${path}
fi
if [ $? == 0 ]; then
    echo -e "\033[36m解压成功\033[0m"
else
    echo -e "\033[31m解压失败\033[0m"
    echo -e "\033[31m请自行百度tar的安装命令,安装成功后再执行本脚本即可\033[0m"
    exit
fi
cd ${path}
name=$(ls ${path})
cd ${name}
echo -e "\033[36m> > > > > > > > > >开始安装依赖< < < < < < < <\033[0m"
if [ -f "/etc/debian_version" ]; then
    apt update
    apt install -y build-essential
    apt install -y libssl-dev
    apt install -y libexpat1-dev
    apt install -y bison
    apt install -y flex
    if [ -f "libevent-2.1.12-stable.tar.gz" ]; then
        rm -rf libevent-2.1.12-stable.tar.gz
    fi
    wget https://www.xlxbk.cn/libevent-2.1.12-stable.tar.gz
    tar -zxvf libevent-2.1.12-stable.tar.gz
    cd libevent-2.1.12-stable
    ./configure --prefix=/usr && make && make install
    cd ..
else
    yum install -y gcc openssl-devel expat-devel libevent-devel make
fi
if [ $? == 0 ]; then
    echo -e "\033[36m依赖安装完成\033[0m"
else
    echo -e "\033[31m依赖安装失败\033[0m"
    echo -e "\033[31m请到 https://www.xlxbk.cn/archives/81 查看手动安装教程\033[0m"
    exit
fi
echo -e "\033[36m> > > > > > > > > >开始编译安装unbound< < < < < < < <\033[0m"
./configure --enable-subnet --with-libevent && make && make install
if [ $? == 0 ]; then
    echo -e "\033[36munbound安装完成\033[0m"
    cd /usr/local/etc/unbound
    rm -rf ${path}
else
    echo -e "\033[31munbound安装失败\033[0m"
    echo -e "\033[31m请到 https://www.xlxbk.cn/archives/81 查看手动安装教程\033[0m"
    exit
fi
echo -e "\033[36m> > > > > > > > > >开始配置unbound< < < < < < < <\033[0m"
wget -O root.hints https://www.internic.net/domain/named.root
touch unbound.log && chmod 666 unbound.log
mv unbound.conf unbound.conf.back
wget https://www.xlxbk.cn/unbound.conf
echo -e "\033[36m> > > > > > > > > >开始配置unbound DNSSEC< < < < < < < <\033[0m"
if [ "$(cat /etc/ld.so.conf | grep "/usr/local/lib")" == "/usr/local/lib" ]; then
    echo -e "\033[36m无需配置ld.so.conf\033[0m"
else
    echo "/usr/local/lib" >>/etc/ld.so.conf
    ldconfig
fi
unbound-anchor
if [ -f "/usr/local/etc/unbound/root.key" ]; then
    echo -e "\033[36m unbound DNSSEC 配置完成\033[0m"
else
    echo -e "\033[31m unbound DNSSEC 配置失败\033[0m"
    echo -e "\033[31m请到 https://www.xlxbk.cn/archives/81 查看手动安装教程\033[0m"
    exit
fi
echo -e "\033[36m> > > > > > > > > >启动unbound< < < < < < < <\033[0m"
if [ "$(cat /etc/group | grep "unbound")" != "" ]; then
    unbound-control start
else
    groupadd unbound
    useradd -m -g unbound -s /bin/false unbound
    unbound-control start
fi
if [ $(ps -C unbound --no-header | wc -l) == 0 ]; then
    echo -e "\033[31m unbound 启动失败\033[0m"
    echo -e "\033[31m请到 https://www.xlxbk.cn/archives/81 查看启动教程部分\033[0m"
else
    echo -e "\033[31m unbound 启动成功\033[0m"
    echo -e "\033[31m unbound 默认端口为53\033[0m"
    echo -e "\033[31m unbound 默认监听ip为所有ip(包含公网和私网)\033[0m"
    echo -e "\033[31m unbound 配置文件位于/usr/local/etc/unbound目录下\033[0m"
    echo -e "\033[31m unbound 默认配置包含EDNS和DNSSEC,无需再次配置\033[0m"
fi

如果有什么疑问可以通过网站上的联系方式联系我或者邮件联系我(邮件联系请附上报错截图)