如何在 Ubuntu 16.04 LTS 上安装和配置 DNS 服务器

在本教程中,我们将向您展示如何在 Ubuntu 16.04 LTS 上安装和配置 DNS 服务器。 对于那些不知道的人,BIND 是一个广泛使用的 DNS 服务器。 理想情况下,DNS 服务器由两台同时工作的机器组成,一台作为主机,另一台作为从机。 如果您的域注册商没有为您提供免费的 DNS 服务器,或者您想要创建自定义 DNS 记录,那么您可能需要托管自己的 DNS 服务器。

本文假设您至少具备 Linux 的基本知识,知道如何使用 shell,最重要的是,您将网站托管在自己的 VPS 上。 安装非常简单,假设您在 root 帐户下运行,如果不是,您可能需要添加 ‘sudo‘ 到命令以获取 root 权限。 我将向您展示在 Ubuntu 16.04 (Xenial Xerus) 服务器上的 ubuntu 上的分步安装和配置 DNS 服务器。

先决条件

  • 运行以下操作系统之一的服务器:Ubuntu 16.04 (Xenial Xerus)。
  • 建议您使用全新的操作系统安装来防止任何潜在问题。
  • 对服务器的 SSH 访问(或者如果您在桌面上,则只需打开终端)。
  • 一种 non-root sudo user或访问 root user. 我们建议充当 non-root sudo user,但是,如果您在充当 root 时不小心,可能会损害您的系统。

在 Ubuntu 16.04 LTS 上安装和配置 DNS 服务器

主持人 角色 私有 FQDN 私有 IP 地址
ns1 主 DNS 服务器 108.100.100.1
ns2 辅助 DNS 服务器 108.100.100.2
主持人 通用主机 你的域名.com 216.239.38.120

步骤 1. 首先,通过运行以下命令确保所有系统包都是最新的 apt-get 终端中的命令。

sudo apt-get update sudo apt-get upgrade

步骤 2. 安装 DNS 服务器 Bind9。

更新系统后,运行以下命令安装用于设置 DNS 服务器的 BIND9 包:

apt-get install bind9 bind9utils bind9-doc

步骤 3. 配置 Bind9。

是时候向您展示如何设置域以解析到服务器的基本配置了:

### nano /etc/bind/named.conf.options  options {     directory "/var/cache/bind";     additional-from-auth no;     additional-from-cache no;     version "Bind Server";      // If there is a firewall between you and nameservers you want     // to talk to, you may need to fix the firewall to allow multiple     // ports to talk.  See https://www.kb.cert.org/vuls/id/800113      // If your ISP provided one or more IP addresses for stable      // nameservers, you probably want to use them as forwarders.       // Uncomment the following block, and insert the addresses replacing      // the all-0's placeholder.       forwarders {          8.8.8.8;         8.8.4.4;      };      //========================================================================     // If BIND logs error messages about the root key being expired,     // you will need to update your keys.  See https://www.isc.org/bind-keys     //========================================================================     dnssec-validation auto;     allow-recursion { 127.0.0.1; };     auth-nxdomain no;    # conform to RFC1035     listen-on-v6 { any; }; };

步骤 4. 配置本地文件。

接下来,我们将配置本地文件,以指定我们的 DNS 区域:

### nano /etc/bind/named.conf.local  //place these lines at the bottom of file  zone "your-domain.com" {         type master;         file "/etc/bind/zones/your-domain.com.db";         allow-transfer { 108.200.200.2; };         also-notify { 108.200.200.200.2; }; };

因为在上面的配置中,我们将区域文件放在“/etc/bind/zones/your-domain.com.db”,那么我们需要创建文件夹和文件:

mkdir /etc/bind/zones nano /etc/bind/zones/your-domain.com.db
$TTL    86400 $ORIGIN your-domain.com. @       IN      SOA     ns1.your-domain.com. root.your-domain.com. (                               1         ; Serial                           86400         ; Refresh                            7200         ; Retry                         2419200         ; Expire                          604800 )       ; Negative Cache TTL ; @       IN      NS      ns1.your-domain.com. @       IN      NS      ns2.your-domain.com. ns1      IN      A       108.100.100.1 ns2      IN      A       108.100.100.2  ;also list other computers @       IN      A       216.239.38.120  www     IN      A       216.239.38.120

然后重新启动 bind9 服务使更改生效:

systemctl restart bind9

步骤 5. 配置辅助 DNS 服务器。

配置从属绑定选项:

### nano /etc/bind/named.conf.options  zone "your-domain.com" { 	type slave; 	file "/var/cache/bind/your-domain.com.db"; 	masters {108.100.100.1;}; };

重新启动 bind9 服务以使更改生效:

systemctl restart bind9

在您更改域的名称服务器之前,此 DNS 服务器将无法工作。 可以从您域的注册商网站完成。 在这种情况下,我们将名称服务器更改为:

ns1.your-domain.com
ns2.your-domain.com

步骤 6. 测试 BIND。

最后,此测试可以在 DNS 服务器本身上或从另一台服务器上或从您自己的 PC 上完成。 在这种情况下,我们将从另一台运行 Ubuntu OS 的服务器上进行测试。

安装 dnsutils:

apt-get install dnsutils

做 dig DNS 测试:

dig your-domain.com

在 Ubuntu 16.04 LTS 上安装和配置 DNS 服务器

做 nslookup dns 测试:

nslookup your-domain.com

在 Ubuntu 16.04 LTS 上安装和配置 DNS 服务器

恭喜! 您已成功安装 DNS 服务器。 感谢您使用本教程在 Ubuntu 16.04 LTS (Xenial Xerus) 系统上安装 DNS 服务器。 如需其他帮助或有用信息,我们建议您查看 官方bind9网站.