Step 1: Install dnsmasq
-
Update your package list:
sudo apt update -
Install
dnsmasq:sudo apt install dnsmasq
Step 2: Configure dnsmasq
-
Backup the default configuration file:
sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.backup -
Edit the
dnsmasqconfiguration file:sudo nano /etc/dnsmasq.conf -
Add the following configuration to set up DHCP and DNS:
# Listen on a specific interface (e.g., eth0 or enp0s3) interface=enp0s3 # Enable DHCP dhcp-range=192.168.1.100,192.168.1.200,12h # Set the gateway (router) IP dhcp-option=3,192.168.1.1 # Set the DNS server (e.g., Google DNS) dhcp-option=6,8.8.8.8,8.8.4.4 # Enable DNS service no-resolv server=8.8.8.8 server=8.8.4.4Replace
enp0s3with your network interface name (useip ato check). Adjust the IP ranges and gateway as needed. -
Save and exit the file (
Ctrl+O,Enter,Ctrl+X).
Step 3: Restart dnsmasq
-
Restart the
dnsmasqservice to apply the changes:sudo systemctl restart dnsmasq -
Enable
dnsmasqto start on boot:sudo systemctl enable dnsmasq
Step 4: Verify the Setup
-
Check the status of
dnsmasq:sudo systemctl status dnsmasq -
Test DHCP by connecting a client device to the network. It should automatically receive an IP address in the range you specified.
-
Test DNS by pinging a domain name from a client:
ping google.com
Step 5: (Optional) Configure Firewall
If you have a firewall enabled (e.g., ufw), allow DHCP and DNS traffic:
sudo ufw allow 53/tcp # DNS
sudo ufw allow 53/udp # DNS
sudo ufw allow 67/udp # DHCP
sudo ufw allow 68/udp # DHCPStep 6: Troubleshooting
-
Check logs for errors:
sudo journalctl -u dnsmasq -
If clients are not receiving IP addresses, ensure:
- The correct network interface is configured.
- No other DHCP server is running on the network.
Resolving Port 53 Conflicts
Your system doesn’t have net-tools installed, which includes netstat. You can install it with:
sudo apt update && sudo apt install net-tools -yAfter that, run:
sudo netstat -tulnp | grep ":53 "If you prefer an alternative without installing net-tools, use:
sudo ss -tulnp | grep ":53 "This will check which process is using port 53, which is crucial for resolving the dnsmasq issue.
If systemd-resolved is using port 53, you can:
-
Disable
systemd-resolved:sudo systemctl stop systemd-resolved sudo systemctl disable systemd-resolved -
Remove the symlink:
sudo rm /etc/resolv.conf -
Create a new
resolv.conffile:sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf -
Restart
dnsmasq:sudo systemctl restart dnsmasq
This should free up port 53 for dnsmasq and allow it to run properly.