Hey everyone! In this guide, we're going to dive into how you can ensure that all devices on your network use a specific DNS server when accessing the internet through your Ubuntu 20.04 server. This is super useful if you're running something like Pi-hole, which acts as a network-wide ad blocker, or if you just want to have more control over DNS resolution across your network. Let's get started!
Understanding the Scenario
Before we jump into the how-to, let's quickly recap the situation. You've got an Ubuntu 20.04 server acting as your DHCP server, handing out IP addresses to all the devices on your network. You also have another machine running Pi-hole, which you've set up with a static IP. The goal is to make sure that all devices that connect to your network use Pi-hole as their DNS server, so all DNS queries go through it, and you can enjoy ad-free browsing and enhanced network security.
This setup is fantastic for a few reasons. First, it gives you centralized control over DNS resolution. You can block ads, malware domains, and more at the network level. Second, it simplifies management. Instead of configuring DNS settings on each device individually, you set it up once on your DHCP server, and it applies to everyone. Third, it can improve performance. A local DNS server like Pi-hole can often resolve queries faster than public DNS servers, leading to quicker browsing.
Step-by-Step Guide to Configuring Your DHCP Server
The key to forcing network clients to use a specific DNS server lies in configuring your DHCP server correctly. Here’s how to do it on Ubuntu 20.04:
1. Accessing the DHCP Server Configuration File
The DHCP server configuration file on Ubuntu is typically located at /etc/dhcp/dhcpd.conf
. You'll need to use a text editor with superuser privileges to modify it. I usually go with nano
, but feel free to use your favorite editor. Open the file by running:
sudo nano /etc/dhcp/dhcpd.conf
Important: Before making any changes, it’s always a good idea to back up the original configuration file. You can do this with a simple command:
sudo cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.bak
This creates a backup copy named dhcpd.conf.bak
in the same directory. If anything goes wrong, you can easily restore the original settings.
2. Setting the DNS Server Option
The most important part of this process is telling your DHCP server to hand out the Pi-hole’s IP address as the DNS server. To do this, you need to modify the dhcpd.conf
file and add or modify the option domain-name-servers
directive.
First, find the subnet declaration that corresponds to your network. It will look something like this:
subnet 192.168.1.0 netmask 255.255.255.0 {
...
}
Within this subnet block, you'll add the option domain-name-servers
directive. If you already have this line, simply modify it. If not, add it. For example, if your Pi-hole's IP address is 192.168.1.10
, you would add the following line inside the subnet block:
option domain-name-servers 192.168.1.10;
If you want to specify multiple DNS servers (for example, Pi-hole and a backup public DNS server), you can list them separated by commas:
option domain-name-servers 192.168.1.10, 8.8.8.8;
In this case, 192.168.1.10
is your Pi-hole, and 8.8.8.8
is Google's public DNS server.
Here’s a complete example of a subnet block with the DNS server option set:
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.100 192.168.1.200;
option routers 192.168.1.1;
option domain-name-servers 192.168.1.10;
option domain-name "yourdomain.local";
}
3. Setting the DNS Server Globally
If you want the DNS server setting to apply to all subnets, you can set the option domain-name-servers
directive outside of any subnet block, in the global configuration section. This is useful if you have a simple network setup with only one subnet. Add the following line at the top of the dhcpd.conf
file:
option domain-name-servers 192.168.1.10;
However, if you have multiple subnets with different DNS requirements, it’s better to set the option within each subnet block.
4. Saving the Changes
Once you've made the necessary changes, save the dhcpd.conf
file. If you're using nano
, you can do this by pressing Ctrl+X
, then Y
to confirm, and then Enter
to save.
5. Restarting the DHCP Server
For the changes to take effect, you need to restart the DHCP server. You can do this using the systemctl
command:
sudo systemctl restart isc-dhcp-server
To ensure that the DHCP server restarted successfully, you can check its status:
sudo systemctl status isc-dhcp-server
Look for the line that says “Active: active (running)” to confirm that the service is up and running.
6. Testing the Configuration
Now comes the fun part – testing your configuration! To make sure your clients are using the specified DNS server, you’ll need to release and renew their IP addresses. This forces them to request new DHCP leases from the server, which will include the new DNS server information.
On most operating systems, you can do this through the command line. Here’s how:
-
Windows:
ipconfig /release ipconfig /renew
-
macOS:
sudo ipconfig set en0 DHCP
(Replace
en0
with your network interface, if necessary.) -
Linux:
sudo dhclient -r <interface> sudo dhclient <interface>
(Replace
<interface>
with your network interface, such aseth0
orwlan0
.)
After renewing the IP address, you can check the DNS server settings on the client. On Windows, use the ipconfig /all
command and look for the “DNS Servers” entry. On macOS and Linux, you can check the /etc/resolv.conf
file (though this file may be managed by a network manager and not directly reflect the DHCP settings).
Another way to test is by using the nslookup
or dig
command to query a domain name and see which DNS server is used. For example:
nslookup google.com
The output should show your Pi-hole’s IP address as the server used for the query.
Alternative Methods and Advanced Configurations
While configuring the DHCP server is the most common way to force DNS server usage, there are a few alternative methods and advanced configurations you might want to consider.
1. Router Configuration
Many home routers also act as DHCP servers and allow you to set the DNS server. The process is similar: you log into your router’s web interface, find the DHCP settings, and specify the DNS server IP address. This method is straightforward if your router is handling DHCP, but it’s less flexible than using a dedicated DHCP server like the one on your Ubuntu machine.
2. Static IP Configuration
On individual devices, you can set a static IP address and manually specify the DNS server. This gives you the most control over DNS settings, but it’s also the most time-consuming, especially if you have many devices. It’s best suited for devices that need a fixed IP address anyway, like servers or printers.
3. DHCP Reservations
If you want certain devices to use a different DNS server than the rest of the network, you can use DHCP reservations. This allows you to assign a specific IP address and DNS server to a device based on its MAC address. In the dhcpd.conf
file, you can create a host declaration within the subnet block:
host specialdevice {
hardware ethernet 00:11:22:33:44:55;
fixed-address 192.168.1.50;
option domain-name-servers 8.8.8.8;
}
This example assigns the IP address 192.168.1.50
and the DNS server 8.8.8.8
to the device with the MAC address 00:11:22:33:44:55
.
Troubleshooting Common Issues
Sometimes, things don’t go as planned. Here are some common issues you might encounter and how to troubleshoot them:
1. Clients Not Getting New DNS Settings
If clients aren’t picking up the new DNS settings, the first thing to do is make sure you’ve restarted the DHCP server after making changes to the dhcpd.conf
file. Also, ensure that clients are releasing and renewing their IP addresses. If that doesn’t work, try restarting the client device.
2. DNS Resolution Problems
If clients are getting the correct DNS server IP but still can’t resolve domain names, there might be an issue with the DNS server itself (e.g., Pi-hole). Check the DNS server’s logs for errors and make sure it’s properly configured to forward queries to upstream DNS servers if needed.
3. Firewall Issues
Firewall rules can sometimes interfere with DNS resolution. Ensure that your firewall isn’t blocking DNS traffic (port 53) between clients and the DNS server.
4. Syntax Errors in dhcpd.conf
A single syntax error in the dhcpd.conf
file can prevent the DHCP server from starting or applying the configuration. Use the dhcpd -t
command to test the configuration file for errors:
sudo dhcpd -t
This command checks the syntax and reports any errors it finds.
Conclusion
Forcing network clients to use a specific DNS server on Ubuntu 20.04 is a powerful way to control DNS resolution and enhance network security. By configuring your DHCP server to hand out the desired DNS server IP address, you can ensure that all devices on your network use it. Whether you’re running Pi-hole for ad blocking or just want more control over DNS, this setup is a game-changer.
We’ve covered everything from the basic configuration steps to alternative methods and troubleshooting tips. Now, you’re well-equipped to set up your network exactly the way you want it. Happy networking, and feel free to reach out if you have any questions!