搭建一個簡單的DNS
為了更方便和清晰的了解DNS的作用,通過搭建一個簡單的DNS服務來學習。
在搭建之前,先簡單了解一下DNS的工作原理
- 客戶端把訪問的域名傳遞給DNS服務器a,如果有記錄,則將IP傳遞給客戶端
- DNS服務器a沒有記錄,則以遞歸方式訪問其他服務器。首先訪問根域
- 根域將匹配的一級域名DNS服務器b地址傳遞給DNS服務器a
- DNS服務器a再去訪問DNS服務器b,DNS服務器b再將匹配的二級域名DNS服務器c傳遞給DNS服務器a
- DNS服務器a再去訪問DNS服務器c,重復以上3,4步驟
- DNS服務器a得到客戶端要訪問域名的ip地址,傳遞給客戶端,并留下記錄,方便以后訪問。
簡單模擬一個DNS工作
-
準備兩臺終端,客戶端,服務器。
這里我以Centos6.9為客戶端,Centos7.3位服務器。 -
在服務端安裝bind(提供DNS服務的軟件)
yum -y install bind[root@centos7 named]# yum -y install bind Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.btte.net Resolving Dependencies --> Running transaction check ---> Package bind.x86_64 32:9.9.4-50.el7_3.1 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================== Package Arch Version Repository Size ================================================================================================== Installing: bind x86_64 32:9.9.4-50.el7_3.1 updates 1.8 M Transaction Summary ================================================================================================== Install 1 Package Total download size: 1.8 M Installed size: 4.3 M Downloading packages: bind-9.9.4-50.el7_3.1.x86_64.rpm | 1.8 MB 00:00:01 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : 32:bind-9.9.4-50.el7_3.1.x86_64 1/1 Verifying : 32:bind-9.9.4-50.el7_3.1.x86_64 1/1 Installed: bind.x86_64 32:9.9.4-50.el7_3.1 Complete!`
-
關閉linux安全策略和防火墻
-
服務器–Centos7
sed -i ‘s/SELINUX=enforcing/SELINUX=permissive/g’ /etc/selinux/configiptables -F
systemctl disable firewalld
systemctl stop firewalld 客戶端–Centos6
sed -i ‘s/SELINUX=enforcing/SELINUX=permissive/g’ /etc/selinux/configchkconfig iptables off
service iptables stop檢查selinux安全策略是否修改為”允許”
cat /etc/selinux/config
-
-
啟動DNS服務器
systemctl start named
systemctl enable named啟動后確認端口開啟(端口號默認為 53)
ss -nutl -
把服務器53端口綁定在所有服務器ip上
cd -p /etc/named.conf{,.bak}
(需要修改陪指文件,建議先備份 )vim /etc/named.conf
修改listen-on port 53 {localhost; }中的localhost
改為any或0.0.0.0或將整行注釋vim /etc/sysconfig/network-scripts/ifcfg-ens33
在最后一行加上DNS1=127.0.0.1重啟服務(配置文件生效)
systemctl restart network -
在客戶端(Centos6)配置DNS,指向服務器DNS(Centos7)
vim /etc/sysconfig/network-scripts/ifcfg-eth0
最后加上DNS1=172.16.0.24(DNS)systemctl restart network
-
在服務器(Centos7)配置DNS,允許本地以外的地址訪問
vim /etc/named.conf
修改allow-query { localhost;any; };
改為any或0.0.0.0或將整行注釋
在客戶端(Centos6)嘗試連接外網
dig www.baidu.com
[root@centos6 ~]# dig www.baidu.com ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.el6_9.2 <<>> www.baidu.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 59143 ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 5, ADDITIONAL: 5 ;; QUESTION SECTION: ;www.baidu.com. IN A ;; ANSWER SECTION: www.baidu.com. 347 IN CNAME www.a.shifen.com. www.a.shifen.com. 43 IN A 61.135.169.121 www.a.shifen.com. 43 IN A 61.135.169.125 ;; AUTHORITY SECTION: a.shifen.com. 254 IN NS ns1.a.shifen.com. a.shifen.com. 254 IN NS ns3.a.shifen.com. a.shifen.com. 254 IN NS ns5.a.shifen.com. a.shifen.com. 254 IN NS ns2.a.shifen.com. a.shifen.com. 254 IN NS ns4.a.shifen.com. ;; ADDITIONAL SECTION: ns1.a.shifen.com. 254 IN A 61.135.165.224 ns2.a.shifen.com. 254 IN A 180.149.133.241 ns3.a.shifen.com. 254 IN A 61.135.162.215 ns4.a.shifen.com. 254 IN A 115.239.210.176 ns5.a.shifen.com. 254 IN A 119.75.222.17 ;; Query time: 1 msec ;; SERVER: 172.16.0.1#53(172.16.0.1) ;; WHEN: Mon Jul 24 14:16:16 2017 ;; MSG SIZE rcvd: 260
原創文章,作者:kstg5663294,如若轉載,請注明出處:http://www.www58058.com/82784