Reliable name resolution is essential for almost every networked service on a Linux server. Red Hat Enterprise Linux 8 gives you several places to configure how hostnames are resolved: the static /etc/hosts file, the resolver configuration in /etc/resolv.conf, the look-up order defined in /etc/nsswitch.conf, and DNS settings managed through NetworkManager. Knowing how these layers interact — and how to test them — will save you hours of networking troubleshooting. This tutorial covers each layer in turn.
Prerequisites
- A running RHEL 8 system with root or sudo access
- At least one configured network interface (check with
ip a) - The
bind-utilspackage installed fordigandnslookup(sudo dnf install -y bind-utils)
Step 1 — Configure Static Hosts in /etc/hosts
The /etc/hosts file maps hostnames to IP addresses without requiring a DNS server. It is consulted first (by default) and is ideal for small internal networks, container environments, or overriding DNS for testing.
# View the current /etc/hosts file
cat /etc/hosts
# Add a static entry for an internal server
sudo bash -c 'echo "192.168.1.50 db01.internal db01" >> /etc/hosts'
# Verify resolution from /etc/hosts
getent hosts db01.internal
Each line follows the format: IP_ADDRESS canonical_hostname [alias ...]. IPv6 addresses are supported on separate lines. Avoid duplicating entries — the first matching line wins.
Step 2 — Understand and Edit /etc/resolv.conf
The /etc/resolv.conf file tells the system which DNS servers to query and which domain suffixes to append to short hostnames. On a RHEL 8 system managed by NetworkManager, this file is typically a symlink or is overwritten on network changes — prefer nmcli for persistent settings (see Step 3).
# View the current resolver configuration
cat /etc/resolv.conf
# Key directives:
# nameserver 8.8.8.8 — DNS server to query (up to 3 allowed)
# search example.com — Append this domain to unqualified names
# domain example.com — Default domain (legacy; prefer 'search')
# Check whether /etc/resolv.conf is managed by NetworkManager
ls -la /etc/resolv.conf
Step 3 — Set DNS Persistently with NetworkManager (nmcli)
Editing /etc/resolv.conf directly will be overwritten on the next network event. Use nmcli to set DNS persistently against a connection profile so your settings survive reboots and interface restarts.
# List all NetworkManager connection profiles
nmcli connection show
# Set primary and secondary DNS on a connection named 'ens3'
sudo nmcli con mod ens3 ipv4.dns "8.8.8.8 1.1.1.1"
# Set the DNS search domain
sudo nmcli con mod ens3 ipv4.dns-search "example.com internal.example.com"
# Prevent NetworkManager from overwriting /etc/resolv.conf
# (only if you want full manual control)
sudo nmcli con mod ens3 ipv4.ignore-auto-dns yes
# Apply the changes
sudo nmcli con up ens3
# Confirm /etc/resolv.conf was updated
cat /etc/resolv.conf
Step 4 — Control Look-up Order with /etc/nsswitch.conf
The /etc/nsswitch.conf file controls the order in which different sources are consulted for name resolution. The hosts line is the most relevant for DNS troubleshooting.
# View the hosts resolution order
grep '^hosts' /etc/nsswitch.conf
# Typical output:
# hosts: files dns myhostname
# 'files' = /etc/hosts (checked first)
# 'dns' = /etc/resolv.conf nameservers
# 'myhostname'= systemd-resolved fallback for the local hostname
# To consult DNS before /etc/hosts (not recommended for security):
# hosts: dns files
Step 5 — Test DNS Resolution
Always verify your configuration with multiple tools, since each exercises a slightly different code path. getent honours nsswitch.conf, while dig and nslookup bypass it and query DNS directly.
# Test resolution honouring nsswitch.conf (including /etc/hosts)
getent hosts db01.internal
getent hosts google.com
# Query DNS directly with dig (bypasses /etc/hosts)
dig google.com
dig @8.8.8.8 google.com A # Query a specific server
dig google.com +short # Clean output
# Reverse DNS lookup
dig -x 8.8.8.8 +short
# Legacy tool - still useful for quick checks
nslookup google.com
nslookup google.com 1.1.1.1 # Query a specific DNS server
Conclusion
You have learned how all three layers of DNS resolution work together on RHEL 8: static entries in /etc/hosts, resolver settings in /etc/resolv.conf, and the look-up order enforced by /etc/nsswitch.conf. By setting DNS through nmcli you ensure your configuration survives network restarts, and by testing with both getent and dig you can quickly pinpoint whether an issue is in the static host file, the resolver, or an upstream DNS server. These skills are the foundation of all further network configuration work on your system.
Next steps: How to Configure a Static IP with nmcli on RHEL 8, How to Set Up a BIND DNS Server on RHEL 8, and How to Troubleshoot Network Connectivity on RHEL 8.