linux 下 安裝 dnsmasq 配置域名解析


dnsmasq_install_on_ubuntu18

1.dnsmasq 簡介

  • Dnsmasq 可以提供 DNS 緩存服務和 DHCP 服務功能,可以做正向反向 dns 解析
  • dnsmasq 輕量且易配置,適用於個人用戶或百台左右規模的集群,bind 較重,配置復雜,建議用於上千或者更大規模的集群

1.1.dnsmasq 查詢順序

1.1.1.域名記錄查詢順序

01./etc/hosts
02./etc/dnsmasq.conf 主配置文件 address 配置
03./etc/dnsmasq.d/ 子配置文件 address 配置
04.向上游 DNS 配置文件 /etc/dnsmasq.resolv.conf 指定的 NS 服務器做最后查詢

1.1.2.獲取域名 NS 服務器列表順序

01./etc/resolv.conf
02.dnsmasq
03./etc/dnsmasq.conf 主配置文件 server 配置
04.resolv-file 指定的配置文件

1.1.3.查詢 域名記錄 時,dnsmasq 查詢 NS 服務器的優先級

01.dnsmasq 主配置文件中下面的 server 參數行優先級高
02.resolv-file 文件列表中上面的優先級高

2.安裝 dnsmasq 軟件

Ubuntu18 安裝 dnsmasq

  • 某些版本的 ubuntu18 默認使用 systemd-resolved 服務管理 dns 可以停掉
# 停掉並禁用默認 dns 服務並安裝 dnsmasq
netstat -lanpt|grep 53
systemctl status systemd-resolved.service
systemctl stop systemd-resolved.service
systemctl disable systemd-resolved.service

# 清理軟連接
rm -f /etc/resolv.conf
echo "nameserver 223.5.5.5" >> /etc/resolv.conf
cat /etc/resolv.conf

# 安裝 dnsmasq
apt install dnsmasq -y
dnsmasq -v

# 啟動服務並設置開機自啟動
systemctl start dnsmasq
systemctl enable dnsmasq
netstat -lanpt|grep 53
  • 如果是 ubuntu14 系統下的托管服務可以修改以下配置文件,例如
vim /etc/resolvconf/resolv.conf.d/head
-------------------------------
172.0.0.1
223.5.5.5
-------------------------------

Centos7 安裝 dnsmasq

  • 可以直接安裝
yum install dnsmasq -y
dnsmasq -v

# 啟動服務並設置開機自啟動
systemctl start dnsmasq
systemctl enable dnsmasq
netstat -lanpt|grep 53

源碼安裝 dnsmasq

http://www.thekelleys.org.uk/dnsmasq/
http://www.thekelleys.org.uk/dnsmasq/dnsmasq-2.80.tar.gz

3.dnsmasq 配置管理

  • 系統 dns-client 獲取域名解析記錄時的順序
/etc/hosts
dns-cache
/etc/resolv.conf
  • 默認 dnsmasq 使用系統的 /etc/resolv.conf,並讀取 /etc/hosts 文件

3.1.修改系統 NS 配置

  • 作為 NS 的主機需要添加本機 127.0.0.1 地址
  • 為了防止意外可以添加幾個公網的 NS 地址,例如阿里 NS 223.5.5.5
vim /etc/resolv.conf
------------------------------
nameserver {{PRIVATE_IP}}
nameserver 127.0.0.1
nameserver 223.5.5.5
------------------------------

3.2.通用配置

  • 這些配置可以保持默認,不影響服務的啟動運行
  • 可以更具需求修改,監聽端口,日志服務等,
# 注意備份配置文件
cp /etc/dnsmasq.conf /etc/dnsmasq.conf.ori

# 修改主配置文件,重啟服務配置生效
vim /etc/dnsmasq.conf
------------------------------
# 監聽地址,可以配置 IP 地址或者接口名稱,多個地址使用英文逗號隔開,或者配置多行
# 只配置監聽 0.0.0.0,無法正常連接到 dnsmasq 獲取解析記錄
listen-address=127.0.0.1,192.168.1.2
#listen-address={{PRIVATE_IP}}

