安装 dnsmasq
根据你的操作系统选择对应的安装方法:
1 Ubuntu/Debian 系统
sudo apt update sudo apt install dnsmasq
2 CentOS/RHEL 7+
sudo yum update sudo yum install dnsmasq
3 macOS
使用 Homebrew:
brew install dnsmasq
4 Windows
下载并安装 dnsmasq 的 Windows 版本,通常可以从官方网站或第三方资源处获取。
配置 dnsmasq
启动 dnsmasq 并创建配置文件:
1 启动并创建配置文件
sudo dnsmasq -C /etc/dnsmasq.conf
或者在终端中运行并按下空格进入编辑模式:
sudo dnsmasq -P /etc/dnsmasq.conf
2 按键编辑配置文件
使用文本编辑器(如 nano)编辑配置文件:
sudo nano /etc/dnsmasq.conf
配置示例:
interface=eth listen_address=192.168.1.1 dhcp-range=192.168.1.2,192.168.1.24,255.255.255.,24h domain=example.com
配置选项说明:
interface=eth:指定接口(如 eth)listen_address:设置 DNS 和 DHCP 服务器的 IP 地址dhcp-range:设置 DHCP 范围domain:设置域名
启动 dnsmasq
1 启动服务
在 Linux 中启动:
sudo systemctl start dnsmasq
设置为开机启动:
sudo systemctl enable dnsmasq
2 Windows
在 Windows 中,启动 dnsmasq 并确保其在系统启动时运行。
测试配置
1 检查 DNS 解析
测试外部域名是否能通过 dnsmasq 解析:
nslookup google.com
2 检查 DHCP 分配
查看已分配的 IP 地址:
arp -a
故障排除
1 配置错误
检查配置文件是否正确:
sudo nano /etc/dnsmasq.conf
2 服务未启动
检查服务状态:
sudo systemctl status dnsmasq
3 端口占用
检查 listen_address 是否正确:
sudo netstat -tuln | grep 53
后续操作
1 设置 DNS over HTTPS (DOH)
如果需要 DOH,配置 dnsmasq 并绑定域名:
sudo nano /etc/dnsmasq.conf
添加 DOH 配置:
doh
2 高级配置
如 DNS 过滤或绑定域名,可参考 dnsmasq 文档或第三方教程。
绑定域名(可选)
如果需要将 dnsmasq 与域名绑定(如 example.com),使用如 BIND9 或其他 DNS 绑定工具。
通过以上步骤,你可以成功安装并配置 dnsmasq,作为 DNS 和 DHCP 服务器或缓存解析器来管理你的网络。









