您的位置:

自建DNS全解析

一、搭建DNS服务器

1、先选择一台服务器作为DNS服务器。通常情况下,我们使用Linux系统的机器进行DNS服务器的搭建。但Windows也可以使用Microsoft DNS Server进行搭建。为方便理解,我们以Linux系统为例。

2、我们需要安装DNS服务器软件,这里以Bind9为例:

sudo apt-get update
sudo apt-get install bind9
sudo systemctl enable bind9

3、配置Bind9。配置文件为/etc/bind/named.conf,示例配置:

options {
    directory "/var/cache/bind";
    recursion yes;
    allow-query { any; };

    forwarders {
        8.8.8.8;
        8.8.4.4;
    };
};

zone "." {
    type hint;
    file "/etc/bind/db.root";
};

zone "localhost" {
    type master;
    file "/etc/bind/db.local";
};

zone "127.in-addr.arpa" {
    type master;
    file "/etc/bind/db.127";
};

zone "0.in-addr.arpa" {
    type master;
    file "/etc/bind/db.0";
};

zone "255.in-addr.arpa" {
    type master;
    file "/etc/bind/db.255";
};

4、添加域名解析。示例配置:

zone "example.com" {
    type master;
    file "/etc/bind/db.example.com";
};

5、编辑域名解析配置文件,示例配置:

$TTL 7200
@   IN  SOA example.com. admin.example.com. (
        2022041501  ; serial
        3h          ; refresh after 3 hours
        1h          ; retry after 1 hour
        1w          ; expire after 1 week
        1h )        ; minimum TTL of 1 hour

@   IN  NS  ns.example.com.

ns  IN  A   192.168.1.1
websrv  IN  A   192.168.1.2
mail    IN  A   192.168.1.3
ftp IN  CNAME   websrv.example.com.

二、自建DNS解析不到百度

由于百度的DNS解析只有在中国境内可用,所以很可能是因为你的DNS服务器在境外导致无法解析。

解决方法是添加国内DNS服务器到/etc/bind/named.conf文件中作为转发器。示例配置:

options {
    directory "/var/cache/bind";
    recursion yes;
    allow-query { any; };

    forwarders {
        8.8.8.8;
        8.8.4.4;
        114.114.114.114;
        223.5.5.5;
    };
};

三、自建DNS好处

1、更快的解析速度。自建DNS服务器可以缓存域名解析结果,下次再有相同的解析请求时,直接从缓存中获取结果,加快了网站的响应速度。

2、自主掌控域名解析。相较于使用公用的DNS服务器,自建DNS服务器可以确保域名解析隐私安全,并且可以自主掌控解析结果。

3、更加可靠。部署自建DNS服务器可以避免因公用DNS服务器宕机或者遭受攻击造成的DNS解析失败问题。

4、提高网站的安全性。可以拒绝解析一些不安全、恶意的网站,从而提高网站的安全性。

四、自建DNS DOH实现方法

DOH(DNS over HTTPS)是一种新的DNS通信协议,它使用加密的HTTPS通道进行DNS查询和响应。下面是自建DNS实现DOH协议的方法:

1、需要安装Nginx和DNSmasq软件,不再赘述。安装完成后,创建一个nginx.conf配置文件,示例配置:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type   application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  dns.example.com;

        location /dns-query {
            proxy_pass http://localhost:3000/dns-query;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

2、在DNSmasq中设置如下选项:

port=53
bind-interfaces
user=root
resolv-file=/etc/resolv.dnsmasq.conf #如无需指定DNS服务器,请注释这行
conf-dir=/etc/dnsmasq.d/,*.conf

dhcp-option-force=208,254,208,115 #将DNS服务器指定为自己的DNS服务器(防止客户端忽略了122号option)

no-resolv
server=127.0.0.1#3000

五、自建DNS解析

1、在自己的电脑上配置DNS服务器为自建DNS服务器。

2、当你要访问DNS解析过的域名时,首先会向你的DNS服务器发送解析请求。如果你的DNS服务器本地已经缓存了解析结果,直接返回结果。否则,需要向外网DNS服务器发起域名解析请求,将结果存入本地缓存并返回给你的电脑。

六、自建DNS的好处

自建DNS服务器可以避免DNS染毒、DNS劫持等问题,而公用DNS服务器有很多安全隐患,可能会被攻击,或者存在隐私泄露的问题。

七、自建DNS解析服务器好处

自建DNS解析服务器可以加快网站的访问速度,减小因域名解析时间带来的延迟。同时,还可以避免公共DNS服务器缓存的信息不更新,导致信息失效的问题。

八、自建DNS缓存服务器

自建DNS缓存服务器可以将DNS解析结果缓存在本地,下次再次访问时,可以直接从缓存中读取,从而提高网站的响应速度。

九、自建DNS解析服务器

自建DNS解析服务器可以更加精确的解析DNS请求,从而避免一些在特定网络环境下解析错误的问题。

以上是自建DNS全解析,希望能对大家有所帮助。