july
july
发布于 2021-10-04 / 1254 阅读
0

dnsmasq搭建本地dns服务

dnsmasq搭建本地dns服务

dnsmasq

安装dnsmasq

sudo aptitude install dnsmasq

相关命令

#dnsmasq 帮助手册
main 8 dnsmasq
#查看状态
sudo systemctl status dnsmasq
#启动dnsmasq
sudo systemctl start dnsmasq
#重启dnsmasq
sudo systemctl restart dnsmasq
#设置开机启动
sudo systemctl enable dnsmasq
#取消开机启动
sudo systemctl disable dnsmasq

配置dnsmasq

sudo nano /etc/dnsmasq.conf

# 取消注释#port=5353,dnsmasq默认端口为5353
port=5353

# 取消注释#resolv-file= 配置上游DNS
# 不注释默认使用/etc/resolv.conf,但每次这个都会被重写,配置麻烦。
resolv-file=/etc/my_dns.conf

#默认情况下,dnsmasq将向任何上游服务器发送查询
#取消注释#strict-order
#强制dnsmasq每次根据resolv-file=下的配置文件顺序查询
strict-order

#取消注释listen-address=
#添加监听地址,127.0.0.1本机解析
#添加192.16.0.11为局域网其他提供DNS解析服务
listen-address=127.0.0.1,192.168.0.11

#取消注释cache-size= 设置缓存大小(条),
cache-size=10000

#取消注释log-queries 每个DNS查询通过时记录其日志
log-queries
log-facility=/var/log/runit/dnsmasq/dnsmasq.log

#取消注释conf-dir=/etc/dnsmasq.d,加载其他配置
conf-dir=/etc/dnsmasq.d
conf-dir=/etc/dnsmasq.d/,*.conf

配置上游DNS


sudo nano /etc/my_dns.conf

nameserver 127.0.0.1
#114DNS
nameserver 114.114.114.114
nameserver 114.114.115.115

#阿里DNS
nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 2400:3200::1
nameserver 2400:3200:baba::1

#谷歌DNS
nameserver 8.8.8.8
nameserver 8.8.4.4

dnsmasq配置去广告

下载配置文件 anti-AD
把下载好的文件放在/etc/dnsmasq.d 目录下

cd /etc/dnsmasq.d
sudo wget https://gitee.com/privacy-protection-tools/anti-ad/raw/master/anti-ad-for-dnsmasq.conf
#重启下服务
sudo systemctl restart dnsmasq

dnsmasq配置日志分割

sudo nano /etc/logrotate.d/dnsmasq
/var/log/runit/dnsmasq/dnsmasq.log {
        notifempty
        daily
        dateext
        rotate 58
        sharedscripts
        postrotate
        [ ! -f /var/run/dnsmasq/dnsmasq.pid ] || kill -USR2 `cat >
        endscript
}

#检验配置是否正确
sudo logrotate -d dnsmasq

参考文章

  1. dnsmasq
  2. 树莓派搭建 DNS 服务器
  3. 利用dnsmasq 屏蔽广告和跟踪
  4. 利用Dnsmasq部署DNS服务
  5. Linux安装DNSmasq搭建自己的公共DNS
  6. Dnsmasq安装与配置
  7. Dnsmasq 进阶技巧
  8. dnsmasq日志分割案例

使用体验

感知不强,pc端设置后拦截广告,但手机端没有任何效果 date:Thu 7 Oct 21:10:53 CST 2021