How to setup DNS manually on Linux?
In Linux /etc/resolv.conf
get often overwritten when we setup the DNS, because of the multitude of programs managing the DNS servers, making a proper setup complicated sometimes.
DNS Config Under Linux
DNS usage on linux is done over a set of routines in the C library that provide access to the Internet Domain Name System (DNS). The resolver configuration file (resolv.conf
) contains information that is read by the resolver routines the first time they are invoked by a process. In short each process requesting DNS will read /etc/resolv.conf
over library. The NSS is layered on top of this, and is configured by /etc/nsswitch.conf
.
Linux DNS config are located in the file /etc/resolv.conf
BUT there are a number of programs/services that wants to automatically manage and handle the DNS configuration file at /etc/resolv.conf
. In some situations you may want to manage this file yourself. Each program/service managing DNS have its own configuration files like /etc/dnsmasq.conf
(for dnsmasq service) and append the DNS config at connection change and/or on other events… a quick solution is to lock the DNS config file with chattr +i /etc/resolv.conf
but this is not recommended in certain case, a better solution is to setup correctly all the program/services using the DNS like (dnsmasq/network-manager/resolvconf/etc.)
Getting Back The Control Of DNS
Here is an exhaustive list of setups to get back the control of resolv.conf and avoid having it overwritten (how to disable/setup DNS from other location other than resolv.conf) note that resolvconf is an independent program from resolv.conf, also depending on your system/config you may not have one or many of the programs listed here.
1. Resolvconf:
Config files
cat /etc/resolvconf/resolv.conf.d/head
nameserver 8.8.4.4
cat /etc/resolvconf/resolv.conf.d/base
nameserver 8.8.4.4
Update the config
sudo resolvconf -u
Disable resolvconf
systemctl disable --now resolvconf.service
2. Dnsmasq Service:
Config files
cat /etc/dnsmasq.conf
server=1.1.1.1
server=8.8.4.4
Update the config
sudo systemctl restart dnsmasq.service
3. Network Manager:
Config files
/etc/NetworkManager/*
Disable DNS
$ cat /etc/NetworkManager/conf.d/no-dns.conf
[main]
dns=none
Enable DNS
$ cat /etc/NetworkManager/conf.d/dns.conf
[main]
dns=default
[global-dns]
searches=example.com
[global-dns-domain-*]
Use resolved service
$ cat /usr/lib/NetworkManager/conf.d/resolved.conf
[main]
dns=systemd-resolved
Use resolvconf
$ cat /usr/lib/NetworkManager/conf.d/resolvconf.conf
[main]
rc-manager=resolvconf
Update the config
systemctl restart NetworkManager.service
4. Network Interfaces:
Config files
$ cat /etc/network/interfaces
#nameservers
# or dns-search like so
# dns-search x.y
dns-nameservers 4.4.4.4 8.8.8.8
Update The Config
reboot
5. DHCP Client:
Config files
$ cat /etc/dhcp3/dhclient.conf
supersede domain-name-servers <dns_ip_address1>,<dns_ip_address2>;
Update The Config
reboot
6. Rdnssd Service:
Disable rdnssd
systemctl disable --now rdnssd.service
7. Resolved Service:
Disable resolved
systemctl disable --now systemd-resolved.service
8. Netconfig:
Config files
/etc/sysconfig/network/config
Disable netconfig
cat /etc/sysconfig/network/config
NETCONFIG_DNS_POLICY=""
Update The Config
reboot
Setting The DNS Server
Example of a /etc/resolv.conf
configuration
#Cloudflare
nameserver 1.0.0.1
#Google
#nameserver 8.8.8.8
#nameserver 8.8.4.4
#Cloudflare
#nameserver 1.1.1.1
#Classic Config
#nameserver 192.168.1.1
#search lan