温馨提示×

Ubuntu虚拟机中如何安装与配置DNS服务器

小樊
35
2025-11-14 20:00:18
栏目: 云计算

Ubuntu 虚拟机安装与配置 DNS 服务器

一 环境准备与安装

  • 更新索引并安装 BIND9 及工具包:
    • sudo apt update && sudo apt install bind9 bind9utils bind9-doc -y
  • 确认服务状态:
    • sudo systemctl status bind9(应显示 active/running)
  • 说明:BIND 的主配置位于 /etc/bind/,常用子配置文件为 named.conf.options(全局选项)、named.conf.local(区域包含)、named.conf.default-zones(默认区域)。

二 配置 BIND9 主文件与区域

  • 编辑全局选项 /etc/bind/named.conf.options(示例为允许任意主机查询并转发上游 DNS):
    • options {
      • directory “/var/cache/bind”;
      • listen-on port 53 { any; };
      • listen-on-v6 { ::1; };
      • allow-query { any; };
      • recursion yes;
      • forward first;
      • forwarders {
        • 223.5.5.5; 223.6.6.6;
        • 8.8.8.8; 8.8.4.4;
        • };
      • dnssec-validation auto;
      • };
  • 在 /etc/bind/named.conf.local 中定义正向与反向区域(将 example.com192.168.1.0/24 替换为你的域名与网段):
    • zone “example.com” {
      • type master;
      • file “/etc/bind/db.example.com”;
      • };
    • zone “1.168.192.in-addr.arpa” {
      • type master;
      • file “/etc/bind/db.192.168.1”;
      • };
  • 创建正向区域文件 /etc/bind/db.example.com(注意 FQDN 以“.”结尾,且 Serial 每次修改需递增):
    • $TTL 604800
    • @ IN SOA ns1.example.com. admin.example.com. (
      • 2 ; Serial
      • 604800 ; Refresh
      • 86400 ; Retry
      • 2419200 ; Expire
      • 604800 ) ; Negative Cache TTL
    • @ IN NS ns1.example.com.
    • ns1 IN A 192.168.1.2
    • @ IN A 192.168.1.2
    • www IN A 192.168.1.3
  • 创建反向区域文件 /etc/bind/db.192.168.1:
    • $TTL 604800
    • @ IN SOA ns1.example.com. admin.example.com. (
      • 2 ; Serial
      • 604800 ; Refresh
      • 86400 ; Retry
      • 2419200 ; Expire
      • 604800 ) ; Negative Cache TTL
    • @ IN NS ns1.example.com.
    • 2 IN PTR ns1.example.com.
    • 3 IN PTR www.example.com.
  • 语法检查与重启:
    • sudo named-checkconf
    • sudo named-checkzone example.com /etc/bind/db.example.com
    • sudo named-checkzone 1.168.192.in-addr.arpa /etc/bind/db.192.168.1
    • sudo systemctl restart bind9。

三 本机与客户端 DNS 设置

  • 本机作为 DNS 客户端使用自建服务器(Ubuntu 18.04+ 常见使用 systemd-resolved):
    • 方案 A(推荐,使用 resolvconf):
      • sudo apt install resolvconf -y
      • echo “nameserver 192.168.1.2” | sudo tee /etc/resolvconf/resolv.conf.d/head
      • sudo resolvconf -u
    • 方案 B(使用 resolved 配置片段):
      • sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
      • sudo systemctl restart systemd-resolved
      • 编辑 /etc/systemd/resolved.conf,设置 DNS=192.168.1.2,然后 sudo systemctl restart systemd-resolved
    • 验证:cat /etc/resolv.conf 应包含 nameserver 192.168.1.2;dig @localhost example.com 应返回解析结果。
  • 局域网其他主机:将 DHCP 或静态网络中的 DNS 设置为 192.168.1.2,或在路由器 DHCP 选项里把首要 DNS 指向该地址。

四 防火墙与网络连通性

  • 若启用防火墙,放行 UDP/TCP 53 端口(示例):
    • sudo ufw allow 53
  • 虚拟机网络建议:
    • 桥接模式:虚拟机与宿主机处于同一网段,局域网其他设备可直接使用虚拟机 IP 作为 DNS。
    • NAT 模式:需在宿主机或虚拟化平台做端口转发(例如将宿主机的 53 转发到虚拟机的 192.168.1.2:53),或改用桥接以便同网段直连。

五 测试与常见问题

  • 常用测试:
    • dig @localhost example.com
    • dig @localhost www.example.com
    • dig -x 192.168.1.3 +short(PTR)
    • nslookup example.com
  • 常见问题排查:
    • 配置语法:named-checkconf 与 named-checkzone 必须返回 OK。
    • 服务状态:systemctl status bind9 查看错误日志(journalctl -u bind9 -xe)。
    • 监听地址:确认 named.conf.options 的 listen-on 包含服务器实际 IP 或 any,且虚拟机防火墙放行 53 端口。
    • resolv.conf 被覆盖:使用 resolvconf 或 resolved 的方式持久化 DNS 设置,避免直接编辑 /etc/resolv.conf。

0