Network ConfigurationLesson 3 of 3
Host names and name resolution
Set a permanent host name, resolve names with /etc/hosts and DNS, and know which tool consults which source.
hostnamectlhostname/etc/hostname/etc/hosts/etc/nsswitch.confgetentdignmcli general hostnamebind-utils
After this lesson you can
- Set and verify the static host name so it survives a reboot.
- Add entries to /etc/hosts and explain the order in which files and DNS are consulted.
- Tell apart getent (what applications see) from dig (DNS only), and diagnose a name that fails to resolve.
The idea
The host name
A machine has up to three names, and hostnamectl shows them all:
| Name | Where | Purpose |
|---|---|---|
| Static | /etc/hostname | The configured name, applied at boot. This is the one you set |
| Transient | Kernel | Currently in use; may come from DHCP or DNS when no static name is set |
| Pretty | /etc/machine-info | Free-form display name |
Use the fully qualified domain name (FQDN) as the static name: node1.lab.example. The short name is the part before the first dot.
sudo hostnamectl set-hostname node1.lab.example
That single command writes /etc/hostname, sets the live name, and survives reboots. Do not edit /etc/hostname and forget to apply it, and do not rely on hostname NAME, which changes only the running kernel’s name until reboot.
Turning names into addresses
Programs that need an address for a name call the C library, which follows /etc/nsswitch.conf. On the hosts: line the sources are consulted in order:
hosts: files myhostname dns
files:/etc/hostsdns: the servers listed in/etc/resolv.confmyhostname: the machine’s own name
/etc/hosts is a plain table: address, canonical name, aliases.
127.0.0.1 localhost localhost.localdomain
192.168.122.51 node2.lab.example node2
It works with no network or DNS at all, which makes it ideal for a lab, for a few critical servers, and for overriding DNS temporarily. /etc/resolv.conf is generated by NetworkManager from the connection: nameserver lines for DNS servers and search for the domains appended to short names.
Which tool uses which source
This causes real confusion:
| Tool | Consults |
|---|---|
getent hosts, ping, ssh, curl, most programs | nsswitch.conf order: files, then DNS |
dig, host, nslookup | DNS only, never /etc/hosts |
So a name in /etc/hosts can ping perfectly and still show nothing in dig. Use getent hosts NAME to see what applications will see, and dig NAME to see what DNS says.
Try it
hostnamectl
Static hostname: node1.lab.example
Icon name: computer-vm
Chassis: vm
Machine ID: 3f1c0c3a6f4d4a1c8e2a7d0d9b0c5e11
Boot ID: 7b2e0d9f0a1c4a45a86b1f31d1f3a2c0
Virtualization: kvm
Operating System: Red Hat Enterprise Linux 10.0 (Coughlan)
Kernel: Linux 6.12.0-55.el10.x86_64
Architecture: x86-64
sudo hostnamectl set-hostname node1.lab.example
cat /etc/hostname
hostname -f
node1.lab.example
node1.lab.example
Your shell prompt still shows the old name until you open a new login shell.
A hosts entry
echo '192.168.122.51 node2.lab.example node2' | sudo tee -a /etc/hosts
getent hosts node2
ping -c1 node2
192.168.122.51 node2.lab.example node2
PING node2.lab.example (192.168.122.51) 56(84) bytes of data.
64 bytes from node2.lab.example (192.168.122.51): icmp_seq=1 ttl=64 time=0.412 ms
getent versus dig
grep '^hosts' /etc/nsswitch.conf
getent hosts node2.lab.example
dig +short node2.lab.example
hosts: files myhostname dns
192.168.122.51 node2.lab.example node2
dig printed nothing: DNS does not know the name, and dig does not read /etc/hosts. The system can still use the name.
When names fail but addresses work
ping -c1 8.8.8.8
ping -c1 example.com
cat /etc/resolv.conf
1 packets transmitted, 1 received, 0% packet loss
ping: example.com: Temporary failure in name resolution
# Generated by NetworkManager
nameserver 192.0.2.1
The network is fine and the first nameserver is wrong or unreachable. Fix it where it is generated:
sudo nmcli connection modify "System eth0" ipv4.dns 192.168.122.1
sudo nmcli connection up "System eth0"
getent hosts example.com
Going further
hostname -I prints all addresses, and hostname -s the short name. nmcli general hostname reads (or sets) the name through NetworkManager. resolvectl is available when systemd-resolved is in use, which is not the RHEL default. host -t MX example.com and dig +short TXT example.com query specific record types. Reverse lookups (dig -x 192.168.122.51) go through PTR records, which /etc/hosts does not provide to dig. For many hosts use DNS, since keeping /etc/hosts files in sync across machines does not scale; but for a handful of cluster nodes it is simple, robust and independent of the DNS server.
Practice
0 of 5 done
Do these on your own system. They get harder. Check each result, then tick it off.
- Warm-up
What is this machine called?
Display the host name in three ways: the short one, the fully qualified one, and the full
hostnamectlreport. Say which file stores the permanent name.Check your result
hostname,hostname -fandhostnamectlprint names./etc/hostnameholds the static name.Hint
hostnamectlshows static, transient and pretty names.cat /etc/hostname.Solution
hostname hostname -f hostnamectl cat /etc/hostname - Practice
Set the permanent name
Set the host name of this machine to
node1.lab.example. Make sure it is still in place after a reboot, and that no separate edit of any file is needed. Do the same on the second machine withnode2.lab.example.Check your result
hostnamectl --staticprintsnode1.lab.example.cat /etc/hostnameshows the same. After a reboot the prompt andhostnameshow the new name.Hint
sudo hostnamectl set-hostname node1.lab.examplewrites/etc/hostnameand applies at once. Open a new shell to see it in the prompt.Solution
sudo hostnamectl set-hostname node1.lab.example hostnamectl --static cat /etc/hostname hostname -f - Practice
Resolve a peer without DNS
Add
192.168.122.51 node2.lab.example node2to/etc/hostsonnode1and the matching line for node1 on node2. Verify withgetent hostsandpingby both the short and the long name. Explain why the entry works even though no DNS server knows the name.Check your result
getent hosts node2prints192.168.122.51 node2.lab.example node2.ping -c1 node2succeeds.Hint
Format: address, then the canonical name, then any aliases, separated by whitespace. Append, never overwrite, the existing
localhostlines.Solution
echo '192.168.122.51 node2.lab.example node2' | sudo tee -a /etc/hosts getent hosts node2 getent hosts node2.lab.example ping -c1 node2 - Practice
Why does ping work but dig does not?
Run
getent hosts node2anddig +short node2.lab.examplefor the name you put in/etc/hosts. Explain the different results using/etc/nsswitch.conf. Install the DNS tools if needed.Check your result
getentprints the address (it usesfilesfirst);digprints nothing because it asks the DNS server directly and never reads/etc/hosts.Hint
grep ^hosts /etc/nsswitch.confshows the order. Packagebind-utilsprovidesdig,hostandnslookup.Solution
sudo dnf install -y bind-utils grep '^hosts' /etc/nsswitch.conf getent hosts node2.lab.example dig +short node2.lab.example - Challenge
Names fail but addresses work
Make DNS fail on purpose by pointing the connection at an unused DNS server (for example
192.0.2.1). Show thatping 8.8.8.8works whileping example.comfails, and that/etc/hostsentries still resolve. Repair it by setting the correct server on the connection, not in/etc/resolv.conf, and show the file updated.Check your result
Broken:
ping example.comgivesTemporary failure in name resolutionor times out. Afternmcli connection modify ... ipv4.dnsandup,getent hosts example.comworks and/etc/resolv.confshows the rightnameserver.Hint
Remember
/etc/resolv.confis generated by NetworkManager. Changeipv4.dnsand reapply.Solution
sudo nmcli connection modify "System eth0" ipv4.dns 192.0.2.1 sudo nmcli connection up "System eth0" ping -c1 8.8.8.8 ping -c1 example.com getent hosts node2 sudo nmcli connection modify "System eth0" ipv4.dns 192.168.122.1 sudo nmcli connection up "System eth0" cat /etc/resolv.conf getent hosts example.com