# 服務運行的網卡,可與 listen-address 結合使用,可以添加多行
# interface=eth1

# 指定服務不在哪些網卡上運行
# except-interface=eth0

# 監聽端口,默認配置
port=53

# 服務運行用戶和運行組
user=root
group=root

# 是否啟用日志,默認啟用日志保存在 /var/log/debug 中,可以手動置頂日志存儲位置
log-queries
log-facility=/var/log/dnsmasq.log

# 設置 DNS 緩存大小,單位條數
cache-size=10000

# 重啟后清空緩存
clear-on-reload

domain-needed
bogus-priv

# conf-file 選項,指定子配置文件
conf-file=/opt/dnsmasq/config/dnsmasq-server.conf

# conf-dir 選項,指定子配置文件的目錄,可以配置多行
# 支持高級語法,根據擴展名來包含或忽略配置文件,星號表示包含,不加星號表示排除
conf-dir=/opt/dnsmasq/config

# conf-dir 高級語法,包含指定目錄下除 .bak 以外的所有文件
# conf-dir=/opt/dnsmasq/config,.bak

# conf-dir 高級語法,包含指定目錄的所有以 .conf 結尾文件
# conf-dir=/opt/dnsmasq/config,*.conf
------------------------------
# 關於子配置文件的高級語法和普通寫法不建議同時使用,防止配置覆蓋

# 檢查生效的配置
egrep  -v '^$|^[#;]' /etc/dnsmasq.conf
------------------------------
listen-address=127.0.0.1,192.168.1.2
port=53
user=root
group=root
log-queries
log-facility=/var/log/dnsmasq.log
cache-size=10000
conf-file=/opt/dnsmasq/config/dnsmasq-server.conf
conf-dir=/opt/dnsmasq/config
------------------------------

# 創建自定義的配置目錄-用於配置域名解析
mkdir -p /opt/dnsmasq/config
tree /opt/dnsmasq

# 檢查配置語法
dnsmasq --test

# 重啟服務配置生效
systemctl restart dnsmasq.service

3.3.使用 dnsmasq 配置域名解析記錄

3.3.1.使用 hosts 配置格式配置域名記錄-即時生效

  • 注意:直接修改 /etc/hosts 不會即時生效,需要重啟服務
  • 特點:使用 addn-hosts 參數指定的額外 hosts 文件,修改配置即時生效,不需要重啟服務,推薦使用
# 創建用於測試的域名記錄
vim /etc/hosts
-----------------------------
1.1.1.1 111.zuiyoujie.com
-----------------------------

# 修改主配置文件,增加相關配置
vim /etc/dnsmasq.conf
-----------------------------
# for_hosts_record
# 是否要讀取 /etc/hosts 文件內容提供解析記錄,默認關閉,表示使用該文件內容
# no-hosts

# hostsdir 參數,指定存放解析記錄文件的目錄,文件名可自定義,配置格式參考 /etc/hosts 格式
hostsdir=/opt/dnsmasq/hostsdir

# addn-hosts 參數,指定存放域名解析記錄的配置文件,指定的文件可以不存在,修改內容即時生效
addn-hosts=/opt/dnsmasq/hosts/dnsmasq.hosts
addn-hosts=/opt/dnsmasq/hosts/333.zuiyoujie.com.hosts
-----------------------------

# 創建自定義的配置目錄-用於配置域名解析
mkdir -p /opt/dnsmasq/{hosts,hostsdir}
tree /opt/dnsmasq

# 創建 addn-hosts 參數指定的域名解析文件,如果文件不存在只告警不影響服務啟動
touch /opt/dnsmasq/hosts/dnsmasq.hosts

# 創建 hostsdir 參數指定的域名解析文件
cd /opt/dnsmasq/hostsdir
vim 222.zuiyoujie.com.hosts
-----------------------------
2.2.2.2 222.zuiyoujie.com
-----------------------------

cd /opt/dnsmasq/hosts
vim 333.zuiyoujie.com.hosts
-----------------------------
3.3.3.3 333.zuiyoujie.com
-----------------------------

