簡介
- DNS服務器牽扯到家庭,企業,公司,國家等等領域,只要是互聯網不必須就要用到他
- DNS(Domain Name Server,域名服務器)是進行域名(domain name)和與之相對應的IP地址 (IP address)轉換的服務器
- DNS中保存了一張域名(domain name)和與之相對應的IP地址 (IP address)的表,以解析消息的域名
- 這一篇我講演示DNF服務器搭建
- 第二篇主要講的是根服務器和COM服務器的搭建,并和其他DNS服務器通信的演示
- 那我們開始
DNS域名和DNS域名結構
名稱 |
代表意義 |
com |
公司、行號、企業 |
org |
組織、機構 |
edu |
教育單位 |
gov |
政府單位 |
net |
網絡、通信 |
mil |
軍事單位 |
- 根域
- 服務器主要用來管理互聯網的主目錄,全世界只有13臺(這13臺根域名服務器名字分別為“A”至“M”),1個為主根服務器在美國
- 其余12個均為輔根服務器,其中9個在美國,歐洲2個,位于英國和瑞典,亞洲1個位于日本
- 一級域名:Top Level Domain: tld
- com, edu, mil, gov, net, org, int,arpa
- 一級域名中只含有一個“.”,且“.”左邊要有內容字段。一級域名又被稱為頂級域名
- 三類:組織域、國家域(.cn, .ca, .hk, .tw)、反向域
- 二級域名
- 二級域名的權重高于二級目錄,二級域名是作為一個獨立的域名出現在互聯網上,而二級目錄是以網站子頁面出現的,所以很多人認為兩者之間的權重相同,這種認識是錯誤的
- 很多人都誤把帶www當成一級域名,把其他前綴的當成二級域名,是錯誤的。正確的域名劃分為:
- .com 頂級域名
- baidu.com 一級域名
-
www.baidu.com 二級域名
- bbs.baidu .com 二級域名
- tieba.baidu .com 二級域名
- 三級域名
- 最多127級域名
- 他用于,域名的反向解析
DNS原理
- 比如說我想訪問一個網站,你的網卡指定了DNS,一般電信或聯通,鐵通等等的技術員,會給網卡配他們的DNS服務器的
- 電腦會發出一個信號,這個信號就是發給你指定的DNS服務器的,這個信號就是你想訪問的這個網站
- 但是這個DNS服務器正好里面沒有這條記錄,他會去找根服務器
- 根服務器里面存放的是所有的,頂級域名服務器的DNS,如
.com
,.cn
- 根里面就沒有這個地址,但是他會推薦你去找個哪個頂級域名結尾的服務器如
.com
結尾的DNS服務器
- 結果你電腦又去找
.com
DNS服務器,如果你是二級域名的話就找到了,如果你是三級域名那還要繼續找下去
- 但是他只是知道他的IP,并不是這個網站的服務器,你會跟著這個IP找到這個IP的DNS服務器
- 最后就是找到了,因為這個DNS服務器肯定知道他服務器的IP,最后你們就可以握手,通信了
- 不過我們電腦都會有連個DNS服務器IP,就算一個壞了我們一樣可以請求找另外一個DNS服務器
DNS解析
- DNS查詢類型:
- 遞歸查詢 :遞歸說簡單點就是,負責到底都是他一個人,獲取到最終結果
- 迭代查詢 : 迭代的意思就是,你問他問題他幫你推薦一個人叫你去問別人問題
- 名稱服務器:域內負責解析本域內的名稱的主機
- 根服務器:13組服務器
- 中9個在美國,
- 歐洲2個,位于英國和瑞典
- 亞洲1個位于日本
- 解析類型:
- FQDN --> IP
- IP --> FQDN
- 注意:正反向解析是兩個不同的名稱空間,是兩棵不同的解析樹
BIND等DNS軟件
- 提醒DNS服務的有很多種軟件
- 其中用的最多的還是BIND
- BIND是伯克利大學發布的一款軟件
- 最近新的來代替BIND的軟件,叫unbound
- 我們在這里安裝的還是BIND,首先我們來看看他的安裝包
[root@localhost ~]# yum info bind
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Available Packages
Name : bind
Arch : x86_64
Epoch : 32
Version : 9.9.4 <<<版本
Release : 37.el7
Size : 1.8 M
Repo : bash
Summary : The Berkeley Internet Name Domain (BIND) DNS (Domain Name System) server
URL : http://www.isc.org/products/BIND/ <<官方網站
License : ISC
Description : BIND (Berkeley Internet Name Domain) is an implementation of the DNS
: (Domain Name System) protocols. BIND includes a DNS server (named),
: which resolves host names to IP addresses; a resolver library
: (routines for applications to use when interfacing with DNS); and
: tools for verifying that the DNS server is operating properly.
- 我們也來看看unbound,因為剛出來市場上反應好像不是很大,因為太新
[root@localhost ~]# yum info unbound
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Available Packages
Name : unbound
Arch : x86_64
Version : 1.4.20 <<版本
Release : 28.el7
Size : 473 k
Repo : bash
Summary : Validating, recursive, and caching DNS(SEC) resolver
URL : http://www.nlnetlabs.nl/unbound/ <<官方網站
License : BSD
Description : Unbound is a validating, recursive, and caching DNS(SEC) resolver.
:
: The C implementation of Unbound is developed and maintained by NLnet
: Labs. It is based on ideas and algorithms taken from a java prototype
: developed by Verisign labs, Nominet, Kirei and ep.net.
:
: Unbound is designed as a set of modular components, so that also
: DNSSEC (secure DNS) validation and stub-resolvers (that do not run
: as a server, but are linked into an application) are easily possible.
- dnsmasq,也提供了一些簡單的DNS服務和DHCP服務
[root@localhost ~]# yum info dnsmasq
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
Name : dnsmasq
Arch : x86_64
Version : 2.66
Release : 21.el7
Size : 464 k
Repo : installed
From repo : anaconda
Summary : A lightweight DHCP/caching DNS server
URL : http://www.thekelleys.org.uk/dnsmasq/
License : GPLv2
Description : Dnsmasq is lightweight, easy to configure DNS forwarder and DHCP server.
: It is designed to provide DNS and, optionally, DHCP, to a small network.
: It can serve the names of local machines which are not in the global
: DNS. The DHCP server integrates with the DNS server and allows machines
: with DHCP-allocated addresses to appear in the DNS with names configured
: either in each host or in a central configuration file. Dnsmasq supports
: static and dynamic DHCP leases and BOOTP for network booting of diskless
: machines.
DNS服務器搭建
- 接下來我將配置DNS服務器
- 我是在
CentOS7.3
上面演示的那我們開始
- 當然在開始之前先運行下
yum repolist
查看下下yum
源是否正常
- 還要在做實驗之前先把防火墻和SElinux全給關了以免出問題
- 首先安裝
bind
,運行命令yum -y install bind
[root@localhost ~]# yum -y install bind
Total 20 MB/s | 2.8 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 32:bind-libs-9.9.4-37.el7.x86_64 1/2
Installing : 32:bind-9.9.4-37.el7.x86_64 2/2
Verifying : 32:bind-9.9.4-37.el7.x86_64 1/2
Verifying : 32:bind-libs-9.9.4-37.el7.x86_64 2/2
Installed:
bind.x86_64 32:9.9.4-37.el7
Dependency Installed:
bind-libs.x86_64 32:9.9.4-37.el7
Complete!
- 在運行
rpm -ql bind
,看一下他的文件列表
[root@localhost ~]# 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
/usr/lib/systemd/system/named-setup-rndc.service
/usr/lib/systemd/system/named.service
/usr/lib/tmpfiles.d/named.conf
/usr/lib64/bind
/usr/libexec/generate-rndc-key.sh
/usr/sbin/arpaname
/usr/sbin/ddns-confgen
/usr/sbin/dnssec-checkds
/usr/sbin/dnssec-coverage
/var/log/named.log <<日志
/var/log/named.log
/var/named
/var/named/data
/var/named/dynamic
/var/named/named.ca << 世界十三個根服務器地址
/var/named/named.empty
/var/named/named.localhost
/var/named/named.loopback
/var/named/slaves
- 我們去用命令,
cat /var/named/named.ca
進去看下
;; ADDITIONAL SECTION:
a.root-servers.net. 3600000 IN A 198.41.0.4 <<ipv4地址 這個地址在美國,可以把IP粘貼到百度上可以查出來
a.root-servers.net. 3600000 IN AAAA 2001:503:ba3e::2:30 <<ipv6地址
b.root-servers.net. 3600000 IN A 192.228.79.201
c.root-servers.net. 3600000 IN A 192.33.4.12
d.root-servers.net. 3600000 IN A 199.7.91.13
d.root-servers.net. 3600000 IN AAAA 2001:500:2d::d
e.root-servers.net. 3600000 IN A 192.203.230.10
f.root-servers.net. 3600000 IN A 192.5.5.241
f.root-servers.net. 3600000 IN AAAA 2001:500:2f::f
g.root-servers.net. 3600000 IN A 192.112.36.4
h.root-servers.net. 3600000 IN A 128.63.2.53
h.root-servers.net. 3600000 IN AAAA 2001:500:1::803f:235
i.root-servers.net. 3600000 IN A 192.36.148.17
i.root-servers.net. 3600000 IN AAAA 2001:7fe::53
j.root-servers.net. 3600000 IN A 192.58.128.30
j.root-servers.net. 3600000 IN AAAA 2001:503:c27::2:30
k.root-servers.net. 3600000 IN A 193.0.14.129
k.root-servers.net. 3600000 IN AAAA 2001:7fd::1
l.root-servers.net. 3600000 IN A 199.7.83.42
l.root-servers.net. 3600000 IN AAAA 2001:500:3::42
m.root-servers.net. 3600000 IN A 202.12.27.33
m.root-servers.net. 3600000 IN AAAA 2001:dc3::35
- 軟件包已解決裝好,接下來我啟動DNS服務器,運行命令
systemctl start named
- 在用命令
systemctl enable named
,把他設為開機啟動
[root@localhost ~]# systemctl enable named
Created symlink from /etc/systemd/system/multi-user.target.wants/named.service to /usr/lib/systemd/system/named.service.
- 既然服務已經啟動那我們看下他對應的端口號
53
端口打開沒有,運行命令 ss -nutl
,他會用兩個端口一個是TCP的53
一個是UDP的53
[root@localhost ~]# ss -nutl
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 *:15562 *:*
udp UNCONN 0 0 127.0.0.1:53 *:*
udp UNCONN 0 0 *:68 *:*
udp UNCONN 0 0 ::1:53 :::*
udp UNCONN 0 0 :::10338 :::*
tcp LISTEN 0 10 127.0.0.1:53 *:*
tcp LISTEN 0 128 *:22 *:*
tcp LISTEN 0 128 127.0.0.1:953 *:*
tcp LISTEN 0 100 127.0.0.1:25 *:*
tcp LISTEN 0 10 ::1:53 :::*
tcp LISTEN 0 128 :::22 :::*
tcp LISTEN 0 128 ::1:953 :::*
tcp LISTEN 0 100 ::1:25 :::*
- 現在服務已經啟動,但是他對應的端口是有問題的,因為綁在了我本地CentOS7.3的本地網卡,既然綁定的別人怎么訪問我的DNS服務器
- 我用我準備好的客戶機
CentOS6.9
來訪問下我的DNS服務器
- 可以用命令
telnet 172.16.253.8 53
,telnet
如果是最小化安裝是沒有的需要重新安裝
-
172.16.253.8
是我CentOS7.3
的IP,53
是指定的端口
[root@localhost ~]# telnet 172.16.253.8 53
Trying 172.16.253.8...
telnet: connect to address 172.16.253.8: Connection refused <<鏈接拒絕的意思
- 接下來我要打開端口,要打開端口的話我們要進入
/etc/named.conf
,里面去修改配置文件
- 在改之前要注意下他的所有者和組的問題,不然會出問題,組必須要有讀權限,備份配置文件的時候要注意
[root@localhost ~]# ll /etc/named.conf
-rw-r-----. 1 root named 1705 Mar 22 2016 /etc/named.conf
- 接下來我們修改配置文件,運行命令
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 { 127.0.0.1; }; << 要修改的行,或者把他注釋掉
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";
allow-query { localhost; };
/*
- 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
recursion.
- If your recursive DNS server has a public IP address, you MUST enable access
control to limit queries to your legitimate users. Failing to do so will
cause your server to become part of large scale DNS amplification
attacks. Implementing BCP38 within your network would greatly
reduce such attack surface
*/
recursion yes;
dnssec-enable yes;
dnssec-validation yes;
/* Path to ISC DLV key */
bindkeys-file "/etc/named.iscdlv.key";
managed-keys-directory "/var/named/dynamic";
- 把里面的
127.0.0.1
刪除換成localhost
,注釋掉也可以
- 修改玩以后,執行命令
systemctl reload named
,來重新讀取配置文件
- 現在在執行下
ss -nutl
,命令查看下成功了沒
[root@localhost ~]# ss -nutl
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 *:15562 *:*
udp UNCONN 0 0 172.16.253.8:53 *:*
udp UNCONN 0 0 127.0.0.1:53 *:*
udp UNCONN 0 0 *:68 *:*
udp UNCONN 0 0 ::1:53 :::*
udp UNCONN 0 0 :::10338 :::*
tcp LISTEN 0 10 172.16.253.8:53 *:*
tcp LISTEN 0 10 127.0.0.1:53 *:*
tcp LISTEN 0 128 *:22 *:*
tcp LISTEN 0 128 127.0.0.1:953 *:*
tcp LISTEN 0 100 127.0.0.1:25 *:*
tcp LISTEN 0 10 ::1:53 :::*
tcp LISTEN 0 128 :::22 :::*
tcp LISTEN 0 128 ::1:953 :::*
tcp LISTEN 0 100 ::1:25 :::*
- 已經成功,我們在用我的
CentOS6.9-1
,的機器連接下
[root@localhost ~]# telnet 172.16.253.8 53
Trying 172.16.253.8...
Connected to 172.16.253.8.
Escape character is '^]'.
- 連接成功,這樣我的
CentOS7.3
服務器就可以對外服務器了
- DNS服務配成功,呵呵這只能算是一個緩存服務器,或轉發服務器
- 第二篇我將搭建根服務器的搭建和從等等的搭建