DNS是什么?
DNS,Domain Name System或者Domain Name Service(域名系统或者余名服务)。域名系统为Internet上的主机分配域名地址和IP地址。用户使用域名地址,该系统就会自动把域名地址转为IP地址。域名服务是运行域名系统的Internet工具。执行域名服务的服务器称之为DNS服务器,通过DNS服务器来应答域名服务的查询。
环境
server 10.10.70.2 debian1.skills.com
client 10.10.70.3 debian2.skills.com
服务搭建
SERVER
- 安装这两个包,bind9 和dnsutils
root@debian1:~# dpkg -l bind9 dnsutils
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pe>
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==============-===================-============-========================>
ii bind9 1:9.16.22-1~deb11u1 amd64 Internet Domain Name Ser>
ii dnsutils 1:9.16.22-1~deb11u1 all Transitional package for>
lines 1-7/7 (END)
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==============-===================-============-=======================================
ii bind9 1:9.16.22-1~deb11u1 amd64 Internet Domain Name Server
ii dnsutils 1:9.16.22-1~deb11u1 all Transitional package for bind9-dnsutils
- 进入bind主配置目录
root@debian1:/etc/bind# ll
total 48
-rw-r--r-- 1 root root 1991 Oct 25 07:29 bind.keys
-rw-r--r-- 1 root root 342 Feb 24 20:12 db.0
-rw-r--r-- 1 root root 271 Oct 25 07:29 db.127
-rw-r--r-- 1 root root 358 Feb 24 20:13 db.255
-rw-r--r-- 1 root root 353 Oct 25 07:29 db.empty
-rw-r--r-- 1 root root 270 Oct 25 07:29 db.local
-rw-r--r-- 1 root bind 463 Oct 25 07:29 named.conf
-rw-r--r-- 1 root bind 499 Feb 24 20:11 named.conf.default-zones
-rw-r--r-- 1 root bind 165 Oct 25 07:29 named.conf.local
-rw-r--r-- 1 root bind 846 Oct 25 07:29 named.conf.options
-rw-r----- 1 bind bind 100 Jan 8 02:40 rndc.key
-rw-r--r-- 1 root root 1317 Oct 25 07:29 zones.rfc1918
root@debian1:/etc/bind# pwd
/etc/bind
- 编辑dns的主配置文件
root@debian1:/etc/bind# vi named.conf.default-zones
zone "skills.com" {
type master;
file "/etc/bind/db.0";
};
zone "70.10.10.in-addr.arpa" {
type master;
file "/etc/bind/db.255";
};
- 复制正向文件和反向配置文件
root@debian1:/etc/bind# cp db.local db.0
root@debian1:/etc/bind# cp db.127 db.255
- 编辑正向区域 ,域名解析IP
root@debian1:/etc/bind# cat db.0
;
; BIND data file for local loopback interface
;
$TTL 604800
@ IN SOA localhost. root.localhost. (
2 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
@ IN NS localhost.
@ IN A 127.0.0.1
@ IN AAAA ::1
debian1 IN A 10.10.70.2
debian2 IN A 10.10.70.3
debian3 IN A 10.10.70.4
- 编辑反向区域,IP解析域名
root@debian1:/etc/bind# cat db.255
;
; BIND reverse data file for local loopback interface
;
$TTL 604800
@ IN SOA localhost. root.localhost. (
1 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
@ IN NS localhost.
1.0.0 IN PTR localhost.
2 IN PTR debian1.skills.com.
3 IN PTR debian2.skills.com.
4 IN PTR debian3.skills.com.
root@debian1:/etc/bind#
- 重启服务
root@debian1:/etc/bind# systemctl restart named
- 加入开机自启
root@debian1:/etc/bind# systemctl enable named
Synchronizing state of named.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable named
- 反向解析
root@debian1:~# nslookup 10.10.70.2
2.70.10.10.in-addr.arpa name = debian1.skills.com.
root@debian1:~# nslookup 10.10.70.3
3.70.10.10.in-addr.arpa name = debian2.skills.com.
root@debian1:~# nslookup 10.10.70.4
4.70.10.10.in-addr.arpa name = debian3.skills.com.
- 正向解析
root@debian1:~# nslookup debian1.skills.com
Server: 10.10.70.2
Address: 10.10.70.2#53
Name: debian1.skills.com
Address: 10.10.70.2
root@debian1:~# nslookup debian2.skills.com
Server: 10.10.70.2
Address: 10.10.70.2#53
Name: debian2.skills.com
Address: 10.10.70.3
root@debian1:~# nslookup debian3.skills.com
Server: 10.10.70.2
Address: 10.10.70.2#53
Name: debian3.skills.com
Address: 10.10.70.4
CLIENT
- 客户端添加dns地址
root@debian2:~# cat /etc/resolv.conf
domain skills.com
search localdomain
nameserver 10.10.70.2
- ping域名或者IP
root@debian2:~# ping 10.10.70.2
PING 10.10.70.2 (10.10.70.2) 56(84) bytes of data.
64 bytes from 10.10.70.2: icmp_seq=1 ttl=64 time=0.371 ms
64 bytes from 10.10.70.2: icmp_seq=2 ttl=64 time=0.403 ms
^C
--- 10.10.70.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1020ms
rtt min/avg/max/mdev = 0.371/0.387/0.403/0.016 ms
root@debian2:~# ping debian1.skills.com
PING debian1.skills.com (10.10.70.2) 56(84) bytes of data.
64 bytes from debian1.skills.com (10.10.70.2): icmp_seq=1 ttl=64 time=0.393 ms
64 bytes from debian1.skills.com (10.10.70.2): icmp_seq=2 ttl=64 time=0.445 ms
^C
--- debian1.skills.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.393/0.419/0.445/0.026 ms
root@debian2:~#