In Linux /etc/resolv.conf get often overwritten when we setup the DNS, because of the multitude of programs managing the DNS servers.
How to properly setup the DNS ?
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.)
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.
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
Config files
cat /etc/dnsmasq.conf
server=1.1.1.1
server=8.8.4.4
Update the config
sudo systemctl restart dnsmasq.service
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
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
Config files
$ cat /etc/dhcp3/dhclient.conf
supersede domain-name-servers <dns_ip_address1>,<dns_ip_address2>;
Update The Config
reboot
Disable rdnssd
systemctl disable --now rdnssd.service
Disable resolved
systemctl disable --now systemd-resolved.service
Config files
/etc/sysconfig/network/config
Disable netconfig
cat /etc/sysconfig/network/config
NETCONFIG_DNS_POLICY=""
Update The Config
reboot
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
getaddrinfo) in the C library is the interface to the DNS. The rest is just an implementation detail. The file /etc/resolve.conf is commonly used for configuring lookups, but not necessarily: a system using systemd-networkd and systemd-resolved can function perfectly well without /etc/resolve.conf.
On Fedora 36 I did:
# print existing DNS settings for all interfaces
$ resolvectl dns
# set new DNS
$ resolvectl dns enp0s22f6 8.8.8.8 8.8.4.4
# check again
$ resolvectl dns
Linux Gnome UI Solution:
Open network settings. For instance in Gnome, Wifi -> All Networks -> Tabs IPv4 and IPv6 -> DNS -> Add DNS entries.
Similarly, you can find these settings for your Desktop Environment if not using Gnome.