# 檢查配置語法
dnsmasq --test

# 重啟服務配置生效
systemctl restart dnsmasq.service
  • 實例演示:
# 檢查啟動日志
-----------------------------
dnsmasq: exiting on receipt of SIGTERM
dnsmasq: started, version 2.79 cachesize 10000
dnsmasq: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth nettlehash DNSSEC loop-detect inotify
dnsmasq: read /etc/hosts - 9 addresses
dnsmasq: read /opt/dnsmasq/hosts/333.zuiyoujie.com.hosts - 1 addresses
dnsmasq: read /opt/dnsmasq/hosts/dnsmasq.hosts - 0 addresses
dnsmasq: read /opt/dnsmasq/hostsdir/222.zuiyoujie.com.hosts - 1 addresses
-----------------------------

# 解析測試
-----------------------------
root@zuiyoujie:~# nslookup 111.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   111.zuiyoujie.com
Address: 1.1.1.1

root@zuiyoujie:~# nslookup 222.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   222.zuiyoujie.com
Address: 2.2.2.2

root@zuiyoujie:~# nslookup 333.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   333.zuiyoujie.com
Address: 3.3.3.3
-----------------------------
  • 如果要支持一個域名對應多個 IP,必須使用 addn-hosts 選項,例如
vim /opt/dnsmasq/hosts/t1.zuiyoujie.com.host
-----------------------------
10.0.0.1 t1.zuiyoujie.com
10.0.0.2 t1.zuiyoujie.com
10.0.0.3 t1.zuiyoujie.com
-----------------------------

3.3.2.使用 address 參數配置域名記錄-重啟服務生效

  • address 參數支持泛解析,可用於防止域名劫持
  • address 參數,修改配置需要重啟服務
# 修改主配置文件,增加相關配置
vim /etc/dnsmasq.conf
-----------------------------
# for_address_config
# address 參數,配置域名解析地址,一行一條解析記錄,修改后重啟服務配置生效
address=/444.zuiyoujie.com/4.4.4.4

# 為了便於域名管理可以將 address 配置在子配置文件中,文件名自定義,配置內容保持正常格式即可
conf-file=/opt/dnsmasq/address/555.zuiyoujie.com.addr
#conf-dir=/opt/dnsmasq/address
conf-dir=/opt/dnsmasq/address,.bak
conf-dir=/opt/dnsmasq/address,*.conf
-----------------------------

# 創建自定義的配置目錄-用於配置域名解析
mkdir -p /opt/dnsmasq/address
tree /opt/dnsmasq

# 配置用於測試的域名記錄
cd /opt/dnsmasq/address
echo "address=/555.zuiyoujie.com/5.5.5.5" > 555.zuiyoujie.com.addr
echo "address=/666.zuiyoujie.com/6.6.6.6" > 666.zuiyoujie.com.bak
echo "address=/.zuiyoujie.com/7.7.7.7" > 777.zuiyoujie.com.conf

# 檢查配置語法
dnsmasq --test

# 重啟服務配置生效
systemctl restart dnsmasq.service
  • 實例演示:
# 檢查啟動日志
--------------------------------
dnsmasq: exiting on receipt of SIGTERM
dnsmasq: started, version 2.79 cachesize 10000
dnsmasq: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth nettlehash DNSSEC loop-detect inotify
dnsmasq: read /etc/hosts - 9 addresses
dnsmasq: read /opt/dnsmasq/hosts/333.zuiyoujie.com.hosts - 1 addresses
dnsmasq: read /opt/dnsmasq/hosts/dnsmasq.hosts - 0 addresses
dnsmasq: read /opt/dnsmasq/hostsdir/222.zuiyoujie.com.hosts - 1 addresses
--------------------------------

# 解析測試
--------------------------------
root@zuiyoujie:/opt/dnsmasq/address# nslookup 555.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   555.zuiyoujie.com
Address: 5.5.5.5      # 子配置文件解析生效

root@zuiyoujie:/opt/dnsmasq/address# nslookup 666.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

 server can t find 666.zuiyoujie.com: NXDOMAIN  ## 使用 .bak 文件,解析記錄失效

