• DNS域名解析服务


    需求根据域名查找出IP地址的流程: 

    1. 使用主机本地的hosts文件查找相关记录
    2. 使用DNS服务进行解析

    DNS系统的作用

    正向解析:根据域名查找对应的IP地址

    反向解析:根据IP地址查找对应的域名

    DNS的端口:

    TCP/53(连接DNS服务器)     UDP/53(解析DNS) 

    DNS系统的分布式数据结构:

    域名结构是一种树形结构,以 根域. 为起点
            主机名.子域.[二级域].顶级域 .
                                                       根域 

    DNS域名解析工作原理 

    1. 客户端会先将DNS解析请求发送给本地缓存域名服务器,
    2. 如果本地缓存域名服务器有相关记录则直接返回给客户端,否则会将DNS解析请求发送给根域服务器进行解析,
    3. 根域服务器会根据域名的项再将DNS解析请求委派给相对应的项级域服务器进行解析,
    4. 顶级域服务器也会根据域名的二级域或子域再将DNS解析请求委派给相对应的二级域或子域服务器进行解析,
    5. 最后子域服务器会根据域名的主机名解析出相对应的IP地址,再返回给本地缓存域名服务器和客户端。  

     DNS域名解析查询方式:

     递归查询:(简单来说就是将DNS解析请求一探到底,再逐层返回)

    1. 本地域名服务器先将DNS解析请求发送给根域名服务器,
    2. 根域名服务器再将解析请求转发给相对应的顶级域名服务器,
    3. 顶级域名服务器再将解析请求转发给相对应的二级域名或子域名服务器,
    4. 最后子域名服务器会根据域名的主机名解析出相对应的IP地址后,再逐层返回给本地域名服务器。

    迭代查询:(简单来说就是每次DNS解析请求都会用相对应的响应回复) 

    1. 本地域名服务器先将DNS解析请求发送给根域名服务器,根域名服务器会返回响应消息给本地域名服务器并告知去找相对应的顶级域名服务器;
    2. 本地域名服务器再将DNS解析请求发送给相对应的顶级域名服务器,顶级域名服务器会返回响应消息给本地域名服务器并告知去找相对应的二级域名或子域名服务器
    3. 最后本地域名服务器将DNS解析请求发送给相对应的子域名服务器,子域名服务器会根据域名的主机名解析出相对应的IP地址后,直接返回给本地域名服务器

    命令 

    搭建本地DNS服务器

    1)初始化系统
    2)安装 bind 软件包
    3)修改主配置 /etc/named.conf      
    listen-on port 53 { any; };      #设置监听IP地址和端口
    allow-query     { any; };        #设置允许访问DNS服务器的客户端

    4)修改区域配置文件 /etc/named.rfc1912.zones
    zone "xy101.com" IN {                     #正向解析配置
            type master;
            file "xy101.com.zone";            #设置区域数据文件名称
    };

    zone "80.168.192.in-addr.arpa" IN {       #反向解析配置,地址倒写
            type master;
            file "xy101.com.zone.local";
    };

    5)修改区域数据文件 /var/named/xy101.com.zone
    cd /var/named
    cp -p named.localhost xy101.com.zone
    vim xy101.com.zone
    ....
            NS      xy101.com.            #设置当前区域名称
            A       192.168.80.20         #设置DNS服务器IP地址
    www  IN A       192.168.80.30         #设置正向解析的主机名与IP地址的映射记录
    xxx  IN CNAME   www                   #设置主机名 xxx 是 www 的别名

    vim xy101.com.zone.local
    ....
    30  IN PTR    www.xy101.com.         #设置IP地址与域名的反向解析记录

    5)重启named服务
    6)客户端设置DNS服务器地址 /etc/resolv.conf ,并验证 nslookup  host  dig

    搭建主从域名服务器

    #主服务器配置
    vim  /etc/named.rfc1912.zones                  
    zone "xy101.com" IN {
            type master;
            file "xy101.com.zone";
            allow-transfer { 192.168.80.30; };     #允许从服务器同步数据文件
    };

    #从服务器配置(不需要配置区域数据文件)
    vim  /etc/named.rfc1912.zones 
    zone "xy101.com" IN {
            type slave;                           #设置为从服务器类型
            masters { 192.168.80.20; };           #指定主服务器地址
            file "slaves/xy101.com.zone";         #设置同步的数据文件保存路径
    };
     

    实验DNS正向解析: 

    关闭防火墙

    1. [root@localhost ~]# systemctl stop firewalld //关闭防火墙
    2. [root@localhost ~]# systemctl disable firewalld
    3. Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
    4. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    5. [root@localhost ~]# setenforce 0
    6. [root@localhost ~]# vim /etc/selinux/config
    7. [root@localhost ~]#

    安装软件 

    1. [root@localhost ~]# cd /mnt/Packages
    2. [root@localhost Packages]# ls | grep bind //ls过滤有没有bind开头
    3. bind-9.11.4-26.P2.el7_9.9.x86_64.rpm
    4. bind-chroot-9.11.4-26.P2.el7_9.9.x86_64.rpm
    5. bind-dyndb-ldap-11.1-7.el7.x86_64.rpm
    6. bind-export-libs-9.11.4-26.P2.el7_9.9.x86_64.rpm
    7. bind-libs-9.11.4-26.P2.el7_9.9.x86_64.rpm
    8. bind-libs-lite-9.11.4-26.P2.el7_9.9.x86_64.rpm
    9. bind-license-9.11.4-26.P2.el7_9.9.noarch.rpm
    10. bind-pkcs11-9.11.4-26.P2.el7_9.9.x86_64.rpm
    11. bind-pkcs11-libs-9.11.4-26.P2.el7_9.9.x86_64.rpm
    12. bind-pkcs11-utils-9.11.4-26.P2.el7_9.9.x86_64.rpm
    13. bind-utils-9.11.4-26.P2.el7_9.9.x86_64.rpm
    14. cmpi-bindings-pywbem-0.9.5-6.el7.x86_64.rpm
    15. keybinder3-0.3.0-1.el7.x86_64.rpm
    16. rpcbind-0.2.0-49.el7.x86_64.rpm
    17. samba-winbind-4.10.16-19.el7_9.x86_64.rpm
    18. samba-winbind-modules-4.10.16-19.el7_9.x86_64.rpm
    19. ypbind-1.37.1-9.el7.x86_64.rpm
    20. [root@localhost Packages]#
    1. [root@localhost ~]# yum install -y bind //安装软件
    2. 已加载插件:fastestmirror, langpacks
    3. Loading mirror speeds from cached hostfile
    4. 正在解决依赖关系
    5. --> 正在检查事务
    6. ---> 软件包 bind.x86_64.32.9.11.4-26.P2.el7_9.9 将被 安装
    7. --> 解决依赖关系完成
    8. 依赖关系解决
    9. ================================================================================================================================================================
    10. Package 架构 版本 源 大小
    11. ================================================================================================================================================================
    12. 正在安装:
    13. bind x86_64 32:9.11.4-26.P2.el7_9.9 local 2.3 M
    14. 事务概要
    15. ================================================================================================================================================================
    16. 安装 1 软件包
    17. 总下载量:2.3 M
    18. 安装大小:5.4 M
    19. Downloading packages:
    20. Running transaction check
    21. Running transaction test
    22. Transaction test succeeded
    23. Running transaction
    24. 正在安装 : 32:bind-9.11.4-26.P2.el7_9.9.x86_64 1/1
    25. 验证中 : 32:bind-9.11.4-26.P2.el7_9.9.x86_64 1/1
    26. 已安装:
    27. bind.x86_64 32:9.11.4-26.P2.el7_9.9
    28. 完毕!
    29. [root@localhost ~]#
    1. [root@localhost ~]#
    2. [root@localhost ~]# rpm -qc bind
    3. /etc/logrotate.d/named
    4. /etc/named.conf
    5. /etc/named.iscdlv.key
    6. /etc/named.rfc1912.zones
    7. /etc/named.root.key
    8. /etc/rndc.conf
    9. /etc/rndc.key
    10. /etc/sysconfig/named
    11. /var/named/named.ca
    12. /var/named/named.empty
    13. /var/named/named.localhost
    14. /var/named/named.loopback
    15. [root@localhost ~]#

    [root@localhost ~]# vim /etc/named.conf
    

     

     也可以这样

     

     

    [root@localhost ~]# vim /etc/named.rfc1912.zones
    

     

     

    1. [root@localhost ~]#
    2. [root@localhost ~]#
    3. [root@localhost ~]# cd /var/named/
    4. [root@localhost named]# ls
    5. data named.ca named.localhost slaves
    6. dynamic named.empty named.loopback
    7. [root@localhost named]# ll
    8. 总用量 16
    9. drwxrwx---. 2 named named 6 224 2022 data
    10. drwxrwx---. 2 named named 6 224 2022 dynamic
    11. -rw-r-----. 1 root named 2253 45 2018 named.ca
    12. -rw-r-----. 1 root named 152 1215 2009 named.empty
    13. -rw-r-----. 1 root named 152 621 2007 named.localhost
    14. -rw-r-----. 1 root named 168 1215 2009 named.loopback
    15. drwxrwx---. 2 named named 6 224 2022 slaves
    16. [root@localhost named]#

     

    1. [root@localhost named]# cp -p named.localhost xy101.com.zone
    2. [root@localhost named]# ll
    3. 总用量 20
    4. drwxrwx---. 2 named named 6 224 2022 data
    5. drwxrwx---. 2 named named 6 224 2022 dynamic
    6. -rw-r-----. 1 root named 2253 45 2018 named.ca
    7. -rw-r-----. 1 root named 152 1215 2009 named.empty
    8. -rw-r-----. 1 root named 152 621 2007 named.localhost
    9. -rw-r-----. 1 root named 168 1215 2009 named.loopback
    10. drwxrwx---. 2 named named 6 224 2022 slaves
    11. -rw-r-----. 1 root named 152 621 2007 xy101.com.zone
    12. [root@localhost named]#

     

    1. [root@localhost named]# vim xy101.com.zone
    2. [root@localhost named]#

     

     

    1. [root@localhost named]# systemctl start named
    2. [root@localhost named]# systemctl enable named
    3. Created symlink from /etc/systemd/system/multi-user.target.wants/named.service to /usr/lib/systemd/system/named.service.
    4. [root@localhost named]#

     

    验证 

     

    1. [root@localhost ~]# vim /etc/resolv.conf
    2. [root@localhost ~]#
    3. [root@localhost ~]# nslookup www.xy101.com
    4. Server: 20.0.0.30
    5. Address: 20.0.0.30#53
    6. Name: www.xy101.com
    7. Address: 20.0.0.40
    8. [root@localhost ~]# nslookup mail.xy101.com
    9. Server: 20.0.0.30
    10. Address: 20.0.0.30#53
    11. Name: mail.xy101.com
    12. Address: 20.0.0.50
    13. [root@localhost ~]# nslookup news.xy101.com
    14. Server: 20.0.0.30
    15. Address: 20.0.0.30#53
    16. Name: news.xy101.com
    17. Address: 20.0.0.60
    18. [root@localhost ~]# nslookup ftp.xy101.com
    19. Server: 20.0.0.30
    20. Address: 20.0.0.30#53
    21. ftp.xy101.com canonical name = www.xy101.com.
    22. Name: www.xy101.com
    23. Address: 20.0.0.40
    24. [root@localhost ~]# nslookup biubiubiu.xy101.com
    25. Server: 20.0.0.30
    26. Address: 20.0.0.30#53
    27. Name: biubiubiu.xy101.com
    28. Address: 20.0.0.100
    29. [root@localhost ~]#
    1. [root@localhost ~]# host www.xy101.com
    2. www.xy101.com has address 20.0.0.40
    3. [root@localhost ~]# host ftp.xy101.com
    4. ftp.xy101.com is an alias for www.xy101.com.
    5. www.xy101.com has address 20.0.0.40
    6. [root@localhost ~]#
    7. [root@localhost ~]# dig www.xy101.com
    8. ; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.9 <<>> www.xy101.com
    9. ;; global options: +cmd
    10. ;; Got answer:
    11. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 10366
    12. ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
    13. ;; OPT PSEUDOSECTION:
    14. ; EDNS: version: 0, flags:; udp: 4096
    15. ;; QUESTION SECTION:
    16. ;www.xy101.com. IN A
    17. ;; ANSWER SECTION:
    18. www.xy101.com. 86400 IN A 20.0.0.40
    19. ;; AUTHORITY SECTION:
    20. xy101.com. 86400 IN NS xy101.com.
    21. ;; ADDITIONAL SECTION:
    22. xy101.com. 86400 IN A 20.0.0.30
    23. ;; Query time: 0 msec
    24. ;; SERVER: 20.0.0.30#53(20.0.0.30)
    25. ;; WHEN: 五 426 02:48:42 CST 2024
    26. ;; MSG SIZE rcvd: 88
    27. [root@localhost ~]#

    实验DNS反向解析:

    在正向的基础上 

    [root@localhost ~]# vim /etc/named.conf
    

    [root@localhost ~]# vim /etc/named.rfc1912.zones
    

     

    1. [root@localhost ~]#
    2. [root@localhost ~]# cd /var/named/
    3. [root@localhost named]# ls
    4. data dynamic named.ca named.empty named.localhost named.loopback slaves xy101.com.zone
    5. [root@localhost named]# ll
    6. 总用量 20
    7. drwxrwx---. 2 named named 23 425 18:17 data
    8. drwxrwx---. 2 named named 60 425 18:18 dynamic
    9. -rw-r-----. 1 root named 2253 45 2018 named.ca
    10. -rw-r-----. 1 root named 152 1215 2009 named.empty
    11. -rw-r-----. 1 root named 152 621 2007 named.localhost
    12. -rw-r-----. 1 root named 168 1215 2009 named.loopback
    13. drwxrwx---. 2 named named 6 224 2022 slaves
    14. -rw-r-----. 1 root named 294 425 18:16 xy101.com.zone
    15. [root@localhost named]#
    16. [root@localhost named]#
    17. [root@localhost named]# cp -p xy101.com.zone xy101.com.zone.local
    18. [root@localhost named]# ll
    19. 总用量 24
    20. drwxrwx---. 2 named named 23 425 18:17 data
    21. drwxrwx---. 2 named named 60 425 18:18 dynamic
    22. -rw-r-----. 1 root named 2253 45 2018 named.ca
    23. -rw-r-----. 1 root named 152 1215 2009 named.empty
    24. -rw-r-----. 1 root named 152 621 2007 named.localhost
    25. -rw-r-----. 1 root named 168 1215 2009 named.loopback
    26. drwxrwx---. 2 named named 6 224 2022 slaves
    27. -rw-r-----. 1 root named 294 425 18:16 xy101.com.zone
    28. -rw-r-----. 1 root named 294 425 18:16 xy101.com.zone.local
    29. [root@localhost named]#

     

    1. [root@localhost named]#
    2. [root@localhost named]# vim xy101.com.zone.local

     

    1. [root@localhost named]# systemctl restart named
    2. [root@localhost named]#

    验证

    1. [root@localhost ~]# nslookup 20.0.0.40
    2. 40.0.0.20.in-addr.arpa name = www.xy101.com.
    3. [root@localhost ~]# nslookup 20.0.0.50
    4. 50.0.0.20.in-addr.arpa name = mail.xy101.com.
    5. [root@localhost ~]# nslookup 20.0.0.60
    6. 60.0.0.20.in-addr.arpa name = news.xy101.com.
    7. [root@localhost ~]# host www.xy101.com.
    8. www.xy101.com has address 20.0.0.40
    9. [root@localhost ~]# host 20.0.0.40
    10. 40.0.0.20.in-addr.arpa domain name pointer www.xy101.com.
    11. [root@localhost ~]# host 20.0.0.50
    12. 50.0.0.20.in-addr.arpa domain name pointer mail.xy101.com.
    13. [root@localhost ~]# host 20.0.0.60
    14. 60.0.0.20.in-addr.arpa domain name pointer news.xy101.com.
    15. [root@localhost ~]#

     

     

     

    实验搭建主从域名服务器 

    以下步骤两台虚拟机同时操作

    1. [root@localhost ~]# systemctl stop firewalld
    2. [root@localhost ~]# systemctl disable firewalld
    3. Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
    4. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    5. [root@localhost ~]# setenforce 0
    6. [root@localhost ~]# vim /etc/selinux/config
    7. [root@localhost ~]# df
    8. 文件系统 1K-块 已用 可用 已用% 挂载点
    9. devtmpfs 1913548 0 1913548 0% /dev
    10. tmpfs 1930624 0 1930624 0% /dev/shm
    11. tmpfs 1930624 12784 1917840 1% /run
    12. tmpfs 1930624 0 1930624 0% /sys/fs/cgroup
    13. /dev/mapper/centos-root 36805060 4577592 32227468 13% /
    14. /dev/sda1 1038336 191084 847252 19% /boot
    15. tmpfs 386128 52 386076 1% /run/user/0
    16. /dev/sr0 4635056 4635056 0 100% /mnt
    17. [root@localhost ~]# yum install -y bind
    18. 已加载插件:fastestmirror, langpacks
    19. Loading mirror speeds from cached hostfile
    20. 正在解决依赖关系
    21. --> 正在检查事务
    22. ---> 软件包 bind.x86_64.32.9.11.4-26.P2.el7_9.9 将被 安装
    23. --> 解决依赖关系完成
    24. 依赖关系解决
    25. ==========================================================================================================================================================
    26. Package 架构 版本 源 大小
    27. ==========================================================================================================================================================
    28. 正在安装:
    29. bind x86_64 32:9.11.4-26.P2.el7_9.9 local 2.3 M
    30. 事务概要
    31. ==========================================================================================================================================================
    32. 安装 1 软件包
    33. 总下载量:2.3 M
    34. 安装大小:5.4 M
    35. Downloading packages:
    36. Running transaction check
    37. Running transaction test
    38. Transacti

    vim /etc/named.conf
    

    以上操作两台虚拟机都要操作

    修改主服务器

    [root@localhost named]# vim /etc/named.rfc1912.zones
    
    1. zone "xy101.com" IN {
    2. type master;
    3. file "xy101.com.zone";
    4. allow-transfer { 20.0.0.10; };
    5. };
    6. zone "0.0.20.in-addr.arpa" IN {
    7. type master;
    8. file "xy101.com.zone.local";
    9. allow-transfer { 20.0.0.10; };
    10. };
    11. zone "localhost.localdomain" IN {
    12. type master;
    13. file "named.localhost";
    14. allow-update { none; };
    15. };
    16. zone "localhost" IN {
    17. type master;
    18. file "named.localhost";
    19. allow-update { none; };
    20. };
    21. zone "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa" IN {
    22. type master;
    23. file "named.loopback";
    24. allow-update { none; };

    1. [root@localhost named]# cd /var/named/
    2. [root@localhost named]# ll
    3. 总用量 24
    4. drwxrwx---. 2 named named 23 425 18:17 data
    5. drwxrwx---. 2 named named 60 425 23:16 dynamic
    6. -rw-r-----. 1 root named 2253 45 2018 named.ca
    7. -rw-r-----. 1 root named 152 1215 2009 named.empty
    8. -rw-r-----. 1 root named 152 621 2007 named.localhost
    9. -rw-r-----. 1 root named 168 1215 2009 named.loopback
    10. drwxrwx---. 2 named named 6 224 2022 slaves
    11. -rw-r-----. 1 root named 294 425 18:16 xy101.com.zone
    12. -rw-r-----. 1 root named 264 425 23:11 xy101.com.zone.local
    13. [root@localhost named]#

     

    1. [root@localhost named]# cp -p named.localhost xy101.com.zone
    2. [root@localhost named]# vim xy101.com.zone

     

     

    从服务器 

    1. [root@localhost ~]#
    2. [root@localhost ~]# vim /etc/named.rfc1912.zones

    先启动主再启动从

    1. [root@localhost named]# systemctl start named
    2. [root@localhost named]# systemctl enable named.service
    3. [root@localhost named]#

    1. [root@localhost ~]# systemctl enable named
    2. Created symlink from /etc/systemd/system/multi-user.target.wants/named.service to /usr/lib/systemd/system/named.service.
    3. [root@localhost ~]#

    验证

    先在主 验证

    1. [root@localhost named]# vim /etc/resolv.conf
    2. [root@localhost named]#

     

     

     

     

    1. [root@localhost ~]# nslookup www.xy101.com
    2. Server: 20.0.0.30
    3. Address: 20.0.0.30#53
    4. Name: www.xy101.com
    5. Address: 20.0.0.40
    6. [root@localhost ~]# nslookup news.xy101.com
    7. Server: 20.0.0.30
    8. Address: 20.0.0.30#53
    9. Name: news.xy101.com
    10. Address: 20.0.0.60
    11. [root@localhost ~]# nslookup 20.0.0.60
    12. 60.0.0.20.in-addr.arpa name = news.xy101.com.
    13. [root@localhost ~]# nslookup 20.0.0.50
    14. 50.0.0.20.in-addr.arpa name = mail.xy101.com.
    15. [root@localhost ~]#

    主服务器关机从还可以继续解析

    1. [root@localhost ~]# nslookup www.xy101.com
    2. Server: 20.0.0.30
    3. Address: 20.0.0.30#53
    4. Name: www.xy101.com
    5. Address: 20.0.0.40
    6. [root@localhost ~]# nslookup news.xy101.com
    7. Server: 20.0.0.30
    8. Address: 20.0.0.30#53
    9. Name: news.xy101.com
    10. Address: 20.0.0.60
    11. [root@localhost ~]# nslookup 20.0.0.60
    12. 60.0.0.20.in-addr.arpa name = news.xy101.com.
    13. [root@localhost ~]# nslookup 20.0.0.50
    14. 50.0.0.20.in-addr.arpa name = mail.xy101.com.
    15. [root@localhost ~]#

    Windows不行有bug

  • 相关阅读:
    动态内存管理
    webpack打包一个文件,做了哪些事情
    OSPF高级特性 —— LSA-3过滤 + Distribute-list过滤
    基于GIS的人口统计数据空间化解决方案
    JavaScript内置对象 - Array数组(四)- 序列生成器
    【博客495】k8s调度器如何自定义插件执行顺序
    【C++】1060:均值 (信息学奥赛)
    【原创】H3C路由器OSPF测试
    C++学习笔记(二十)
    使用tcpdump抓取vpp接口中的报文
  • 原文地址:https://blog.csdn.net/2301_82109773/article/details/138176130