1. 程式人生 > >linux使用dnsmasq搭建DNS服務器

linux使用dnsmasq搭建DNS服務器

很多 listen udp 阿裏雲 文件路徑 是什麽 5.5 直接 tst

介紹:什麽是DNS服務器就不說了,至於搭建dns服務器,在linux上有很多的服務可以搭建的,比如:bind、dnsmasq、unbound....等;

直接上幹貨:

搭建環境:redhat7.0,yum源掛載的是redhat7.0的光盤;

一、Dnsmasq安裝
?? ?yum install dnsmasq -y

二、Dnsmasq配置

1、Dnsmasq的配置文件為:/etc/dnsmasq.conf

2、編輯/etc/dnsmasq.conf

找到下列參數修改或添加:

?? ?resolv-file=/etc/resolv.dnsmasq.conf ??? ??? ? //會從這個文件中尋找上遊dns服務器
?? ?strict-order ? ? ? ? ? ? ?? ??? ??? ??? ??? ? //去掉前面的#

?? ?addn-hosts=/etc/dnsmasq.hosts ? ? ? ? ? ? ? ?//在這個目錄裏面ip和域名dnsmasq?
?? ?listen-address=127.0.0.1,172.22.10.50?? ???//監聽地址

3、修改/etc/resolv.conf

? ? ? ? /etc/resolv.conf 定義了本機的dns地址(在本身不是dns服務器之前),將該文件內容註釋並添加??nameserver 127.0.0.1? ,使用本機作為dns服務器;

4、創建 /etc/resolv.dnsmasq.conf?文件並添加上遊dns服務器的地址

? ? ? ? 默認沒有??/etc/resolv.dnsmasq.conf ,需要按照dnsmasq配置文件中參數定義的路徑創建;

? ? ? ? touch /etc/resolv.dnsmasq.conf

? ? ? ? echo ‘nameserver 114.114.114.114‘ > /etc/resolv.dnsmasq.conf? ? ? ?

? ? ? ? * 以 114.114.114.114 為上遊的dns服務器;

? ? ? ? resolv.dnsmasq.conf 中設置的是真正的nameserver,可以用電信、聯通等公共的DNS

5、創建 /etc/dnsmasq.hosts 文件

? ? ? ? ? ?touch /etc/dnsmasq.hosts

? ? ? ? ? ?vim 編輯 /etc/dnsmasq.hosts 文件,添加ip對應的域名,例如:

? ? ? ? ? ? ? ? ? ? ? ? ?192.168.111.111? ? ? ?xpt.redhat.cn

? ? ? ? ? ? 這樣,當客戶端要求解析 xpt.redhat.cn 時,會解析為192.168.111.111 ;

三、Dnsmasq啟動

1、設置Dnsmasq開機啟動並啟動Dnsmasq服務:
? ? systemctl start dnsmasq.service
?? ?systemctl enable dnsmasq.service

2、netstat -tunlp | grep 53 查看Dnsmasq是否正常啟動:
?? ?# netstat -tlunp|grep 53
?? ?tcp ? ? ? ?0 ? ? ?0 0.0.0.0:53 ? ? ? ? ? ? ? ? ?0.0.0.0: ? ? ? ? ? ? ? ? ? LISTEN ? ? ?2491/dnsmasq ? ? ? ?
?? ?tcp ? ? ? ?0 ? ? ?0 :::53 ? ? ? ? ? ? ? ? ? ? ? :::
? ? ? ? ? ? ? ? ? ? ? ?LISTEN ? ? ?2491/dnsmasq ? ? ? ?
?? ?udp ? ? ? ?0 ? ? ?0 0.0.0.0:53 ? ? ? ? ? ? ? ? ?0.0.0.0: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2491/dnsmasq ? ? ? ?
?? ?udp ? ? ? ?0 ? ? ?0 :::53 ? ? ? ? ? ? ? ? ? ? ? :::
? ? ? ?

如果有防火墻的話還需要打開防火墻:

?firewall-cmd --add-service=dns? ? ? ? //允許防火墻臨時放行dns服務;

firewall-cmd --add-service=dns --permanent? ? ? ? //允許dns通過,永久生效;

#####? 到此處,一個簡單的dns服務器就搭建成功了,主要是理解配置文件中參數定義的文件路徑都是什麽作用;

