| Tested on | RHEL 10.2 (Coughlan) — vm1.lab.example (192.168.56.116), vm2.lab.example (192.168.56.117) |
|---|---|
| Package | systemd 257-23.el10_2.2bind-utils 9.18.33-15.el10_2.2 |
| Applies to | Ubuntu, Debian, Kali Linux, Linux Mint, Pop!_OS, Raspberry Pi OS, elementary OS, Zorin OS, Parrot OS, MX Linux, RHEL, Rocky Linux, AlmaLinux, Oracle Linux, CentOS Stream, Fedora, Arch Linux |
| Privilege | Normal user for lookups; sudo or root to edit /etc/hosts, nsswitch.conf, and NetworkManager profiles |
| Scope | Name resolution flow, hostname vs FQDN, /etc/hosts, nsswitch.conf, getent hosts, dig and host, DNS servers and search domains with NetworkManager, resolv.conf, and ordered troubleshooting. Does not cover BIND server setup, changing the local hostname with hostnamectl, or full DNS protocol theory. |
| Related guides | hostnamectl command dig and host commands nmcli command examples RHCSA tutorial Linux command line |
When you ssh vm2.lab.example, the shell does not magically know 192.168.56.117. Applications ask the Name Service Switch (NSS) layer, which may read /etc/hosts, query DNS, or consult other backends configured on your system. This walkthrough uses two lab hosts: vm1.lab.example resolves peers through local files and DNS depending on the name you test.
How Linux Hostname Resolution Works
Application (ssh, curl, ping)
↓
libc / getent
↓
Name Service Switch (nsswitch.conf)
├── files → /etc/hosts
├── dns → DNS servers (resolv.conf / stub resolver)
├── myhostname → local machine name (RHEL/Fedora)
└── other backends if configured (LDAP, etc.)Resolution is not one fixed path. The hosts: line in /etc/nsswitch.conf defines which sources NSS tries and in which order. The first source that returns an answer wins for getent hosts.
Hostname Resolution Quick Reference
| Task | Command or file |
|---|---|
| Show local hostname | hostnamectl |
| Test NSS hosts database | getent hosts NAME |
| Test closer to modern apps | getent ahosts NAME |
| Query DNS directly | dig NAME |
| Simple DNS lookup | host NAME |
| Local static mappings | /etc/hosts |
| Resolution order | /etc/nsswitch.conf |
| NetworkManager DNS | nmcli device show |
| Resolver file (often generated) | /etc/resolv.conf |
Hostname vs DNS Name vs FQDN
| Term | Meaning |
|---|---|
| Static hostname | Name this machine uses for itself (hostnamectl) |
| Short name | Single label such as vm2 or server |
| FQDN | Host plus domain, such as vm2.lab.example |
| DNS domain | Suffix published in DNS zones (lab.example) |
| Alias | Extra name in /etc/hosts or DNS pointing at the same address |
On the lab client:
hostnamectl statusStatic hostname: vm1.lab.example
Icon name: computer-vm
Chassis: vm 🖴
Machine ID: 23b7a5ba4a464d768c37c2b2990e7d06
Boot ID: a15989ae19014058b9f02b857023ac9f
Product UUID: d5f4d06c-44bf-4b46-b216-ff4f3472c78d
Virtualization: oracle
Operating System: Red Hat Enterprise Linux 10.2 (Coughlan)
CPE OS Name: cpe:/o:redhat:enterprise_linux:10.2
Kernel: Linux 6.12.0-211.42.1.el10_2.x86_64
Architecture: x86-64
Hardware Vendor: innotek GmbHStatic hostname is how this VM names itself. Remote peers you ssh to are separate DNS or /etc/hosts entries. To change the local hostname, see hostnamectl command.
Configure Local Name Resolution with /etc/hosts
/etc/hosts maps addresses to names before DNS is consulted when files appears first in nsswitch.conf.
Syntax:
192.0.2.10 server.example.com server| Piece | Role |
|---|---|
| Address | IPv4 or IPv6 |
| Canonical name | First hostname after the address |
| Aliases | Additional names on the same line |
This lab already maps vm2:
grep vm2 /etc/hosts192.168.56.117 vm2.lab.example vm2Add a temporary peer entry:
sudo tee -a /etc/hosts <<'EOF'
192.168.56.118 labpeer.lab.example labpeer
EOFIPv6 entries use the same layout with an address such as 2001:db8::10 in the first column.
Understand /etc/nsswitch.conf
The hosts: line controls NSS order for hostname lookups:
grep '^hosts:' /etc/nsswitch.confhosts: files dns myhostname| Source | Typical role |
|---|---|
files |
/etc/hosts |
dns |
DNS servers from resolver configuration |
myhostname |
Resolves the local static hostname (common on RHEL and Fedora) |
Distributions differ. Some systems add resolve for systemd-resolved or mdns4_minimal for multicast DNS. Read the actual line on the host you troubleshoot rather than assuming a universal template.
Test the Complete Linux Resolution Path
getent hosts tests the NSS hosts database and shows whether /etc/hosts, DNS, or another configured NSS backend can resolve the name. It uses the legacy gethostbyname2() / gethostbyaddr() interfaces—not getaddrinfo(), which many modern applications call instead.
getent hosts labpeer.lab.example192.168.56.118 labpeer.lab.example labpeerShort names resolve when an alias or search domain applies:
getent hosts labpeer192.168.56.118 labpeer.lab.example labpeerCompare with a name that exists only in /etc/hosts but not in public DNS:
getent hosts vm2.lab.example192.168.56.117 vm2.lab.example vm2Query DNS for the same FQDN with full dig so you can read the response status in the header (+short hides it):
dig vm2.lab.example;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 62341
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1
;; QUESTION SECTION:
;vm2.lab.example. IN Astatus: NXDOMAIN means the DNS server reports that name does not exist in DNS. An empty dig +short result alone is weaker—it only shows no address in the answer section and can also appear when the name exists but the queried record type has no data.
getent succeeded from the NSS files source; dig queried DNS and therefore did not use /etc/hosts. That split is normal and is why both tools matter in troubleshooting.
Remove the temporary mapping:
sudo sed -i '/labpeer/d' /etc/hostsQuery DNS Directly
dig and host speak to DNS servers configured in resolver settings, not to NSS file order.
Public name through DNS:
dig +short google.com142.250.67.46NSS should return the same address when dns is in the hosts: line:
getent hosts google.com142.250.67.46 google.comWhen both agree, NSS reached DNS successfully.
DNS failure for a hosts-only name:
host vm2.lab.exampleHost vm2.lab.example not found: 3(NXDOMAIN)| Observation | Likely meaning |
|---|---|
dig/host work, getent fails for the same FQDN |
NSS order, missing dns in nsswitch, or bracketed NSS actions such as [NOTFOUND=return] |
dig/host work, getent fails for a short name only |
Search-domain mismatch or no /etc/hosts alias for the short form |
getent works, dig returns NXDOMAIN |
Answer came from /etc/hosts or another non-DNS NSS source |
| Both fail | Name missing everywhere, or DNS/network path broken |
More query patterns live in dig and host commands.
Configure DNS Servers with NetworkManager
On RHEL and Fedora desktops and servers with NetworkManager, DNS servers and search domains usually come from connection profiles—not hand-edited resolv.conf. RHEL 10 documents ipv4.dns and ipv4.dns-search as the NetworkManager properties for these settings.
List active connection profiles so you know which name to edit:
nmcli -t -f NAME,DEVICE con show --activeenp0s3:enp0s3
Internal-LAN:enp0s8
lo:loSet a static DNS server and search suffix on the lab LAN profile (Internal-LAN on this host). The address 192.0.2.53 is documentation TEST-NET space—replace it with a resolver that exists on your network:
sudo nmcli con mod Internal-LAN ipv4.dns "192.0.2.53"Add the search domain on the same profile:
sudo nmcli con mod Internal-LAN ipv4.dns-search "lab.example"Reactivate the profile so NetworkManager regenerates resolver data:
sudo nmcli con up Internal-LANConnection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)Confirm DNS and domain settings on the interface that uses that profile:
nmcli device show enp0s8 | grep -E 'IP4\.(DNS|DOMAIN)'IP4.DNS[1]: 192.0.2.53When the profile still uses DHCP (ipv4.method auto), NetworkManager may merge manually configured DNS and search values with values supplied automatically by DHCP. The system-wide /etc/resolv.conf can list nameservers from more than one active connection:
grep -E '^(search|nameserver)' /etc/resolv.confsearch nsn-intra.net lab.example
nameserver 192.168.0.1
nameserver fd17:625c:f037:2::3
nameserver 192.0.2.53To replace DHCP-provided DNS entirely on that profile instead of merging with it, set:
sudo nmcli con mod Internal-LAN ipv4.ignore-auto-dns yesThen reactivate the connection. NetworkManager documents this property explicitly for profiles that should use only the DNS servers you configure.
For more connection editing patterns, see nmcli command examples.
Understand DNS Search Domains
Search domains let applications resolve short names by appending suffixes from search or domain lines in resolver configuration. glibc applies the search list to names with fewer dots than the ndots threshold (often 1), so server.example.com is queried as written while server may expand to server.lab.example.
With search lab.example in /etc/resolv.conf, an application may expand server to server.lab.example before querying DNS.
| You type | Resolver may try |
|---|---|
vm2.lab.example |
Exact FQDN first |
vm2 |
vm2.lab.example if lab.example is in search |
server |
server.lab.example, then other search suffixes |
/etc/hosts aliases can make short names work without DNS. vm2 resolves here because the line includes the vm2 alias, not because DNS published it.
Check Resolver Configuration
Read the active resolver file the system uses:
cat /etc/resolv.conf# Generated by NetworkManager
search nsn-intra.net lab.example
nameserver 192.168.0.1
nameserver fd17:625c:f037:2::3On NetworkManager-managed systems, the header Generated by NetworkManager means manual edits may be overwritten when a connection reactivates. Change DNS on the connection profile instead.
Some installations use systemd-resolved with a stub file at /etc/resolv.conf pointing to 127.0.0.53. The troubleshooting pattern is the same: find which component owns DNS (nmcli, resolved, or static files) before you edit.
Troubleshoot Hostname Resolution
Work in order:
- Confirm the exact name the application uses (FQDN vs short name).
- Check
/etc/hostsfor a static mapping. - Read
hosts:in/etc/nsswitch.conf. - Inspect DNS servers and
searchinnmcli device showand/etc/resolv.conf. - Query DNS directly with
digorhost. - Verify search domains expand short names as you expect.
- Confirm routing to the DNS server (
ip route) when errors mention timeouts. - Check firewalls only when DNS UDP/TCP to the server is blocked.
When you want behavior closer to modern applications (including IPv4/IPv6 address selection), getent ahosts uses getaddrinfo():
getent ahosts vm2.lab.example192.168.56.117 STREAM vm2.lab.example
192.168.56.117 DGRAM
192.168.56.117 RAW| Symptom | Likely cause | Next step |
|---|---|---|
Name or service not known |
No NSS source returned an answer | getent hosts NAME; check hosts file and nsswitch |
Temporary failure in name resolution |
DNS unreachable or timeout | dig NAME; check routes and nameserver reachability |
NXDOMAIN from dig |
Name absent in DNS | Add DNS record or use /etc/hosts for lab names |
| Wrong IP returned | Stale /etc/hosts entry wins over DNS |
Compare getent vs dig; fix files line or order |
| FQDN works, short name fails | Missing or wrong search domain, or no /etc/hosts short alias |
Check search in /etc/resolv.conf, ipv4.dns-search, and /etc/hosts aliases |
| Short name works, expected FQDN fails | Short name likely from a /etc/hosts alias or another NSS source |
Compare getent for both forms; inspect hosts file before blaming search domains |
dig works, application fails for same FQDN |
NSS path or bracketed NSS actions differ from direct DNS | Test getent hosts and getent ahosts with the exact string the app uses |
Practical Resolution Examples
Two-host lab workflow on vm1.lab.example:
- Map the peer in
/etc/hosts:
grep vm2 /etc/hosts- Confirm NSS resolution:
getent hosts vm2192.168.56.117 vm2.lab.example vm2- Confirm DNS does not publish the lab name (expected in this lab)—read
status:in thedigheader:
dig vm2.lab.example +noall +comments +answer;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 53019
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1-
Set DNS servers and search suffixes on the connection profile with
nmcli(ipv4.dnsandipv4.dns-searchonInternal-LANin this lab). -
Connect by name (resolution succeeds before SSH authenticates):
getent hosts vm2.lab.exampleUse getent to prove the name resolves; use ssh, curl, or your application to test the full connection after that.
References
- hosts(5) — Linux manual page
- nsswitch.conf(5) — Linux manual page
- resolv.conf(5) — Linux manual page
- getent(1) — Linux manual page
- Red Hat — Configuring the order of DNS servers
- Red Hat — Changing a hostname using hostnamectl
Summary
Linux hostname resolution is a chain: applications call NSS, NSS reads nsswitch.conf, and sources such as /etc/hosts and DNS answer in order. getent hosts tests the NSS hosts database; getent ahosts follows getaddrinfo() closer to what modern apps use. dig and host query DNS directly and can disagree when /etc/hosts or another NSS source answers first.
On this lab, vm2.lab.example resolves through /etc/hosts while DNS returns NXDOMAIN in the dig header—a pattern you will see for internal names until you publish DNS records or keep static mappings. Configure DNS servers with ipv4.dns and search domains with ipv4.dns-search on NetworkManager connection profiles; use ipv4.ignore-auto-dns when you must replace DHCP DNS instead of merging with it. Treat generated /etc/resolv.conf as output rather than the primary edit target.
When troubleshooting, compare getent hosts, getent ahosts, full dig status lines, nsswitch.conf, and resolver settings before you blame the application. For changing this machine’s hostname, use hostnamectl command. For DNS query syntax, see dig and host commands. For connection profiles, see nmcli command examples.