root@zuiyoujie:/opt/dnsmasq/address# nslookup 777.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   777.zuiyoujie.com
Address: 7.7.7.7      # 配置的泛域名解析生效

root@zuiyoujie:/opt/dnsmasq/address# nslookup 888.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   888.zuiyoujie.com
Address: 7.7.7.7      # 配置的泛域名解析生效
--------------------------------

3.3.3.使用 host-record 參數配置域名記錄-重啟服務生效

# 修改主配置文件,增加相關配置
vim /etc/dnsmasq.conf
-----------------------------
# for_host-record_config
host-record=ttt.zuiyoujie.com,10.10.10.10
-----------------------------

# 檢查配置語法
dnsmasq --test

# 重啟服務配置生效
systemctl restart dnsmasq.service
  • 實例演示:
# 檢查啟動日志
--------------------------------
dnsmasq: exiting on receipt of SIGTERM
dnsmasq: started, version 2.79 cachesize 10000
dnsmasq: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth nettlehash DNSSEC loop-detect inotify
dnsmasq: reading /opt/dnsmasq/config/dnsmasq.resolv.conf
dnsmasq: using nameserver 8.8.8.8#53
dnsmasq: using nameserver 4.4.4.4#53
dnsmasq: read /etc/hosts - 9 addresses
dnsmasq: read /opt/dnsmasq/hosts/333.zuiyoujie.com.hosts - 1 addresses
dnsmasq: read /opt/dnsmasq/hosts/dnsmasq.hosts - 0 addresses
dnsmasq: read /opt/dnsmasq/hostsdir/222.zuiyoujie.com.hosts - 1 addresses
--------------------------------

# 解析測試
--------------------------------
root@zuiyoujie:/opt/dnsmasq/address# nslookup ttt.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   ttt.zuiyoujie.com
Address: 10.10.10.10    ## 泛域名解析優先級低於已配置的域名記錄
--------------------------------

3.3.4.使用 cname 參數配置域名別名記錄-重啟服務生效

  • 配置域名 cname 別名記錄
# 修改主配置文件,增加相關配置
vim /etc/dnsmasq.conf
-----------------------------
# for_cname_config
cname=ccc.zuiyoujie.com,ttt.zuiyoujie.com
-----------------------------

# 檢查配置語法
dnsmasq --test

# 重啟服務配置生效
systemctl restart dnsmasq.service
  • 實例演示:
# 檢查啟動日志
--------------------------------
dnsmasq: exiting on receipt of SIGTERM
dnsmasq: started, version 2.79 cachesize 10000
dnsmasq: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth nettlehash DNSSEC loop-detect inotify
dnsmasq: read /etc/hosts - 9 addresses
dnsmasq: read /opt/dnsmasq/hosts/333.zuiyoujie.com.hosts - 1 addresses
dnsmasq: read /opt/dnsmasq/hosts/dnsmasq.hosts - 0 addresses
dnsmasq: read /opt/dnsmasq/hostsdir/222.zuiyoujie.com.hosts - 1 addresses
--------------------------------

# 解析測試
--------------------------------
root@zuiyoujie:/opt/dnsmasq/address# nslookup ccc.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

ccc.zuiyoujie.com       canonical name = ttt.zuiyoujie.com.
Name:   ttt.zuiyoujie.com
Address: 10.10.10.10
   server can t find ttt.zuiyoujie.com: NXDOMAIN
--------------------------------

3.4.配置上游 DNS

  • 配置 dnsmasq 到其他 DNS 服務器獲取解析記錄
  • 一般的主機或者服務器只能配置 2 到 3 個 DNS 服務器,dnsmasq 支持配置多個 DNS 服務器

3.4.1.使用 resolv-file 參數配置上游 DNS 服務器

  • resolv-file 指定的文件配置格式與 /etc/resolv.conf 相同
  • resolv-file 制定的文件,修改內容立即生效,不需要重啟服務,推薦使用
  • resolv-file 指定的文件可以不存在,默認從 /etc/resolv.conf 中獲取
  • 默認 dnsmasq 會查詢 /etc/resolv.conf 文件獲取 NS 服務器列表
# 修改主配置文件,增加相關配置
vim /etc/dnsmasq.conf
-----------------------------
# for_resolv_config
# no-resolv 參數,控制是否查詢 resolv 相關文件,默認關閉,即不使用上游 DNS 服務器查詢域名記錄
# no-resolv

# no-poll 參數,是否輪詢查找不同的 resolv 配置文件獲取 NS 服務器列表,默認關閉,即同時查詢所有配置的 resolv 文件
# no-poll

# resolv-file 參數,配置上游 DNS 服務器列表
resolv-file=/opt/dnsmasq/config/dnsmasq.resolv.conf

# strict-order 參數,控制是否嚴格按照 resolv 文件中的列表順序查詢,默認關閉,即將請求同時發送到所有 DNS 服務器,使用響應最快的服務器的結果
strict-order

# all-servers 參數,是否對所有注冊的 DNS 服務器發起查詢請求,選擇響應最快的服務器的結果
# all-servers
-----------------------------

# 創建自定義的配置目錄
mkdir -p /opt/dnsmasq/config
tree /opt/dnsmasq/

# 創建對應的 resolv 配置文件上游 DNS 服務器配置
vim /opt/dnsmasq/config/dnsmasq.resolv.conf
-----------------------------
# resolv_for_dnsmasq
nameserver 119.29.29.29
nameserver 180.76.76.76
-----------------------------

# 檢查配置語法,重啟服務
dnsmasq --test
systemctl restart dnsmasq.service
  • 實例演示:查看 dnsmasq 日志
dnsmasq: reading /opt/dnsmasq/config/dnsmasq.resolv.conf
dnsmasq: using nameserver 119.29.29.29#53
dnsmasq: using nameserver 180.76.76.76#53

3.4.2.使用 server 參數配置上游 DNS 服務器

  • server 參數與 resolv-file 相比,配置格式不同,但功能更多,比如支持對某個域名單獨指定獲取解析時使用的 DNS 服務器
  • server 參數不支持子配置文件
  • server 參數修改配置內容需要重啟生效
# 修改主配置文件,增加相關配置
vim /etc/dnsmasq.conf
-----------------------------
# for_resolv_config
# server 參數,配置 DNS 上游服務器
server=/baidu.com/8.8.8.8
server=/aliyun.com/223.6.6.6
server=114.114.114.114
server=/google.com/8.8.8.8

# conf-dir 高級語法,包含指定目錄的所有以 .conf 結尾文件
# conf-dir=/opt/dnsmasq/config,*.conf
-----------------------------

# 檢查配置語法,重啟服務
dnsmasq --test
systemctl restart dnsmasq.service
  • 實例演示:
# 查看 dnsmasq 啟動日志
-----------------------------
dnsmasq: exiting on receipt of SIGTERM
dnsmasq: started, version 2.79 cachesize 10000
dnsmasq: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth nettlehash DNSSEC loop-detect inotify
dnsmasq: using nameserver 8.8.8.8#53 for domain google.com 
dnsmasq: using nameserver 114.114.114.114#53
dnsmasq: using nameserver 223.6.6.6#53 for domain aliyun.com 
dnsmasq: using nameserver 8.8.8.8#53 for domain baidu.com 
dnsmasq: reading /opt/dnsmasq/config/dnsmasq.resolv.conf
dnsmasq: using nameserver 8.8.8.8#53 for domain google.com 
dnsmasq: using nameserver 114.114.114.114#53
dnsmasq: using nameserver 223.6.6.6#53 for domain aliyun.com 
dnsmasq: using nameserver 8.8.8.8#53 for domain baidu.com 
dnsmasq: using nameserver 119.29.29.29#53
dnsmasq: using nameserver 180.76.76.76#53
dnsmasq: read /etc/hosts - 9 addresses
dnsmasq: read /opt/dnsmasq/hosts/333.zuiyoujie.com.hosts - 1 addresses
dnsmasq: read /opt/dnsmasq/hosts/dnsmasq.hosts - 0 addresses
dnsmasq: read /opt/dnsmasq/hostsdir/222.zuiyoujie.com.hosts - 1 addresses
-----------------------------

