正文
准备一台机子充当dns服务器
1 dnsmasq安装
在dns服务器上安装dnsmasq
sudo apt-get -y install dnsmasq
Created symlink /etc/systemd/system/multi-user.target.wants/dnsmasq.service → /lib/systemd/system/dnsmasq.service.
Job for dnsmasq.service failed because the control process exited with error code.
See "systemctl status dnsmasq.service" and "journalctl -xe" for details.
invoke-rc.d: initscript dnsmasq, action "start" failed.
● dnsmasq.service - dnsmasq - A lightweight DHCP and caching DNS server
Loaded: loaded (/lib/systemd/system/dnsmasq.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2020-10-31 08:21:52 GMT; 4ms ago
Process: 7595 ExecStart=/etc/init.d/dnsmasq systemd-exec (code=exited, status=2)
Process: 7594 ExecStartPre=/usr/sbin/dnsmasq --test (code=exited, status=0/SUCCESS)
10月 31 08:21:52 linux89.com systemd[1]: Starting dnsmasq - A lightweight DHCP and caching DNS server...
10月 31 08:21:52 linux89.com dnsmasq[7594]: dnsmasq: syntax check OK.
10月 31 08:21:52 linux89.com dnsmasq[7595]: dnsmasq: failed to create listening socket for port 53: 地址已在使用
10月 31 08:21:52 linux89.com dnsmasq[7595]: failed to create listening socket for port 53: 地址已在使用
10月 31 08:21:52 linux89.com dnsmasq[7595]: 启动失败
10月 31 08:21:52 linux89.com systemd[1]: dnsmasq.service: Control process exited, code=exited status=2
10月 31 08:21:52 linux89.com systemd[1]: dnsmasq.service: Failed with result 'exit-code'.
10月 31 08:21:52 linux89.com systemd[1]: Failed to start dnsmasq - A lightweight DHCP and caching DNS server.
Processing triggers for ureadahead (0.100.0-21) ...
ureadahead will be reprofiled on next reboot
Processing triggers for systemd (237-3ubuntu10.42) ...
以上错误信息中发现53端口被占用了,无法Starting dnsmasq。查询一下53端口,被systemd-resolve (dns缓存服务)占用。暂时关闭掉这个服务,重启dnsmasq
#暂时关闭,下次开机会重启
$ sudo systemctl stop systemd-resolved.service
$ sudo systemctl restart dnsmasq.service
2 配置dnsmasq
参考 https://cloud.tencent.com/developer/article/1174717
2、Dnsmasq的主要作用
(1)将Dnsmasq作为本地DNS服务器使用,直接修改电脑的本地DNS的IP地址即可。
(2)应对ISP的DNS劫持(反DNS劫持),输入一个不存在的域名,正常的情况下浏览器是显示无法连接,DNS劫持会跳转到一个广告页面。先随便nslookup 一个不存在的域名,看看ISP商劫持的IP地址。
(3)智能DNS加快解析速度,打开/etc/dnsmasq.conf文件,server=后面可以添加指定的DNS,例如国内外不同的网站使用不同的DNS。
国内指定DNS
server=/cn/114.114.114.114
server=/taobao.com/114.114.114.114
server=/taobaocdn.com/114.114.114.114
国外指定DNS
server=/google.com/8.8.8.8
(4)屏蔽网页广告,将指广告的URL指定127这个IP,就可以将网页上讨厌的广告给去掉了。
address=/ad.youku.com/127.0.0.1
address=/ad.iqiyi.com/127.0.0.1
(5)指定域名解析到特定的IP上。这个功能可以让你控制一些网站的访问,非法的DNS就经常把一些正规的网站解析到不正确IP上。
address=/freehao123.com/123.123.123.123
(6)管理控制内网DNS,首先将局域网中的所有的设备的本地DNS设置为已经安装Dnsmasq的服务器IP地址。然后修改已经安装Dnsmasq的服务器Hosts文件:/etc/hosts,指定域名到特定的IP中。
例如想让局域网中的所有用户访问www.freehao123.com时跳转到192.168.0.2,添加:
192.168.0.2 www.freehao123.com
在Hosts文件中既可,整个过程也可以说是“DNS劫持”。
于是编辑/etc/hosts,最后添加一行
dns服务器ip 域名
10.0.230.89 linux89.com
重启服务使配置生效
3 使用该dns服务器
在另一台机子上指定域名解析地址为 dns服务器的ip,当然,前提是该机子能访问到dns服务器
打开终端 ping linux89.com