安装 dnsmasq
根据你的操作系统进行安装:
在 Ubuntu 或 Debian 系统中:
sudo apt-get update sudo apt-get install dnsmasq
在 Mac OS X 中:
安装 Homebrew 并运行:
brew install dnsmasq
在 Windows 中:
下载并安装最新版本的 dnsmasq,通常可以从 GitHub 获取。
启动并设置服务
启动 dnsmasq 服务并确保其在系统启动时启用:
Ubuntu/Debian:
sudo systemctl start dnsmasq sudo systemctl enable dnsmasq
Mac OS X:
如果使用 Homebrew 安装,启动服务:
sudo brew services start dnsmasq
配置 DNS 服务器
编辑配置文件 /etc/dnsmasq.conf:
添加本地 DNS 解析:
interface= server=/local
绑定接口(如 eth):
bind-interfaces interface=eth
保存并退出。
配置 DHCP 服务器
在同一文件中添加 DHCP 配置:
分配 IP 范围:
dhcp-range=192.168.1./24,192.168.1.1
映射 MAC 到固定 IP:
dhcp-host=AA:BB:CC:DD:EE:FF,192.168.1.100
指定接口:
interface=eth
保存并退出。
启用转发代理(可选)
如果需要 dnsmasq 作为 DNS 转发代理:
在命令行运行:
dnsmasq --option=--forward-only
或在配置文件中添加:
option=--forward-only
重启服务
确保配置生效:
sudo systemctl restart dnsmasq
测试配置
使用 nslookup 或 dig 命令测试:
测试本地主机:
nslookup localhost
测试网络主机:
nslookup 192.168.1.100
验证 DHCP 配置
查看 DHCP 客户端列表:
cat /var/lib/dnsmasq/dnsmasq.leases
日志监控(可选)
检查日志以确保没有错误:
tail -f /var/log/dnsmasq.log
防火墙设置(可选)
确保防火墙允许 DNS 和 DHCP 传输:
Ubuntu:
sudo ufw allow out 53 sudo ufw allow out 67
故事结束
通过以上步骤,你应该能够成功配置并运行 dnsmasq 作为 DNS 和 DHCP 服务器,记得根据实际网络需求调整配置选项,如 IP 范围和域名解析设置。