# 解析測試
root@zuiyoujie:/opt/dnsmasq/address# nslookup www.baidu.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Non-authoritative answer:
www.baidu.com   canonical name = www.a.shifen.com.
Name:   www.a.shifen.com
Address: 14.215.177.38
Name:   www.a.shifen.com
Address: 14.215.177.39
www.a.shifen.com        canonical name = www.wshifen.com.
------------------------------
dnsmasq: query[A] www.baidu.com from 127.0.0.1  # 按照 /etc/resolv.conf 文件中的 NS 記錄順序
dnsmasq: forwarded www.baidu.com to 8.8.8.8  # dnsmasq 接受請求,根據主配置文件中 server=/baidu.com/8.8.8.8 中的配置,使用 8.8.8.8 劫持查詢
dnsmasq: reply www.baidu.com is <CNAME>
dnsmasq: reply www.a.shifen.com is 14.215.177.38
dnsmasq: reply www.a.shifen.com is 14.215.177.39
dnsmasq: query[AAAA] www.a.shifen.com from 127.0.0.1
dnsmasq: forwarded www.a.shifen.com to 114.114.114.114
dnsmasq: reply www.a.shifen.com is <CNAME>
dnsmasq: reply www.wshifen.com is NODATA-IPv6
------------------------------

root@zuiyoujie:/opt/dnsmasq/address# nslookup www.google.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Non-authoritative answer:
Name:   www.google.com
Address: 142.250.66.68
Name:   www.google.com
Address: 2404:6800:4005:81b::2004
------------------------------
dnsmasq: query[A] www.google.com from 127.0.0.1  # 按照 /etc/resolv.conf 文件中的 NS 記錄順序
dnsmasq: forwarded www.google.com to 8.8.8.8     # dnsmasq 接受請求,根據主配置文件中 server=/google.com/8.8.8.8 中的配置,使用 8.8.8.8 劫持查詢
dnsmasq: reply www.google.com is 142.250.4.147
dnsmasq: reply www.google.com is 142.250.4.105
dnsmasq: reply www.google.com is 142.250.4.106
dnsmasq: reply www.google.com is 142.250.4.99
dnsmasq: reply www.google.com is 142.250.4.104
dnsmasq: reply www.google.com is 142.250.4.103
dnsmasq: query[AAAA] www.google.com from 127.0.0.1
dnsmasq: forwarded www.google.com to 8.8.8.8
dnsmasq: reply www.google.com is 2404:6800:4003:c0f::69
dnsmasq: reply www.google.com is 2404:6800:4003:c0f::63
dnsmasq: reply www.google.com is 2404:6800:4003:c0f::67
dnsmasq: reply www.google.com is 2404:6800:4003:c0f::6a
  • 根據 dnsmasq 啟動和查詢日志可知:
  • 1.獲取 NS 服務器列表時,先查詢 dnsmasq 主配置文件,后查詢 resolv-file 文件列表
  • 2.查詢 域名記錄 時,dnsmasq 主配置文件中越靠后的 server 參數行優先級越高,resolv-file 文件列表中越往上優先級越高

3.5.常用配置參數說明

