When trying to configure a local DNS server or a specific DNS server in an Ubuntu environment, you may encounter issues where changes to the /etc/resolv.conf file are not applied or revert after a reboot. This is because the systemd-resolved service manages DNS settings.
There are several methods to configure DNS servers in an environment managed by systemd-resolved.
1. Unlink /etc/resolv.conf Symlink and Edit Directly
This is the most direct method, but requires caution as it removes the file from systemd-resolved management.
First, confirm that /etc/resolv.conf is a symbolic link managed by systemd-resolved.
ls -l /etc/resolv.conf
# Example: lrwxrwxrwx 1 root root 39 Apr 9 10:00 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
Unlink the symbolic link and create a new file.
sudo rm /etc/resolv.conf
sudo nano /etc/resolv.conf
When the file opens in the nano editor, write the DNS server IP addresses as follows:
# /etc/resolv.conf
nameserver 192.168.1.100 # Local DNS server IP address
nameserver 8.8.8.8 # Fallback public DNS (e.g., Google Public DNS)
Save the file and exit the editor.
2. Configure DNS Server Using netplan (Recommended)
Starting from Ubuntu 18.04 LTS, netplan is recommended for network configuration management. Using netplan, you can manage network interfaces, DNS servers, and other settings in a centralized manner through YAML configuration files.
The netplan configuration files are typically located in the /etc/netplan/ directory (e.g., 01-network-manager-all.yaml or 50-cloud-init.yaml). Edit an existing file or create a new one.
sudo nano /etc/netplan/00-custom-dns.yaml
Write the file content as follows. Replace the interface name under ethernets (e.g., enp0s3 or eth0) with the one for your environment.
# /etc/netplan/00-custom-dns.yaml
network:
version: 2
renderer: networkd # or NetworkManager
ethernets:
enp0s3: # Replace with your interface name
dhcp4: yes # When using DHCP
# Or for static IP configuration
# addresses: [192.168.1.10/24]
# gateway4: 192.168.1.1
nameservers:
addresses: [192.168.1.100, 8.8.8.8] # DNS server IP addresses
search: [yourdomain.local] # Search domain (optional)
After saving the configuration file, apply the settings with the following commands:
sudo netplan try
# If the configuration is correct, press Enter to apply
sudo netplan apply
This causes systemd-resolved to read the netplan configuration and properly set the DNS servers.
3. Edit the systemd-resolved Configuration File
You can also edit the systemd-resolved configuration file /etc/systemd/resolved.conf.
sudo nano /etc/systemd/resolved.conf
Uncomment the following lines and enter the DNS server IP addresses you want to configure.
# /etc/systemd/resolved.conf
[Resolve]
#DNS=
#FallbackDNS=
#Domains=
#DNSSEC=no
#DNSOverTLS=no
#MulticastDNS=no
#LLMNR=no
#Cache=yes
#DNSStubListener=yes
#ReadEtcHosts=yes
DNS=192.168.1.100 8.8.8.8
#FallbackDNS=
#Domains=yourdomain.local
After saving the changes, restart the systemd-resolved service.
sudo systemctl restart systemd-resolved
Using any of these methods, you can properly configure a local DNS server in an Ubuntu environment. The netplan method is generally recommended.