dnsmasq (简体中文)
Dnsmasq 提供 DNS 缓存和 DHCP 服务功能。作为域名解析服务器(DNS),dnsmasq可以通过缓存 DNS 请求来提高对访问过的网址的连接速度。作为DHCP 服务器,dnsmasq 可以为局域网电脑提供内网ip地址和路由。DNS和DHCP两个功能可以同时或分别单独实现。dnsmasq轻量且易配置,适用于个人用户或少于50台主机的网络。此外它还自带了一个 PXE 服务器。
Contents
安装
DNS 缓存设置
要在单台电脑上以守护进程方式启动dnsmasq做DNS缓存服务器,编辑/etc/dnsmasq.conf
,添加监听地址:
listen-address=127.0.0.1
如果用此主机为局域网提供默认 DNS,请用为该主机绑定固定 IP 地址,设置:
listen-address=192.168.x.x
DNS 地址文件
在配置好dnsmasq后,你需要编辑/etc/resolv.conf
让DHCP客户端首先将本地地址(localhost)加入 DNS 文件(/etc/resolv.conf
),然后再通过其他DNS服务器解析地址。配置好DHCP客户端后需要重新启动网络来使设置生效。
使用dhcpcd
dhcpcd 可以是通过创建(或编辑)/etc/resolv.conf.head
文件或 /etc/resolv.conf.tail
文件来指定dns服务器,使/etc/resolv.conf
不会被每次都被dhcpcd重写
echo "nameserver 127.0.0.1" > /etc/resolv.conf.head #设置dns服务器为127.0.0.1
使用dhclient
要使用 dhclient, 取消 /etc/dhclient.conf
文件中如下行的注释:
prepend domain-name-servers 127.0.0.1;
使用NetworkManager
Networkmanager 默认直接使用 dhcpcd 而不是 dhclient。dhcpcd 现在不再导入 /etc/resolv.conf.head
和 /etc/resolv.conf.tail
名称解析设置。有如下几种选项:
NetworkManager 配置为启动 dnsmasq
,将 dns=dnsmasq
加入 NetworkManager.conf
的 [main]
部分,并禁用 dsmasq 的开机启动:
/etc/NetworkManager/NetworkManager.conf
[main] plugins=keyfile dns=dnsmasq
如果目录不存在,需要手动创建:
mkdir /etc/NetworkManager/dnsmasq.d echo "cache-size=1000" | sudo tee /etc/NetworkManager/dnsmasq.d/cache
其他方式
Another option is in NetworkManagers' settings (usually by right-clicking the applet) and entering settings manually. Setting up will depending on the type of front-end used; the process usually involves right-clicking on the applet, editing (or creating) a profile, and then choosing DHCP type as 'Automatic (specify addresses).' The DNS addresses will need to be entered and are usually in this form: 127.0.0.1, DNS-server-one, ...
.
自定义配置
从 NetworkManager 0.9.6 开始,dnsmasq 自定义配置可以在 /etc/NetworkManager/dnsmasq.d/
中配置。
DHCP 服务器设置
dnsmasq默认关闭DHCP功能,如果该主机需要为局域网中的其他设备提供IP和路由,应该对dnsmasq 配置文件(/etc/dnsmasq.conf
)必要的配置如下:
# Only listen to routers' LAN NIC. Doing so opens up tcp/udp port 53 to # localhost and udp port 67 to world: interface=<LAN-NIC> # dnsmasq will open tcp/udp port 53 and udp port 67 to world to help with # dynamic interfaces (assigning dynamic ips). Dnsmasq will discard world # requests to them, but the paranoid might like to close them and let the # kernel handle them: bind-interfaces # Dynamic range of IPs to make available to LAN pc dhcp-range=192.168.111.50,192.168.111.100,12h # If you’d like to have dnsmasq assign static IPs, bind the LAN computer's # NIC MAC address: dhcp-host=aa:bb:cc:dd:ee:ff,192.168.111.50
启动守护进程
现在我们启动它:
# systemctl start dnsmasq
要让dnsmasq开机启动:
# systemctl enable dnsmasq
查看dnsmasq是否启动正常,检查 /var/log/messages.log
日志文件。
你需要重启 network 这样 dhcpd 会重新建立 /etc/resolv.conf
。
# /etc/rc.d/network restart
现在让我们测试一下 DNS 查询然后测量响应时间:
$ dig archlinux.org | grep "Query time"
Query time 应该会减少。
并且如果你移除 grep,你可以看到使用的服务器(Query time 下一行),现在它应该是 localhost 也就是 127.0.0.1。
测试
DNS 缓存
要测试查询速度,请访问一个 dnsmasq 启动后没有访问过的网站,执行 (dig
(位于 dnsutils 软件包):
$ dig archlinux.org | grep "Query time"
再次运行命令,因为使用了缓存,查询时间应该大大缩短。
DHCP 服务器
从一个连接到使用了 dnsmasq 的计算机的计算机,配置它使用 DHCP 自动获取 IP 地址,然后尝试连接到你平时使用的网络。
小技巧
避免 OpenDNS 重定向 Google 请求
要避免 OpenDNS 重定向所有 Google 请求到他们自己的搜索服务器,添加以下内容到 /etc/dnsmasq.conf
:
server=/www.google.com/X.X.X.X
用你的 ISP 的 DNS 服务器/路由器的 IP 替代 X.X.X.X 。
查看租约
cat /var/lib/misc/dnsmasq.leases