Dnsmasq 是一个轻量级的,易配置的 DNS 转发器和 DHCP 服务器。 它旨在为小型网络提供 DNS 和 DHCP 服务。 它还可以用来解析那些公网上没有的,本地网络的主机名称的 IP 地址。 DHCP 服务器整合了一个 DNS 服务器,以及一个用来分配地址 DHCP 分配器;不管 DNS 的解析名字是配置在各个主机里,还是统一配置在一个文件里。 Dnsmasq 可以支持动态或者静态的地址分配,以及用于 BOOTP 这样无盘系统的场景下。 OpenWrt 内置,且默认配置了该软件。
配置方法可以使用统一的配置文件 /etc/config/dhcp,不过你也可以同时在 /etc/dnsmasq.conf 文件里配置。
只要在 uci 的配置文件里做了相应的设置,你也可以使用 /etc/ethers 和 /etc/hosts 进行另一些参数的配置。
→ /etc/config/dhcp 采用了统一配置接口文件,那些在统一配置命令的特定功能 uci,几乎都可以在这个文件里配置出来。
统一的配置文件“/etc/config/dhcp”也可以跟“/etc/dnsmasq.conf”同时配置使用。 而“dnsmasq.conf”文件默认是不存在的,但是当 dnsmasq 软件启动的时候,如果发现有该文件,那么它就会去处理。 注意,在配置文件最终转换成 dnsmasq 配置命令参数的时候,“/etc/config/dhcp”文件的配置参数优先级高,相同参数的配置会覆盖“dnsmasq.conf”文件里配置参数。
例如:
默认情况下,Dnsmasq 可以将你的主机解析到 .lan 的域名下
这个可以在配置文件里就指定了:
# allow /etc/hosts and dhcp lookups via *.lan local=/lan/ domain=lan
You can change this to whatever you'd like your home domain to be.
Also, if you want your hosts to be available via your home domain without having to specify the domain in your /etc/hosts file, add the expand-hosts directive to your /etc/dnsmasq.conf file.
As an example, without expand-hosts, you can only reach router, ubuntu-desktop and ubuntu-laptop.
With expand-hosts on, you can reach router, router.lan, ubuntu-desktop, ubuntu-desktop.lan, etc.
This probably matches what you're looking for anyway.
Without this setting, you'll have to add .lan entries to your /etc/hosts.
中文翻译: 在这个参数里你可以随便定义自己的主机域名。 同样的,如果你想让你定义的域名访问到你的本地机器,又不想在“/etc/hosts”文件里添加域名解析信息,你也可以直接在“/etc/dnsmasq.conf”里添加“expand-hosts”参数设定。
举例说,如果不使用“expand-hosts”选项设置,你就只可以访问到 router, ubuntu-desktop and ubuntu-laptop。 而如果使用了“expand-hosts”选项,那么你可以访问到除了router, ubuntu-desktop and ubuntu-laptop,还有 router.lan, ubuntu-desktop.lan等等。 这个特点可能正是你想要的。
如果不用这个选项来设置的话,你要达到同样的效果,就需要你在 /etc/hosts 文件里配置 .lan 的域名条目了。
可以在 /etc/ethers 中分配静态地址。
详情请参考→ static_leases.
在 /etc/hosts 文件中配置 DNS 的解析条目。
Dnsmasq 软件将使用这些条目来响应来自网络的 DNS 查询请求。
DNS 条目的编写格式如下:
Format:
[IP地址] 主机名称 主机名称缩写 ...
例如:
192.168.1.1 router OpenWrt localhost 192.168.1.2 debian-server 192.168.1.3 ubuntu-laptop
Sometimes when an interface is on the edge of the capacity (especially WiFi over longer distances) a DHCP request could be not replied in time. Therefore the DHCP client will not be able to receive proper network settings. A possible workaround is using static IPs or very long DHCP leases (more than 12h). This is particularly important when one has several WiFi repeaters that use DHCP and are distant from each other or not easily accessible.
中文翻译: 有些时候,当一个网络接口快达到它的处理能力的时候(特别是因为 WiFi 因为超出其稳定信号的范围),DHCP 的请求可能不会及时响应。 因此 DHCP 客户端就没法收到网络地址的配置。 一种折中的解决办法设置静态 IP 地址,或者加长 DHCP 租用时间(比如大于12小时)。 这在存在很多 WiFi 中继的场景中很重要,因为它们之间很远或者连接不稳定。
Windows 7 among others ask for proxy settings using DHCP. The issue is that they do not stop asking until they have received an answer. This results in that the log contains a lot information about these requests, an example can be found below (thanks to the excito wiki for the info).
解决方法如下:
uci add_list dhcp.lan.dhcp_option='252,"\n"'
uci commit dhcp
service dnsmasq restart
Windows 7 包含一个新的 Microsoft-enhanced 功能。 系统不会把一个之前已经由 DHCP 服务器分配给另外一个接口(对应网卡)的 IP 地址,再配置给当前接口;即使是这个接口现在已经关闭了(比如该接口的网线已经拔了)。 这种行为是独一无二的,在旧版本的 Windows、Mac OS、Linux 中都未报告过。
如果你尝试在你的路由器里配置 MAC 地址绑定和解绑定的功能,Windows 7 客户端就会陷入无休止的 DORA 循环请求。
Solution:
/etc/config/dhcp中文翻译:
解决方案如下:
uci add dhcp host uci set dhcp.@host[-1].name="example-host" uci set dhcp.@host[-1].ip="192.168.1.230" uci set dhcp.@host[-1].mac="00:a0:24:5a:33:69 00:11:22:33:44:55 02:a0:24:5a:33:69 02:11:22:33:44:55" uci commit dhcp service dnsmasq restart
If you use Adguard DNS as forwarder (to have a cheap and efficient network adblocker), you need to disable Rebind protection, to avoid lag or site unreachable due to Rebin protection.
If not, you can see lot of this log in system.log, and have lag or host unreachable issue.
daemon.warn dnsmasq[xxx]: possible DNS-rebind attack detected: any.adserver.dns