/etc/resolv.conf
From: https://www.baeldung.com/linux/dns-resolv-conf-file
Configuring DNS in the resolv.conf File
Last updated: November 30, 2022 Written by: baeldung
1. Overview
Domain Name System (DNS) is a vital service for the Intranet and Internet.
It’s responsible for translating machine names into IP addresses. In this
tutorial, we’ll explain multiple ways to configure DNS in the Linux
operating system.
2. Domain Name System
DNS is a mechanism to make the internet human-friendly. Computers
communicate with each other using their IP addresses. There are lots of IP
addresses in the internet world, and it’s impossible to remember them all.
To solve this issue and make it more human-friendly, DNS was invented. DNS
servers map IP addresses to hostnames. When we enter a domain name like
baeldung.com into our browser, the computer finds our nearest DNS server and
asks what’s the correct IP address for baeldung.com.
Then, it returns the IP address to our system so that it can communicate
with the baeldung.com server. For example, DNS translates the domain name
baeldung.com to IP address 172.66.40.248. In Linux, there are DNS lookup
tools like nslookup and dig, which are made to query DNS servers. For
getting the IP address of a domain name, we can use the nslookup command:
$ nslookup baeldung.com
Server: 8.8.8.8
Address: 8.8.8.8#53
Non-authoritative answer:
Name: baeldung.com
Address: 172.66.40.248
Name: baeldung.com
Address: 172.66.43.8
3. DNS Configuration
There are two approaches to configuring a Linux system’s DNS service:
Resolver Configuration File, and Hosts.
3.1. Resolver Configuration File
We’re able to set the DNS configuration in network interface config files.
However, this is not the only way. We can configure a DNS service using the
/etc/resolv.conf
file. If we want to change our DNS configuration, we can
use the nameserver keyword:
$ cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 4.2.2.4
In the above configuration, we set the DNS servers to public DNS servers
like the Google server (8.8.8.8). Also, note that any changes made manually
to the /etc/resolv.conf
configuration file is bound to be overwritten upon
changes in the network or upon system reboot.
3.2. Hosts
Another way to configure a Linux system’s DNS service is by manipulating
the /etc/hosts
file. The /etc/hosts
file contains server names and their IP
addresses statically saved:
$ cat /etc/hosts
127.0.0.1 localhost
This file can be changed by the root user and will map domain names to IP
addresses. The /etc/hosts
file has a higher priority than /etc/resolv.conf
file.
4. DNS Priority
DNS priority tells the system about the priority of DNS lookup. Linux
normally performs lookups in /etc/hosts
before it uses DNS. We can modify
this behavior by editing the /etc/nsswitch.conf
file, and specifically, the
hosts line. Let’s check the DNS lookup order in /etc/nsswitch.conf
file:
$ cat /etc/nsswitch.conf | grep hosts
hosts: files mdns4_minimal [NOTFOUND=return] dns
The above configuration means that DNS lookup refers to files (/etc/hosts
)
first, and then DNS servers specified in DNS (/etc/resolv.conf
). This means
when the system wants to find the IP address of a domain name, it first
reads the /etc/hosts
file and then /etc/resolv.conf
.
5. Conclusion
In this article, we explained the DNS service and various ways of
configuring it in the Linux operating system. Additionally, we discussed how
Linux determines the DNS lookup priority.