參數名稱 參數說明
listen-address=127.0.0.1,192.168.1.2 監聽地址,可以配置 IP 地址或者接口名稱,多個地址使用英文逗號隔開
interface=eth1 服務運行的網卡,可以添加多行,可與 listen-address 結合使用
except-interface=eth0 指定服務不在以下網卡上運行
no-dhcp-interface=eth0 指定不提供 DHCP 或 TFTP 服務的接口,僅提供 DNS 服務
port=53 偵聽端口
user=root 和 group=root 服務運行用戶及用戶組
cache-size=10000 DNS 緩存條數
clear-on-reload 重啟后清空緩存
log-queries 啟用日志記錄,默認保存在 /var/log/debug 中
log-facility=/var/log/dnsmasq.log 日志文件名
log-async=20 異步 log,可以緩解阻塞
no-negcache 不緩存未知域名緩存,默認情況下 dnsmasq 緩存未知域名並直接返回為客戶端
bogus-nxdomain=114.114.114.114 對於任何被解析到此 IP 的域名,將響應 NXDOMAIN 使其解析失效,可以多次指定,通常用於對於訪問不存在的域名,禁止其跳轉到運營商的廣告站點
domain-needed 完整的域名才向上游服務器查找,如果僅僅是主機名僅查找hosts文件
bogus-priv Never forward addresses in the non-routed address spaces.避免含有格式出錯的域名或私有 IP 地址的數據包離開你的網絡
----------------------- ---------------------------
no-hosts 不加載本地的 /etc/hosts 文件
addn-hosts= 域名解析配置文件,可以有多行,hosts 文件格式,不支持泛域名解析,修改內容重啟服務生效
conf-file=/etc/dnsmasq.more.conf 指定域名解析記錄的文件,可以配置多行,hosts 文件格式,修改內容解析立即生效,默認為 /etc/dnsmasq.more.conf 文件
conf-dir=/etc/dnsmasq.d 指定域名解析記錄的文件目錄,可以配置多行,hosts 文件格式,修改內容解析立即生效,默認為 /etc/dnsmasq.d 目錄
conf-dir=/etc/dnsmasq.d,.bak 高級語法,包含指定目錄的所有文件,但是排除以 .bak 結尾的文件
conf-dir=/etc/dnsmasq.d/,*.conf 高級語法,包含指定目錄的所有文件,且文件需要以 .conf 結尾
expand-hosts 和 domain=zuiyoujie.com 用於擴站主機名,在主機名后自動添加指定的域名,可以省略很多配置,
address=/zuiyoujie.com/192.168.1.5 泛域名配置,除非在域名規划架構相當合理的時候否則盡量少用繁泛域名解析,k8s 集群中使用泛域名也會極大的減少管理成本
----------------------- ---------------------------
no-resolv 不使用上游 DNS 服務器的配置文件 /etc/resolv.conf 或者 resolv-file 選項
no-poll 不允許 Dnsmasq 通過輪詢 /etc/resolv.conf 或者其他文件來動態更新上游 DNS 服務列表
resolv-file= 指定上游 DNS 服務器的配置文件,dnsmasq 可以從哪里獲取解析記錄,修改內容重啟服務生效
strict-order 嚴格按照 resolv.conf 文件中的順序進行查找
server=/aliyun.com/223.5.5.5 配置某個域名使用指定的 NS 進行解析

3.6.配置日志格式

vim /etc/logrotate.d/dnsmasq
------------------------------
/var/log/dnsmasq.log {
    daily
    copytruncate
    missingok
    rotate 30
    compress
    notifempty
    dateext
    size 200M
} 
------------------------------

3.7.使用 supervisor 管理 dnsmasq

  • 配置服務統一管理和宕機重啟
  • 需要讓 dnsmasq 保持前台運行
# 停止並禁用 systemctl 管理的 dnsmasq 服務
systemctl stop dnsmasq
systemctl disable dnsmasq

# 檢查服務狀態
netstat -lanpt

cd /opt/supervisord/etc
vim supervisor_dnsmasq.conf
-----------------------------
[program:dnsmasq]
directory = /opt/dnsmasq
command = /usr/sbin/dnsmasq --no-daemon --log-queries
user=root
group=root
numprocs=1
stopsignal=TERM
startretries=0
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile = /opt/supervisord/var/log/dnsmasq.log
-----------------------------

# 更新 supervisor 配置
supervisorctl update

# 啟動管理 dnsmasq
supervisorctl restart dnsmasq
supervisorctl status dnsmasq

10.參考地址:

https://www.cnblogs.com/sunsky303/p/9238669.html
https://cloud.tencent.com/developer/article/1174717
https://linux.cn/article-9438-1.html
http://www.enkichen.com/2017/05/23/dnsmasq-introduce/
https://www.jianshu.com/p/26e11c3babc2
https://linux.cn/article-9438-1.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM