How to check if a DNS server is configured in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a DNS server is configured on your Linux system. We will explore the primary configuration file /etc/resolv.conf to identify the configured DNS servers.

Following that, you will use the nslookup command to test DNS resolution and verify that your system can successfully translate domain names into IP addresses. Finally, we will inspect the systemd-resolved configuration file /etc/systemd/resolved.conf to understand how the local DNS resolver service is configured.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/RemoteAccessandNetworkingGroup(["Remote Access and Networking"]) linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/RemoteAccessandNetworkingGroup -.-> linux/netstat("Network Monitoring") linux/RemoteAccessandNetworkingGroup -.-> linux/ping("Network Testing") linux/RemoteAccessandNetworkingGroup -.-> linux/ip("IP Managing") subgraph Lab Skills linux/cat -.-> lab-558709{{"How to check if a DNS server is configured in Linux"}} linux/netstat -.-> lab-558709{{"How to check if a DNS server is configured in Linux"}} linux/ping -.-> lab-558709{{"How to check if a DNS server is configured in Linux"}} linux/ip -.-> lab-558709{{"How to check if a DNS server is configured in Linux"}} end

Check DNS with cat /etc/resolv.conf

In this step, we'll start by looking at how your Linux system finds websites and services on the internet. This process is called DNS resolution, and it's handled by a service that translates human-readable domain names (like google.com) into computer-readable IP addresses (like 172.217.160.142).

The primary configuration file for DNS resolution on many Linux systems is /etc/resolv.conf. This file tells your system which DNS servers to use.

Let's view the contents of this file using the cat command. The cat command is a simple but powerful tool used to display the content of files.

Open your terminal if it's not already open. You can find the Xfce Terminal icon on the left side of your desktop.

Now, type the following command and press Enter:

cat /etc/resolv.conf

You should see output similar to this:

## This is /run/systemd/resolve/stub-resolv.conf.
## Handled by systemd-resolved(8).
## DNS servers configured in /etc/systemd/resolved.conf.
nameserver 127.0.0.53
options edns0 trust-ad

Let's break down the output:

  • Lines starting with # are comments and are ignored by the system.
  • nameserver 127.0.0.53: This line is the most important here. It specifies the IP address of the DNS server your system will use. In this case, 127.0.0.53 is a special IP address that points to a local DNS resolver service, often systemd-resolved, which we'll look at later. This local resolver then forwards your DNS requests to external DNS servers.
  • options edns0 trust-ad: These are options related to DNS queries. edns0 is an extension mechanism for DNS, and trust-ad relates to DNSSEC (DNS Security Extensions). You don't need to worry about these options for now.

Understanding /etc/resolv.conf is the first step in troubleshooting network connectivity issues related to DNS. If this file is missing or contains incorrect information, your system might not be able to resolve domain names.

Click Continue to proceed to the next step.

Test DNS resolution with nslookup

In the previous step, we looked at the /etc/resolv.conf file, which tells your system where to find DNS servers. Now, let's actively test if your system can resolve a domain name using the nslookup command.

nslookup is a command-line utility for querying the Domain Name System (DNS) to obtain domain name or IP address mapping or for any other specific DNS record. It's a fundamental tool for network troubleshooting.

Let's use nslookup to find the IP address for example.com. Type the following command in your terminal and press Enter:

nslookup example.com

You should see output similar to this:

Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
Name:	example.com
Address: 93.184.216.34

Let's break down this output:

  • Server: 127.0.0.53: This shows the IP address of the DNS server that nslookup used for the query. As we saw in the previous step, this is the local resolver specified in /etc/resolv.conf.
  • Address: 127.0.0.53#53: This is the IP address and port number of the DNS server. DNS typically uses port 53.
  • Non-authoritative answer:: This indicates that the answer came from a caching DNS server, not the authoritative server for example.com.
  • Name: example.com: This is the domain name you queried.
  • Address: 93.184.216.34: This is the IP address that the DNS server returned for example.com. This is the address your computer would connect to when you try to visit example.com.

If nslookup returns an IP address, it means your system successfully resolved the domain name. If it fails, you might see an error message indicating a problem with DNS resolution.

You can try resolving other domain names as well, like google.com or labex.io.

nslookup google.com

The output will show the IP addresses associated with google.com. Note that a single domain name can have multiple IP addresses.

This command is invaluable for verifying that your DNS configuration is working correctly and for finding the IP addresses of specific domain names.

Click Continue to move on to the next step.

Inspect resolved config with cat /etc/systemd/resolved.conf

In the previous steps, we saw that /etc/resolv.conf often points to a local address (127.0.0.53) which is handled by systemd-resolved. This service is part of the systemd system and service manager and provides network name resolution to local applications.

The configuration for systemd-resolved is typically found in the /etc/systemd/resolved.conf file. Let's inspect this file to see how the local resolver is configured.

Use the cat command again to view the contents of /etc/systemd/resolved.conf. Type the following command in your terminal and press Enter:

cat /etc/systemd/resolved.conf

You should see output similar to this:

##  This file is part of systemd.
#
##  systemd is free software; you can redistribute it and/or modify it
##  under the terms of the GNU Lesser General Public License as published by
##  the Free Software Foundation; either version 2.1 of the License, or
##  (at your option) any later version.
#
## Entries in this file show the compile time defaults. Local configuration
## should be placed in a file located in /etc/systemd/resolved.conf.d/.
## See resolved.conf(5) for details.

[Resolve]
#DNS=
#FallbackDNS=
#Domains=
#DNSSEC=no
#DNSOverTLS=no
#MulticastDNS=yes
#LLMNR=yes
#Cache=yes
#DNSStubListener=yes
#ReadEtcHosts=yes

Let's look at the key parts of this file:

  • The file starts with comments explaining its purpose and licensing.
  • [Resolve]: This marks the beginning of the main configuration section for name resolution.
  • DNS=: This commented-out line is where you would typically list the IP addresses of the upstream DNS servers that systemd-resolved should use. Since it's commented out, systemd-resolved is likely getting its DNS server information from another source, such as your network configuration (e.g., DHCP).
  • FallbackDNS=: This commented-out line allows you to specify fallback DNS servers that will be used if the primary ones listed in DNS= are unreachable.
  • Other commented-out options control various aspects of systemd-resolved's behavior, such as DNSSEC validation, DNS over TLS, caching, and support for Multicast DNS (mDNS) and Link-Local Multicast Name Resolution (LLMNR).

In this environment, the actual DNS servers being used are likely provided by the underlying network infrastructure (the Docker container environment). systemd-resolved acts as a local cache and forwarder.

Understanding /etc/systemd/resolved.conf is important for advanced DNS configuration and troubleshooting on systems using systemd-resolved.

You have now successfully inspected the main configuration files related to DNS resolution on this Linux system.

Click Continue to complete this lab.

Summary

In this lab, we learned how to check the DNS server configuration on a Linux system. We started by examining the /etc/resolv.conf file using the cat command, which is the primary configuration file specifying the DNS servers used by the system. We understood that lines starting with # are comments and the nameserver line indicates the IP address of the DNS server, often a local resolver like 127.0.0.53 which is handled by systemd-resolved.

We then tested DNS resolution using the nslookup command to verify if the configured DNS server can successfully translate domain names into IP addresses. Finally, we inspected the configuration of the systemd-resolved service by viewing the /etc/systemd/resolved.conf file with cat, which provides further details about how the local DNS resolver is configured and forwards requests to external DNS servers. These steps provide a fundamental understanding of how DNS is configured and tested on Linux.