ubuntu

虚拟机ubuntu如何配置DNS服务器

小樊
50
2025-08-05 10:27:10
栏目: 云计算

在Ubuntu虚拟机中配置DNS服务器可以通过以下几种方法实现,这里以使用systemd-resolvedbind9为例进行说明:

使用systemd-resolved配置DNS服务器(适用于Ubuntu 18.04及更高版本)

  1. 安装systemd-resolved(如果尚未安装):

    sudo apt update
    sudo apt install systemd-resolved
    
  2. 编辑systemd-resolved配置文件

    sudo nano /etc/systemd/resolved.conf
    
  3. 取消注释并设置DNS服务器: 在[Resolve]部分添加或修改DNS服务器地址,例如:

    DNS=8.8.8.8 8.8.4.4
    
  4. 重启systemd-resolved服务以应用更改:

    sudo systemctl restart systemd-resolved
    
  5. 验证DNS设置: 使用以下命令检查当前的DNS设置:

    systemd-resolve --status
    

使用bind9配置DNS服务器

  1. 安装bind9

    sudo apt update
    sudo apt install bind9 bind9utils bind9-doc
    
  2. 编辑bind9的主配置文件

    sudo nano /etc/bind/named.conf.options
    
  3. 配置DNS服务器的全局选项,例如设置转发器:

    options {
        directory "/var/cache/bind";
        forwarders {
            8.8.8.8;
            8.8.4.4;
        };
        dnssec-validation auto;
        auth-nxdomain no;    # conform to RFC1035
        listen-on-v6 { any; };
    };
    
  4. 编辑bind9的区域配置文件

    sudo nano /etc/bind/named.conf.local
    
  5. 添加区域配置

    zone "example.local" {
        type master;
        file "/etc/bind/db.example.local";
    };
    
  6. 创建区域数据库文件

    sudo cp /etc/bind/db.local /etc/bind/db.example.local
    sudo nano /etc/bind/db.example.local
    
  7. 添加区域记录

    $TTL    604800
    @       IN      SOA     ns1.example.local. admin.example.local. (
                        2         ; Serial
                        604800         ; Refresh
                        86400         ; Retry
                        2419200         ; Expire
                        604800 )       ; Negative Cache TTL
        ;
        @       IN      NS      ns1.example.local.
        ns1     IN      A       192.168.1.10
        www     IN      A       192.168.1.10
    
  8. 重启bind9服务以应用更改:

    sudo systemctl restart bind9
    
  9. 验证DNS配置: 使用以下命令检查配置文件是否正确:

    sudo named-checkconf
    sudo named-checkzone example.local /etc/bind/db.example.local
    
  10. 测试DNS服务器: 使用nslookupdig命令测试:

    nslookup www.example.local
    dig @127.0.0.1 www.example.local
    

配置客户端使用DNS服务器

0
看了该问题的人还看了