可以使用 自己的windows電腦做測試,將自己windows的dns修改成搭建dns服務器的ip;

運行cmd,使用host 或者nslookup命令都可以:

? ? ? ? ? ? ? hsot???xpt.redhat.cn

? ? ? ? ? ? ? nslookup???xpt.redhat.cn

工作流程:

? ? ? ? ? windows向dns服務器發送請求解析?xpt.redhat.cn ,dns服務器接收到之後先去?/etc/dnsmasq.hosts 文件中尋找有誤匹配的ip、域名,如果有則返回域名對應的ip,如果沒有則向?/etc/resolv.dnsmasq.conf 中定義的上級dns服務器發送請求解析;

下面是對dns服務器配置進一步的深入:

1.對參數的解釋:
?? ?resolv-file?? ??? ??? ?定義dnsmasq從哪裏獲取上遊DNS服務器的地址, 默認是從/etc/resolv.conf獲取。
?? ?strict-order?? ??? ?表示嚴格按照resolv-file文件中的順序從上到下進行DNS解析,直到第一個解析成功為止。
?? ?listen-address?? ??? ?定義dnsmasq監聽的地址,默認是監控本機的所有網卡上。
?? ??? ??? ??? ??? ??? ??? ?局域網內主機若要使用dnsmasq服務時,指定本機的IP地址。
?? ?address?? ??? ??? ??? ?啟用泛域名解析,即自定義解析a記錄,如下配置為demon.com這個域名:
?? ??? ??? ??? ??? ??? ??? ?address=/demon.com/127.0.0.1 ?#訪問demon.com時的所有域名都會被解析成127.0.0.1
?? ?server?? ??? ??? ??? ?指定dnsmasq程序使用哪個DNS服務器進行解析。對於不同的網站可以使用不同的域名對應解析如下配置
?? ??? ??? ??? ??? ??? ??? ?server=/google.com/8.8.8.8 ? ?#表示對於google的服務,使用谷歌的DNS解析。
?? ?bogus-nxdomain?? ??? ?為防止DNS汙染,使用參數定義的DNS解析的服務器。註意:如果是阿裏雲服務器上配置dnsmasq要啟用此項。

############## dns服務器的優化 ################

一、應對ISP的DNS劫持
1.輸入一個不存在的域名,正常的情況下瀏覽器是顯示無法連接,DNS劫持會跳轉到一個廣告頁面。
?? ?先隨便 nslookup 一個不存在的域名,看看ISP商劫持的IP地址。?? ??? ?
2.接著編輯/etc/dnsmasq.conf文件,將:bogus-nxdomain=123.123.123.123 加入進去,
?? ?後面的IP是剛剛查詢到的DNS劫持IP地址;
3.重啟dnsmasq,再嘗試打開不存在的域名,這時瀏覽器就會顯示正常的無法連接頁面了。

二、智能DNS加快解析速度。
?? ?打開/etc/dnsmasq.conf文件,server=後面可以添加指定的DNS,例如國內外不同的網站使用不同的DNS。
?? ??? ?#國內指定DNS
?? ??? ?server=/cn/114.114.114.114
?? ??? ?server=/taobao.com/223.5.5.5
?? ??? ?server=/taobaocdn.com/114.114.114.114
?? ??? ?#國外指定DNS
?? ??? ?server=/google.com/8.8.8.8
?? ??? ?server=/cn/表示所有的cn域名都使用114這個公共DNS,
?? ??? ?
三、屏蔽網頁廣告。
?? ?將指廣告的URL指定127這個IP,就可以將網頁上討厭的廣告給去掉了。
?? ?address=/ad.youku.com/127.0.0.1
?? ?address=/ad.iqiyi.com/127.0.0.1?? ?

四、指定域名解析到特定的IP上。
?? ?這個功能可以讓你控制一些網站的訪問,非法的DNS就經常把一些正規的網站解析到不正確IP上。
?? ?address=/freehao123.com/123.123.123.123

五、內網DNS。
?? ?首先將局域網中的所有的設備的本地DNS設置為已經安裝Dnsmasq的服務器IP地址。
?? ?然後修改已經安裝Dnsmasq的服務器Hosts文件:/etc/hosts,指定域名到特定的IP中。

linux使用dnsmasq搭建DNS服務器