正向解析DNS服務
前提準備:
一臺centos7做服務器,兩張網卡(橋接and僅主機),一臺centos6做客戶端,僅主機
##### A、搭建DNS服務器:
1、[root@centos7 ~]# yum install bind
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
Installed:
bind.x86_64 32:9.9.4-72.el7
Dependency Installed:
Complete!
2、[root@centos7 ~]# rpm -ql bind
/etc/logrotate.d/named
/etc/named
/etc/named.conf
/etc/named.iscdlv.key
/etc/named.rfc1912.zones
/etc/named.root.key
/etc/rndc.conf
/etc/rndc.key
/etc/rwtab.d/named
/etc/sysconfig/named
/run/named
/var/log/named.log
/var/named
/var/named/data
/var/named/named.ca
/var/named/named.empty
/var/named/named.localhost
/var/named/named.loopback
/var/named/slaves
3、[root@centos7 ~]# rpm -qa "bind*"
bind-libs-lite-9.9.4-72.el7.x86_64
bind-license-9.9.4-72.el7.noarch
bind-9.9.4-72.el7.x86_64
bind-libs-9.9.4-72.el7.x86_64
bind-utils-9.9.4-72.el7.x86_64
5、查看named的狀態且啟動該服務:
[root@centos7 ~]# systemctl status named
● named.service - Berkeley Internet Name Domain (DNS)
Loaded: loaded (/usr/lib/systemd/system/named.service; disabled; vendor preset: disabled)
Active: inactive (dead)
[root@centos7 ~]# systemctl start named
[root@centos7 ~]#
6、查看IP,有IP即可上網
[root@centos7 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:87:73:17 brd ff:ff:ff:ff:ff:ff
inet 192.168.58.254/24 brd 192.168.58.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe87:7317/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:87:73:21 brd ff:ff:ff:ff:ff:ff
inet 172.20.119.243/16 brd 172.20.255.255 scope global noprefixroute dynamic ens37
valid_lft 83928sec preferred_lft 83928sec
inet6 fe80::23ec:ee7:326a:3f9c/64 scope link noprefixroute
valid_lft forever preferred_lft forever
7、編輯網卡配置文件,添加DNS1=192.168.58.254:
[root@Centos6 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
[root@Centos6 ~]# service network restart
Shutting down interface eth0: Device state: 3 (disconnected)
[ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: Active connection state: activating
Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/6
state: activated
Connection activated
[ OK ]
8、我們在客戶端查看配置文件,會顯示主機IP:
[root@Centos6 ~]# cat /etc/resolv.conf
Generated by NetworkManager
domain localdomain
search localdomain
nameserver 192.168.58.1
nameserver 192.168.58.254
(host、dig、nslookup是三個專業測試名字解析的工具)
9、[root@Centos6 ~]# host www.qq.com
;; connection timed out; trying next origin
;; connection timed out; no servers could be reached
此時,我們去修改配置文件:
10、[root@centos7 ~]# vim /etc/named.conf
//
// named.conf
//
// Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
// server as a caching only nameserver (as a localhost DNS resolver only).
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//
// See the BIND Administrator‘s Reference Manual (ARM) for details about the
// configuration located in /usr/share/doc/bind-{version}/Bv9ARM.html
options {
listen-on port 53 { localhost; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; };
/*
- If you are building an AUTHORITATIVE DNS server, do NOT enable recursion.
- If you are building a RECURSIVE (caching) DNS server, you need to enable
"/etc/named.conf" 61L, 1802C
將文件中的127.0.0.1 改為localhost,將allow-query {} 中改為 any ,
11、使其生效:
[root@centos7 ~]# rndc reload
server reload successful
12、此時作為客戶端,去訪問百度,能通,如下:
[root@Centos6 ~]# dig www.baidu.com
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.68.rc1.el6 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 60669
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 5, ADDITIONAL: 5
;; QUESTION SECTION:
;www.baidu.com. IN A
;; ANSWER SECTION:
www.baidu.com. 1200 IN CNAME www.a.shifen.com.
www.a.shifen.com. 300 IN A 61.135.169.125
www.a.shifen.com. 300 IN A 61.135.169.121
;; AUTHORITY SECTION:
a.shifen.com. 1200 IN NS ns4.a.shifen.com.
a.shifen.com. 1200 IN NS ns1.a.shifen.com.
a.shifen.com. 1200 IN NS ns2.a.shifen.com.
a.shifen.com. 1200 IN NS ns5.a.shifen.com.
a.shifen.com. 1200 IN NS ns3.a.shifen.com.
;; ADDITIONAL SECTION:
ns5.a.shifen.com. 1200 IN A 180.76.76.95
ns1.a.shifen.com. 1200 IN A 61.135.165.224
ns3.a.shifen.com. 1200 IN A 112.80.255.253
ns2.a.shifen.com. 1200 IN A 220.181.57.142
ns4.a.shifen.com. 1200 IN A 14.215.177.229
;; Query time: 1018 msec
;; SERVER: 192.168.58.254#53(192.168.58.254)
;; WHEN: Thu Jan 31 19:09:55 2019
;; MSG SIZE rcvd: 260
分析:此時作為客戶端盡管無上外網功能,去ping百度可通,當把7的內網關閉,6無法訪問外網,卻可以訪問7訪問過的百度,這是7留下了緩存!當把7的緩存rndc flush 清除掉,6就無法再訪問外網了,所以,7就叫只緩存服務器,只利用緩存去訪問。
接下來,我們的目標是把它變成能有自己的數據庫,可以解析數據的域後綴的DNS服務器。
1、查看配置文件:
[root@centos7 ~]# vim /etc/named.conf
其中的 “ include "/etc/named.rfc1912.zones"; ” 專門存放域名的
查看:[root@centos7 ~]# vim /etc/named.rfc1912.zones
zone "localhost.localdomain" IN {
type master;
file "named.localhost";
allow-update { none; };
};
zone "localhost" IN {
type master;
file "named.localhost";
allow-update { none; };
};
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 {
type master;
file "named.loopback";
allow-update { none; };
};
zone "1.0.0.127.in-addr.arpa" IN {
type master;
file "named.loopback";
allow-update { none; };
};
zone "0.in-addr.arpa" IN {
type master;
file "named.empty";
allow-update { none; };
};
我們把zone "magedu.com" IN {
type master;
file "magedu.com.zones";
}; 添加進去
[root@centos7 ~]# named-checkconf 來檢查語法,無錯不提示
2、[root@centos7 ~]# cd /var/named
[root@centos7 named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@centos7 named]# cp -p named.localhost magedu.com.zone
[root@centos7 named]# id named
uid=25(named) gid=25(named) groups=25(named)
[root@centos7 named]# vim magedu.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
AAAA :
"magedu.com.zone" 10L, 152C
我們將其文件修改,變成下圖所示:
$TTL 1D
@ IN SOA master admin.magedu.com. (
2019013120 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimun
NS master
master A 192.168.58.254
www CNAME websrv
websrv A 192.168.58.133
blog A 192.168.58.136
@ mx 10 mailsrv1
@ mx 20 mailsrv2
mailsrv1 A 192.168.58.137
mailsrv2 A 192.168.58.138
使用“ named-checkzone ” 來檢查語法。
[root@centos7 ~]# rndc reload
server reload successful
[root@Centos6 ~]# cat /etc/resolv.conf
Generated by NetworkManager
domain localdomain
nameserver 192.168.58.254
搜索結果如下:
[root@Centos6 ~]# dig www.magedu.com
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.68.rc1.el6 <<>> www.magedu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15911
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 18
;; QUESTION SECTION:
;www.magedu.com. IN A
;; ANSWER SECTION:
www.magedu.com. 600 IN A 101.200.188.230
;; AUTHORITY SECTION:
magedu.com. 172154 IN NS ns2.alidns.com.
magedu.com. 172154 IN NS ns1.alidns.com.
我們現在恢復網卡:ifconfig Eth1 up
再添加一個路由:
route add default gw 192.168.58.100 (可隨便寫)
ping www.baidu.com 可通但IP每次都不一樣,
說明DNS服務可以做到均衡負載的功能。
正向解析DNS服務