-
安装DNSMasq:
- 在Ubuntu系统中使用apt-get安装:
sudo apt-get install dnsmasq
- 启用PRIVIOUS和FORWARDING选项:
sudo dpkg-reconfigure dnsmasq
选择接口,勾选PRIVIOUS和FORWARDING。
- 启用防火墙规则:
sudo ufw allow out 53 sudo ufw allow out 80 sudo ufw allow out 443
- 启动服务并设置开机启动:
sudo systemctl start dnsmasq sudo systemctl enable dnsmasq
- 在Ubuntu系统中使用apt-get安装:
-
配置DNS解析:
- 创建配置文件:
sudo touch /etc/dnsmasq.conf
- 添加接口和域名映射:
# /etc/dnsmasq.conf interface= server=192.168.1.100/24 domain=example.com
- 修改resolv.conf:
sudo nano /etc/resolv.conf
添加以下行:
nameserver 127...1
- 创建配置文件:
-
设置端口转发:
- 使用iptables配置NAT:
sudo iptables -t nat -A POSTROUTING -o eth -j MASQUERADE sudo iptables -t nat -A PREROUTING -d 0.../ -j DNAT --target-type DNAT --target-ip 192.168.1.100 --source-port 80:80 sudo iptables -t nat -A PREROUTING -d 0.../ -j DNAT --target-type DNAT --target-ip 192.168.1.100 --source-port 443:443
- 保存iptables规则:
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
- 编辑防火墙配置文件:
sudo nano /etc/rc.local
添加以下行:
iptables-restore < /etc/iptables.ipv4.nat
- 使用iptables配置NAT:
-
配置DNS重写规则:
- 修改配置文件:
sudo nano /etc/dnsmasq.conf
- 添加CNAME记录:
host-record=* cname=www.example.com,192.168.1.100
- 重启DNSMasq服务:
sudo systemctl restart dnsmasq
- 修改配置文件:
-
其他规则设置:
- 添加重定向规则:
address=/etc/dnsmasq.conf redirect=www.example.com www.example.com
- 禁止某些访问:
deny=...
- 保存后重启服务:
sudo systemctl restart dnsmasq
- 添加重定向规则:
-
验证配置:
- 检查log:
sudo tail -f /var/log/dnsmasq.log
- 在浏览器中访问
http://www.example.com,确保重定向到HTTPS或指定页面。
- 检查log:
通过以上步骤,你可以配置DNSMasq来提供DNS和网络服务,确保本地网络和外部访问的域名解析和端口转发。









