enWRT中DNS和DHCP配置主要在/etc/config/dhcp文件,可设置动态IP分配及静态IP、自定义DNS等
OpenWRT DNS与DHCP配置文件详解
DHCP配置文件(/etc/config/dhcp)
基本结构
OpenWRT的DHCP功能由dnsmasq服务提供,其配置文件位于/etc/config/dhcp,文件采用UCI(Unified Configuration Interface)格式,通过config块定义配置项。
LAN接口DHCP配置示例
config interface 'lan'
option type 'bridge'
option ifname 'eth0' # 物理网卡名称(如eth0、eth1等)
option proto 'static' # 静态IP或DHCP客户端模式
option ipaddr '192.168.1.1' # LAN口IP地址
option netmask '255.255.255.0' # 子网掩码
option dns '114.114.114.114 223.5.5.5' # 默认DNS服务器
DHCP范围配置
config dhcp 'lan'
option interface 'lan' # 绑定到LAN接口
option start '100' # DHCP分配起始IP
option limit '150' # 最大可分配IP数量
option leasetime '12h' # IP租期
静态IP绑定
通过host配置块为指定MAC地址设备分配固定IP:
config host 'static_device'
option ip '192.168.1.100'
option mac '00:11:22:33:44:55'
option name 'MyServer' # 可选描述
| 参数名 | 说明 | 示例值 |
|---|---|---|
| interface | 绑定的网络接口 | lan/wan/eth0 |
| start | DHCP起始IP | 100 |
| limit | 最大可分配IP数量 | 150 |
| leasetime | IP租期(s/m/h/d) | 12h |
| dns | 默认DNS服务器列表 | 8.8.8 114.114.114.114 |
DNS配置文件(/etc/config/dns)
全局DNS设置
config global
list server '8.8.8.8' # Google公共DNS
list server '8.8.4.4'
list server '114.114.114.114' # 阿里公共DNS
option cachesize '1000' # 缓存条目数
option rebindinterval '3600' # 重绑定时间(秒)
域名解析规则
config redirect
option domain 'example.com'
option target '192.168.1.200' # 将请求重定向到此IP
DNS代理配置
config filter
option name 'block_ads'
list category 'ad' # 阻止广告域名
list action 'deny'
| 参数名 | 说明 | 示例值 |
|---|---|---|
| server | 上游DNS服务器列表 | 8.8.8/114.114.114.114 |
| cachesize | DNS缓存容量 | 1000 |
| rebindinterval | 缓存刷新间隔(秒) | 3600 |
| domain | 自定义域名重定向规则 | example.com |
| category | 过滤类别(如广告/安全) | ad/malware |
关键命令与操作
服务管理命令
/etc/init.d/dnsmasq restart # 重启DNS/DHCP服务 /etc/init.d/network restart # 重启网络服务
临时DNS测试命令
ping c 3 www.baidu.com # 测试域名解析 cat /etc/resolv.conf # 查看当前DNS配置
UCI命令行配置
uci set dhcp.lan.dns='8.8.8.8 1.1.1.1' # 修改DNS服务器 uci commit dhcp # 保存配置
常见问题与解答
Q1:如何为IoT设备保留固定IP地址?
A:在/etc/config/dhcp中添加host配置块,
config host 'iot_device'
option ip '192.168.1.200'
option mac 'AA:BB:CC:DD:EE:FF'
option name 'LivingRoomLight'
保存后执行/etc/init.d/dnsmasq restart使配置生效。
Q2:为什么OpenWRT命令行无法访问外网?
A:可能原因及解决方案:
- DNS配置错误:检查
/etc/config/dhcp中的option dns设置,或编辑/etc/resolv.conf添加公共DNS(如nameserver 114.114.114.114)。 - 防火墙规则限制:执行
iptables L zone_wan_output,确保WAN出站规则为ACCEPT,若被阻断,可通过uci set firewall.@zone[1].output='ACCEPT'修正。 - 服务未运行:使用
/etc/init.d/dnsmasq status检查服务状态,必要时执行`/etc/init.d/dns
来源互联网整合,作者:小编,如若转载,请注明出处:https://www.aiboce.com/ask/230994.html