fbpx Configure DNS on Linux: resolv.conf and nmcli | Certificação Linux

How to Configure DNS on Linux: resolv.conf, nmcli and systemd-resolved

📅 12/08/2021 ⏱ 5 min ✍️ Uira Ribeiro
Linuxcommands
How to Configure DNS on Linux: resolv.conf, nmcli and systemd-resolved
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

DNS – Domain Name System is used for name resolution to IP addresses and vice-versa. In this article we explain in a simple way how to configure DNS on Linux manually.

The implementation of the name resolution service is done by the BIND package or other DNS server packages. This package contains the necessary files for DNS configuration and the service called named.

DNS is structured on two basic points, the organization of the Internet into domains and the distribution of name servers on the network in the form of a hierarchy.

The organization of the Internet into domains prevents the use of the same name by more than one device and decentralizes the registration of networks and equipment.

Domains can be hierarchized by institutions and geographically.

The distribution of DNS servers is necessary since the centralization of a single database containing the information for the conversion of symbolic names into IP addresses would be unfeasible.

The Internet itself was born from a project called ARPANET, created by the US Department of Defense, which advocated for a network without a central backbone and without centralized administration, making it virtually indestructible if some computer, or piece of the network became inoperative.

On the internet there are several root servers geographically scattered and organized through suffixes. For example, the root servers of the suffix .br are hosted at registro.br. The root servers of registro.br need to know all the DNS servers that host domains ending with .br.

There are 3 important configuration files for name resolution, already mentioned in the book:

  • /etc/nsswitch.conf: This file has a line with the command "hosts" that determines the search order for name resolution. The "files" tag determines that /etc/hosts should be consulted and the "dns" tag determines that the file /etc/resolv.conf should be consulted to determine the IP address of the DNS server.
  • /etc/resolv.conf: This file has a line with the nameserver command that determines the IP address of the DNS server;
  • /etc/hosts: This file can contain some name resolutions to IPs manually;

When accessing the Internet through a browser or any other network application, the Linux host follows the following sequence to identify the IP address of the desired Internet address name:

  1. Linux checks the file /etc/nsswitch.conf to know the search order: whether it is in file (/etc/hosts) or by DNS server (/etc/resolv.conf).
  2. If the order in /etc/nsswitch.conf is first "files", the file /etc/hosts is consulted to try to determine the name resolution. If the occurrence is found, the IP address will be returned. If not found, the host will read the DNS server IP address from the file /etc/resolv.conf and will make a query to the DNS server to try to determine the IP address of the desired address. If the server returns an IP address, the name will be resolved. Otherwise, the application will receive a name resolution failure.
  3. If the order in /etc/nsswitch.conf is first "dns", the file /etc/resolv.conf is consulted to get the DNS server IP address and a query will be made to the DNS server to try to determine the IP address of the desired address. If the DNS server finds the address, the application will receive the desired IP address. Otherwise, the host will access the file /etc/hosts to try to determine the IP address of the desired address. If found, the IP address will be returned. If negative, the application will receive a name resolution failure.

Example of /etc/nsswitch.conf configuration

passwd:         files systemdgroup:          files systemdshadow:         filesgshadow:        fileshosts:          files dnsnetworks:       files

Example of basic /etc/resolv.conf file configuration:

search meudominio.com.brnameserver 10.25.12.1nameserver 200.216.215.5

Example of basic /etc/hosts file configuration:

127.0.0.1    localhost.localdomain            localhost 10.25.12.1   www.certificacaolinux.com.br     www 

It is important that you understand the functioning of the name resolution process, which the files /etc/hosts, /etc/nsswitch.conf, /etc/resolv.conf regulate the DNS client configuration.

It is important to know that the name resolution order in the file /etc/nsswitch.conf affects how name resolution will behave. As soon as a name resolution occurrence is found, whether on the DNS server or locally in the /etc/hosts file, the host considers itself satisfied and does not resort to another service.

With that, the following error cases may occur:

  • There may be an address listed in /etc/hosts manually, which differs from the IP address that DNS servers resolve. This can cause the host to connect to a wrong server, or even not be able to connect because the IP address does not exist;
  • A certain DNS server listed in /etc/resolv.conf is not active or the host cannot get a connection to it, making name resolution impossible;

Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp