dnsmasq設定 - money168/cobbler GitHub Wiki
mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bk
vim /etc/dnsmasq.conf
-
DNS Setting
#監聽的埠,dns預設53埠,如果設定為0,則完全禁止DNS功能
port=53
從不轉發格式錯誤的功能#正確的域名格式才轉發
domain-needed
從不轉發不在路由地址中的功能
bogus-priv
#bogus-nxdomain 為防止DNS汙染,使用引數定義的DNS解析的伺服器。
注意:如果是阿里雲伺服器上配置dnsmasq要啟用此項。
#bogus-nxdomain=114.114.114.114
resolv-file配置Dnsmasq對外上層的DNS伺服器,如果不開啟就使用主機預設的/etc/resolv.conf裡的nameserver,
指定其他檔。
resolv-file=/etc/dnsmasq.d/upstream_dns.conf
resolv-file=/etc/dnsmasq.resolv.conf
resolv-file=/etc/resolv2.conf
依照resolv.conf中的DNS Server順序進行查詢。
strict-order
不使用上層nameserver配置檔案(/etc/resolv.conf或resolv-file)
no-resolv
當/etc/resolv.conf或resolv-file檔案變化,不重新載入
no-poll
server參數是指定網域使用特定DNS伺服器進行解析。
server=/localnet/192.168.1.1
對於localnet的服務,使用內部的DNS解析。
server=/google.com/8.8.8.8
對於google的服務,使用google的DNS解析。
opendns server
server=208.67.222.222
server=208.67.220.220
大陸指定DNS
server=/cn/114.114.114.114
server=/taobao.com/114.114.114.114
server=/taobaocdn.com/114.114.114.114
server=/cn/表示所有的cn域名都使用114這個公共DNS,
server=/taobao.com/表示所有的taobao.com域名都用114,
國外指定DNS
server=/google.com/223.5.5.5
223.5.5.5 是阿里雲的公共DNS,你可以換成其它的。
設置一個反向解析,所有192.168.3.0/24的位址都到192.168.1.3去解析
#server=/3.168.192.in-addr.arpa/192.168.1.3
限定只有內部才解析的域名,但不影嚮其它域名,限制域名會在/etc/hosts或dchp進行查詢
local=/localnet/
local=/sample.com/
address 自定義解析a記錄,強制解析到你定的位址
如下配置為sample.com這個域名:
address=/sample.com/127.0.0.1
訪問sample.com時的所有域名都會被解析成127.0.0.1*
同上,還支持ipv6
#address=/www.sample.com/fe80::20d:60ff:fe36:f83
指定網域解析到特定的IP上。這個功能可以讓控制網站的訪問,非法的DNS就經常把一些正規的網站解析到不正確IP上。
address=/baidu.com.com/123.123.123.123
遮蔽網頁廣告。
將指廣告的URL指定127.0.0.1,可以將廣告給去掉。
address=/ad.youku.com/127.0.0.1
address=/ad.iqiyi.com/127.0.0.1
增加查詢yahoo google和它們的子功能變數名稱到vpn、search查找
#ipset=/yahoo.com/google.com/vpn,search
你還可以控制Dnsmasq和Server之間的查詢從哪個網卡出去
- server=10.1.2.3@eth1
指定源位址攜帶10.1.2.3位址和192.168.1.1的55埠進行通訊
如果你想Dnsmasq監聽某個埠為dhcp、dns提供服務
#interface=
你還可以指定哪個埠你不想監聽
#except-interface=
定義dnsmasq監聽的地址,預設是監控本機的所有網絡卡上。區域網內主機若要使用dnsmasq服務時,指定本機的IP地址。本機要使用寫上127.0.0.1。
listen-address=127.0.0.1,192.168.1.1
如果你想在某個埠只提供dns服務,則可以進行配置禁止dhcp服務
#no-dhcp-interface=
#bind-interfaces
#不使用/etc/hosts,開啟後expand-hosts不生效
no-hosts
#配置本地解析的hosts,使用/etc/hosts進行解析,對應no-hosts
addn-hosts=/etc/hosts
addn-hosts=/etc/dnsmasq.hosts
在dnsmasq中自定義域名。可以將hosts設定主機自動加上自定義域名。
expand-hosts
domain=sample.com
dnsmasq會自動轉換成完整域名,host2→host2.sample.com
#自動載入目錄配置
conf-dir=/etc/dnsmasq.d
使用副檔名來包含或忽略配置文件。星號表示包含,不加星號表示排除。
conf-dir=/etc/dnsmasq.d/, *.conf, *.foo
conf-dir=/etc/dnsmasq.d/, .old, .bak, .tmp
設置dns緩存大小,預設為150,提高速度
cache-size=500
#開啟log
log-facility=/var/log/dnsmasq.log
#dns log
log-queries
#dhcp log
log-dhcp
#啟用非同步日誌記錄,緩解阻塞,提高效能。預設佇列長度為5,合理值為5-25,最大限制為100
log-async=20
#DHCP Setting
指定dhcp的一個子網域
#domain=sample.com,192.168.2.0/24
同上,不過子域是一個範圍
#domain=sample.com,192.68.3.100,192.168.3.200
dhcp分發ip的範圍,以及每個ip的租約時間
#dhcp-range=192.168.0.50,192.168.0.150,12h
同上,不過給出了遮罩
#dhcp-range=192.168.0.50,192.168.0.150,255.255.255.0,12h
#Dnsmasq is installed on a LAN router with both wired and wireless interfaces at 192.168.1.10 and 192.168.2.10.
#定義不同區段分發ip的範圍
dhcp-range=lan,192.168.1.100,192.168.1.200
dhcp-range=wifi,192.168.2.100,192.168.2.200
#定義不同區段gateway
dhcp-option=lan,3,192.168.1.50
dhcp-option=wifi,3,192.168.2.50
#定義不同區段DNS server
dhcp-option=lan,6,192.168.1.10
dhcp-option=wifi,6,192.168.2.10
dnsmasq —test
配定檔測試。
dnsmasq 在 kubernetes中使用,如果你的 kubernetes 域名是sample.com
在dnsmasq中設定
address=/sample.com/192.168.1.1
可以讓dnsmasq解析所有sample.com請求。
dnsmasq直接執行 參數前要接—
dnsmasq —no-daemon —log-queries
前景執行顯示輸出狀況,並保留日誌
檢查/var/log裏所有檔案中,有包含dnsmasq的字串,不包含dist-upgrade檔案
grep -ir —exclude-dir=dist-upgrade dnsmasq /var/log/
使用grep —exclude-dir= 時,不用指定完整路徑,只需檔案名稱。
dnsmasq —no-daemon —log-queries —log-facility=/var/log/dnsmasq.log
指定專用文件
,設定檔為 log-facility=/var/log/dnsmasq.log
使用副檔名來包含或忽略配置文件。星號表示包含,不加星號表示排除。
conf-dir=/etc/dnsmasq.d/, *.conf, *.foo
conf-dir=/etc/dnsmasq.d, .old, .bak, .tmp
可以將主機配置設定於多個檔案中。
—addn-hosts=
dnsmasq預設讀取/etc/hosts。
可以在/etc/hosts,寫主機名,
127.0.0.1 localhost
192.168.0.1 host2
192.168.0.2 host3
192.168.0.3 host4
在dnsmasq中設定域名。
expand-hosts
domain=sample.com
dnsmasq會自動轉換成完整域名,host2→host2.sample.com
-—
dnsmasq 在 kubernetes中使用,如果你的 kubernetes 域名是sample.com
在dnsmasq中設定
address=/sample.com/192.168.0.5
可以讓dnsmasq解析所有sample.com請求。
====
https://www.linux.com/learn/dnsmasq-easy-lan-name-services
使用自訂conf
vim /etc/dnsmasq.d/address.conf
- 指定dnsmasq為預設查詢的上游伺服器。
server=8.8.8.8
server=8.8.4.4
- 把所有.cn的名稱全部通過114.114.114.114 DNS伺服器來解析
server=/cn/114.114.114.114
- 給*.apple.com和taobao.com使用專用的DNS
server=/taobao.com/223.5.5.5
server=/.apple.com/223.5.5.5
- 把www.sample.com解析到特定的IP
address=/www.sample.com/192.168.101.107
sample.com相當於*.sample.com
address=/sample.com/192.168.101.107
注:也可以直接添加到/etc/dnsmasq.conf中,但/etc/dnsmasq.d/*.conf的優先順序大於/etc/dnsmasq.conf。
==
Dnsmasq性能優化
將自定義設定檔放置在共用記憶體裡的檔,比如/dev/shm/dnsrecord.txt
由於記憶體的非持久性,重啟就消失,可以定期同步硬碟上的某個內容到記憶體檔中。
配置dnsmasq
vim /etc/dnsmasq.conf
-—-
no-hosts
addn-hosts=/dev/shm/dnsrecord.txt
-—-
解決同步問題
- 開機啟動
$ echo “cat /etc/hosts > /dev/shm/dnsrecord.txt” >>/etc/rc.local
- 定時同步內容
$ crontab -e
*/10 * * * * cat /etc/hosts > /dev/shm/dnsrecord.txt
==
Dnsmasq選擇最快的上游DNS伺服器
Dnsmasq伺服器配罝多個上層伺服器,轉發本地的dns請求,
實際上Dnsmasq只挑了一個上層dns伺服器來查詢並轉發結果,
這樣如果選錯伺服器的話會導致DNS回應變慢。
解決方法
vim /etc/dnsmasq.conf
-—-
all-servers
server=8.8.8.8
server=219.141.136.10
-—-
all-servers表示對以下設置的所有server發起查詢,選擇回應最快的作為查詢結果返回。
https://www.hi-linux.com/posts/30947.html#dnsmasq-china-list項目
dnsmasq-china-list項目
dnsmasq-china-list專案維護了一張國內常用但是通過國外DNS會解析錯誤的網站功能變數名稱的清單,
保證List中的國內功能變數名稱全部走國內DNS伺服器解析。
項目位址: https://github.com/felixonmars/dnsmasq-china-list
dnsmasq-china-list使用
取消dnsmasq.conf裡conf-dir=/etc/dnsmasq.d這一行的注釋
獲取專案檔案
$ git clone https://github.com/felixonmars/dnsmasq-china-list.git
將accelerated-domains.china.conf, bogus-nxdomain.china.conf,google.china.conf(可選)
放到/etc/dnsmasq.d/目錄下(如目錄不存在則建立一個)。
將dnsmasq-update-china-list放到/usr/bin/,這是一個批量修改DNS伺服器的工具(可選)。